//可以参考一下CBitmapButton类。void CBitmapButton::DrawItem(LPDRAWITEMSTRUCT lpDIS) { ASSERT(lpDIS != NULL); // must have at least the first bitmap loaded before calling DrawItem ASSERT(m_bitmap.m_hObject != NULL); // required // use the main bitmap for up, the selected bitmap for down CBitmap* pBitmap = &m_bitmap; UINT state = lpDIS->itemState; if ((state & ODS_SELECTED) && m_bitmapSel.m_hObject != NULL) pBitmap = &m_bitmapSel; else if ((state & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL) pBitmap = &m_bitmapFocus; // third image for focused else if ((state & ODS_DISABLED) && m_bitmapDisabled.m_hObject != NULL) pBitmap = &m_bitmapDisabled; // last image for disabled // draw the whole button CDC* pDC = CDC::FromHandle(lpDIS->hDC); CDC memDC; memDC.CreateCompatibleDC(pDC); CBitmap* pOld = memDC.SelectObject(pBitmap); if (pOld == NULL) return; // destructors will clean up CRect rect; rect.CopyRect(&lpDIS->rcItem); pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY); memDC.SelectObject(pOld); }
很简单
上面是引用别人的 下面是我自己写的
http://blog.cfan.com.cn/html/57/28957-152469.html
我用得MFC
太激动了 呵呵
{
ASSERT(lpDIS != NULL);
// must have at least the first bitmap loaded before calling DrawItem
ASSERT(m_bitmap.m_hObject != NULL); // required // use the main bitmap for up, the selected bitmap for down
CBitmap* pBitmap = &m_bitmap;
UINT state = lpDIS->itemState;
if ((state & ODS_SELECTED) && m_bitmapSel.m_hObject != NULL)
pBitmap = &m_bitmapSel;
else if ((state & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL)
pBitmap = &m_bitmapFocus; // third image for focused
else if ((state & ODS_DISABLED) && m_bitmapDisabled.m_hObject != NULL)
pBitmap = &m_bitmapDisabled; // last image for disabled // draw the whole button
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap* pOld = memDC.SelectObject(pBitmap);
if (pOld == NULL)
return; // destructors will clean up CRect rect;
rect.CopyRect(&lpDIS->rcItem);
pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
&memDC, 0, 0, SRCCOPY);
memDC.SelectObject(pOld);
}
如果不用MFC,可以用下WTL挺好的
#include<windows.h>
#include<stdio.h>
#include"resource.h"
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
bool DrawButton(HBITMAP hBitmap,HWND hButton)
{
PAINTSTRUCT ps;
HDC hDC,hMemDC;
hDC=BeginPaint(hButton,&ps);
hMemDC=CreateCompatibleDC(hDC);
SelectObject(hMemDC,hBitmap);
BitBlt(hDC,0,0,100,50,hMemDC,0,0,SRCCOPY);
EndPaint(hButton,&ps);
return true;
}
static HWND hButton;
HBITMAP hBmpOn[2] ;
HINSTANCE hInst;LRESULT CALLBACK ButtonProc(HWND hwnd , UINT uMsg, WPARAM wParam, LPARAM lParam);
WNDPROC oldButtonProc;
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
// HICON hicon;
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(hInstance,IDI_APPLICATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="cc";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow("cc","Zoom",WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX &~WS_THICKFRAME&~WS_OVERLAPPED ,0,0,400,600,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while( GetMessage(&msg,NULL,0,0))
{TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{switch (uMsg)
{
case WM_CREATE:
hButton=CreateWindow("button","cc",
WS_CHILD|WS_VISIBLE|BS_BITMAP|BS_OWNERDRAW,
150,200,100,50,hwnd,(HMENU)button_id,NULL,NULL);
hBmpOn[0]=LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_BITMAP1));
hBmpOn[1]=LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_BITMAP2));
//SendMessage(hButton,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hBmpOn);
oldButtonProc = (WNDPROC)SetWindowLong(hButton,GWL_WNDPROC,(LONG)ButtonProc);
break;
case WM_CLOSE:
if (IDYES==MessageBox(hwnd,"是否真的退出?","cc",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}LRESULT CALLBACK ButtonProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
static HBITMAP hBitmap=hBmpOn[0];
static BOOL bIsOnButton = true;
switch(uMsg)
{
case WM_PAINT:
DrawButton(hBitmap,hWnd);
break;
case WM_MOUSEMOVE:
if(bIsOnButton)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER|TME_LEAVE;
tme.hwndTrack = hWnd;
tme.dwHoverTime = 1;
TrackMouseEvent(&tme);
}
break;
case WM_MOUSEHOVER:
hBitmap=hBmpOn[1];
InvalidateRect(hWnd, NULL,FALSE);
bIsOnButton = false;
break;
case WM_MOUSELEAVE:
hBitmap=hBmpOn[0];
InvalidateRect(hWnd, NULL,FALSE);
//MessageBox(0,0,0,0);
bIsOnButton = true;
break;
}
return CallWindowProc(oldButtonProc,hWnd,uMsg,wParam,lParam);
}为什么没有实现鼠标移动在按钮上图片变化的效果