www.gusucode.com > VC++实现Vista玻璃窗体程序 > VC++实现Vista玻璃窗体程序/gusucode/Vista玻璃窗体程序文件/GDIPNGDlg.cpp

    // GDIPNGDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GDIPNG.h"
#include "GDIPNGDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
extern int m_TopWidth;
extern Image *m_pImageTopc;            // 背景图像指针,Image是一个图像类。
extern int m_TopWidth;
extern int m_TopHeight;
extern int Pos_Top_x;
extern int Pos_Top_y;
//按钮的状态
extern BOOL m_bOver;	//鼠标位于按钮之上时该值为true,反之为flase
extern BOOL m_bTracking;	//在鼠标按下没有释放时该值为true
extern BOOL m_bSelected;	//按钮被按下是该值为true
extern BOOL m_bFocus;	//按钮为当前焦点所在时该值为true
extern BOOL m_bChe;
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGDIPNGDlg dialog

CGDIPNGDlg::CGDIPNGDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGDIPNGDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGDIPNGDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGDIPNGDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGDIPNGDlg)
	DDX_Control(pDX, IDC_BUTTON1, m_CloseBtn);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGDIPNGDlg, CDialog)
	//{{AFX_MSG_MAP(CGDIPNGDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_LBUTTONDOWN()
	ON_WM_SIZE()
	ON_WM_RBUTTONDOWN()
	ON_WM_DRAWITEM()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGDIPNGDlg message handlers

BOOL CGDIPNGDlg::OnInitDialog()
{
	CAboutDlg Dlg2;
	CDialog::OnInitDialog();
	// Add "About..." menu item to system menu.
 Dlg2.Create(IDD_ABOUTBOX,NULL); Dlg2.ShowWindow(SW_SHOW);
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	static CRect RectButton;
	GetWindowRect(&RectButton);
	GetDlgItem(IDC_BUTTON1)->GetWindowRect(&RectButton);//top 190, bottom 549, left 269, right 755
	ScreenToClient(&RectButton);//把屏幕坐标转化成用户坐标
	static CRect f;
	f.top=RectButton.top;//258
	f.bottom =RectButton.bottom ;//495
	f.left =RectButton.left ;//424
	f.right =RectButton.right ;//436
    GetDlgItem(IDC_BUTTON1)->MoveWindow(Pos_Top_x,Pos_Top_y,m_TopWidth,m_TopHeight);
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGDIPNGDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CGDIPNGDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
		//MessageBox("aa");
		HDC hdcTemp = GetDC()->m_hDC;
		m_hdcMemory = CreateCompatibleDC(hdcTemp);
		HBITMAP hBitMap = CreateCompatibleBitmap(hdcTemp, m_BakWidth, m_BakHeight);
		SelectObject(m_hdcMemory, hBitMap);
		
		// m_Blend.SourceConstantAlpha=100;                              // 窗口透明度最大为255,最小为0
		HDC hdcScreen = ::GetDC(m_hWnd);
		RECT rct;
		GetWindowRect(&rct);
		POINT ptWinPos = {rct.left, rct.top};
		
		Graphics graph(m_hdcMemory);
		Rect destinationRect(0, 0, 1 * m_BakWidth, 1* m_BakHeight);
		graph.DrawImage(
			m_pImageBack,
			Rect(0, 0,  m_BakWidth,  m_BakHeight),  // destination rectangle 
		//destinationRect,
			0, 0,        // upper-left corner of source rectangle
			m_BakWidth,       // width of source rectangle
			m_BakHeight,      // height of source rectangle
			UnitPixel);

		//修改风格   
       graph.DrawImage(m_pImageTop,Pos_Top_x,Pos_Top_y);
	   if (m_bOver)
	   {
		  graph.DrawImage(m_pImageTopc,Pos_Top_x-8,Pos_Top_y-9);
		}
	   else if(m_bChe){
		   graph.DrawImage(m_pImageTop,Pos_Top_x,Pos_Top_y);
	   }
	   
	   
			//绘制窗体标题
		FontFamily  fontFamily(L"Arial");
		Font        font(&fontFamily, 12, FontStyleRegular, UnitPixel);
		PointF      pointF(15.0f, 19.0f);
		SolidBrush  solidBrush1(Color(180, 0, 0, 0));
		graph.SetTextRenderingHint(TextRenderingHintAntiAlias);
		graph.DrawString(L"GDI+ Demo - UPC Kid", -1, &font, pointF, &solidBrush1);

		SIZE sizeWindow={m_BakWidth,m_BakHeight};
		POINT ptSrc={0,0};
		DWORD dwExStyle=GetWindowLong(m_hWnd,GWL_EXSTYLE);
		if((dwExStyle&0x80000)!=0x80000)
			SetWindowLong(m_hWnd,GWL_EXSTYLE,dwExStyle^0x80000);
		UpdateLayeredWindow(m_hWnd, hdcScreen, &ptWinPos, &sizeWindow, m_hdcMemory, &ptSrc, 0, &m_Blend, 2);
		
		graph.ReleaseHDC(m_hdcMemory);
		::ReleaseDC(m_hWnd,hdcScreen);
		hdcScreen=NULL;
		::ReleaseDC(m_hWnd,hdcTemp);
		hdcTemp=NULL;
		DeleteObject(hBitMap);
		DeleteDC(m_hdcMemory);
           m_hdcMemory=NULL;
		
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGDIPNGDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

int CGDIPNGDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	hFuncInst = LoadLibrary("User32.DLL"); 
	if(hFuncInst) 
		UpdateLayeredWindow=(MYFUNC)GetProcAddress(hFuncInst, "UpdateLayeredWindow");
	else
	{
		AfxMessageBox("User32.dll ERROR!");
		exit(0);
	}
	//初始化gdiplus的环境
	// Initialize GDI+.
	m_Blend.BlendOp=0; //theonlyBlendOpdefinedinWindows2000
	m_Blend.BlendFlags=0; //nothingelseisspecial...
	m_Blend.AlphaFormat=1; //...
	m_Blend.SourceConstantAlpha=255;//AC_SRC_ALPHA            // 透明度
	
	using namespace Gdiplus;            // 名称空间
//	m_pImageBack = Gdiplus::Image::FromFile(L"123.png");   // 直接读取文件,注意是双“\\”斜杠
	  ImageFromIDResource(IDR_PNG2, "PNG", m_pImageBack);      // 读取资源中的PNG图片, 为自订义类形“PNG”
	  ImageFromIDResource(IDR_PNG1, "PNG", m_pImageTop); 
	  ImageFromIDResource(IDR_PNG3, "PNG", m_pImageTopc);
	  //ImageFromIDResource(IDR_PNG4, "PNG", m_pImageTop);

	  // 读取资源中的PNG图片, 为自订义类形“PNG”
	// 这里Image没有提供字节调用资源中图像的函数,
	// ImageFromIDResource()是通过资源名称"PNG"和资源ID号将图像的Image指针传递给指针应用。来完成的。
	// 这个函数是摘自网络
	// ImageFileMe("F:\\abc.png", m_pImageBack);                        // 读取二进制文件
	m_BakWidth = m_pImageBack->GetWidth();                        // 返回图片宽度
	m_BakHeight = m_pImageBack->GetHeight();                  // 返回图片高度
//	m_pImageTop = Gdiplus::Image::FromFile(L"001.png");
//	Image image(L"001.png");
	m_TopWidth=m_pImageTop->GetWidth();
	m_TopHeight=m_pImageTop->GetHeight();
	Pos_Top_x=408;	Pos_Top_y=11;							//右上角关闭按钮图标的位置
	//////////////////////////////////////////////////////////////////////////
//	CAboutDlg abd;
//abd.DoModal();
		// 以下是实现窗口在最上面。
	//::SetWindowPos(m_hWnd, HWND_TOPMOST,0,0,m_BakWidth,m_BakHeight,SWP_NOSIZE|SWP_NOMOVE);
    // this->MoveWindow(0, 0, m_BakWidth, m_BakHeight);
	 //////////////////////////////////////////////////////////////////////////
	 
	return 0;
}

void CGDIPNGDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	// TODO: Add your message handler code here
	delete m_pImageBack;
     m_pImageBack = NULL;
}
BOOL CGDIPNGDlg::ImageFromIDResource(UINT nID, LPCTSTR sTR,Image * &pImg)
{
	HINSTANCE hInst = AfxGetResourceHandle();
	HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),sTR); // type
	if (!hRsrc)
		return FALSE;
	
	// load resource into memory
	DWORD len = SizeofResource(hInst, hRsrc);
	BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
	if (!lpRsrc)
		return FALSE;
	
	// Allocate global memory on which to create stream
	HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
	BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
	memcpy(pmem,lpRsrc,len);
	IStream* pstm;
	CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);
	
	// load from stream
	pImg=Gdiplus::Image::FromStream(pstm);
	
	// free/release stuff
	GlobalUnlock(m_hMem);
	pstm->Release();
	FreeResource(lpRsrc);
}
void CGDIPNGDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//禁止显示移动矩形窗体框
	::SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,TRUE,NULL,0);
	//非标题栏移动整个窗口
	SendMessage(WM_SYSCOMMAND,0xF012,0);
	// PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));

	CDialog::OnLButtonDown(nFlags, point);
}

void CGDIPNGDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
		if (nType==SIZE_MAXIMIZED )
	{
		MessageBox("123456");
	}
}

//DEL void CGDIPNGDlg::OnButton2() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	SendMessage(WM_CLOSE,1,0);
//DEL }

void CGDIPNGDlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//SendMessage(WM_CLOSE,1,0);
	//////////////////////////////////////////////////////////////////////////
	/*static CRect RectButton;
	GetWindowRect(&RectButton);
	GetDlgItem(IDC_BUTTON1)->GetWindowRect(&RectButton);//top 190, bottom 549, left 269, right 755
	ScreenToClient(&RectButton);//把屏幕坐标转化成用户坐标
	static CRect f;
	f.top=RectButton.top;//258
	f.bottom =RectButton.bottom ;//495
	f.left =RectButton.left ;//424
	f.right =RectButton.right ;//436
    GetDlgItem(IDC_BUTTON1)->MoveWindow(Pos_Top_x,Pos_Top_y,m_TopWidth,m_TopHeight);*/
//GetWindow(IDD_DIALOG1)->ShowWindow(5);

	//////////////////////////////////////////////////////////////////////////
//	CString str;
	//str.Format("Pza=%d",Pza);
	//MessageBox(str);
	CDialog::OnRButtonDown(nFlags, point);
}

void CGDIPNGDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

void CGDIPNGDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	SendMessage(WM_CLOSE,1,0);
}

void CGDIPNGDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//CString str;
	//str.Format("m_bOver=%d",m_bOver);
//MessageBox(str);
	CGDIPNGDlg::OnPaint() ;
	CDialog::OnMouseMove(nFlags, point);
}