// WinButton.cpp: implementation of the CWinButton class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h" #include "WinButton.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWinButton::CWinButton() { }
CWinButton::~CWinButton() {
}
BOOL CWinButton::Init(HWND hWnd) { if (IsWindow(hWnd)) { m_hWnd = hWnd;//托管原来的BUTTON
m_parenthWnd = GetParent(m_hWnd); if (SetProp(m_hWnd,"CWINBUTTON",(HANDLE)this) == 0) { return FALSE; } m_OldProc = SetWindowLong(m_hWnd,GWL_WNDPROC,(LONG)stdProc);//改变窗口回调过程
} return FALSE; }
LRESULT WINAPI CWinButton::stdProc(HWND hWnd,UINT uMsg,UINT wParam,LONG lParam) { CWinButton* cw = (CWinButton*)GetProp(hWnd,"CWINBUTTON"); return cw->WndProc(uMsg,wParam,lParam); } //改变原BUTTON的回调过程
LRESULT CALLBACK CWinButton::WndProc(UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch(message) { case WM_PAINT://调用自己设定的刷新
hdc = BeginPaint(m_hWnd, &ps); OnPaint(hdc); EndPaint(m_hWnd, &ps); break; } return CallWindowProc((WNDPROC)m_OldProc, m_hWnd, message, wParam, lParam); //默认回调过程
} //在这里添加BUTTON的背景设置
LRESULT CWinButton::OnPaint(HDC hdc) { HDC hdcMem,hdcBmp; RECT rt; int width,height; HBITMAP bmp; SetBkMode(hdc,TRANSPARENT); GetClientRect(m_hWnd,&rt); width = rt.right - rt.left; height = rt.bottom - rt.top; hdcMem = CreateCompatibleDC(hdc); hdcBmp = CreateCompatibleDC(hdc); SetBkMode(hdcMem,TRANSPARENT); bmp = CreateCompatibleBitmap(hdc,width,height); SelectObject(hdcMem,bmp); if (hBackPic != NULL) { SelectObject(hdcBmp,hBackPic); } BitBlt(hdcMem,0,0,width,height,hdcBmp,0,0,SRCCOPY); BitBlt(hdc,0,0,width,height,hdcMem,0,0,SRCCOPY); DeleteDC(hdcBmp); DeleteDC(hdcMem); DeleteObject(bmp); return TRUE; }
BOOL CWinButton::SetBackPic(HBITMAP hBitmap, BOOL bReSize) { BITMAP bm; RECT rt; hBackPic = hBitmap; if (hBitmap != NULL) { if(bReSize) { GetObject(hBackPic,sizeof(bm),&bm); SetWindowPos(m_hWnd,0,0,0,bm.bmWidth,bm.bmHeight,SWP_NOMOVE|SWP_NOREPOSITION); } GetClientRect(m_hWnd,&rt); InvalidateRect(m_hWnd,&rt,TRUE); return TRUE; } return FALSE; }
|