It seems that you want to change the bitmap of a button instead of a menu item. You should first specify BS_BITMAP
(or BS_ICON
) when creating the button, and then send the bitmap to the button handle through the BM_SETIMAGE
message, like:
case ID_MENUBAR_MAX:
{
WINDOWPLACEMENT wp;
wp.length = sizeof(WINDOWPLACEMENT);
HWND hMaxButton = (HWND)lParam;
if (wp.showCmd == SW_SHOWNORMAL)
{
SendMessage(hMaxButton, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hMBB.restore);
}
if (wp.showCmd == SW_SHOWMAXIMIZED)
{
SendMessage(hMaxButton, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hMBB.max);
}
//DrawMenuBar(hwnd);
return 0;
}
EDIT:
I have create an owner-draw menu:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_RBUTTONDOWN:
OnRightClick(hWnd);
return TRUE;
case WM_MEASUREITEM:
OnMeasureItem(hWnd, (LPMEASUREITEMSTRUCT)lParam);
return TRUE;
case WM_DRAWITEM:
OnDrawItem(hWnd, (LPDRAWITEMSTRUCT)lParam);
return TRUE;
....
}
}
BOOL WINAPI OnRightClick(HWND hwnd)
{
HMENU hmenuPopup = CreatePopupMenu();
MENUITEMINFO mii = { 0 };
// Add the bitmap menu items to the menu.
MYITEM* pMyItem = (MYITEM*)malloc(sizeof(MYITEM));
//hBitmap1&hBitmap2 have been initialized before
pMyItem->hBitmap1 = hBitmap1;
pMyItem->hBitmap2 = hBitmap2;
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_FTYPE | MIIM_DATA;
mii.fType = MFT_OWNERDRAW;
mii.dwItemData = (ULONG_PTR)pMyItem;
InsertMenuItem(hmenuPopup, 0, false, &mii);
RECT rc;
GetWindowRect(hwnd,&rc);
TrackPopupMenuEx(hmenuPopup,0,rc.left + 100,rc.top + 100,hwnd,0);
free(pMyItem);
return TRUE;
}
VOID WINAPI OnMeasureItem(HWND hwnd, LPMEASUREITEMSTRUCT lpmis)
{
//lpmis->CtlType = ODT_MENU;
lpmis->itemWidth = 40;
lpmis->itemHeight = 40;
}
VOID WINAPI OnDrawItem(HWND hwnd, LPDRAWITEMSTRUCT lpdis)
{
MYITEM* pMyItem = (MYITEM*)lpdis->itemData;
HBITMAP hBitmap;
static int flag = 0;
if (lpdis->itemState & ODS_SELECTED)
{
flag = !flag;
}
if(flag)
hBitmap = pMyItem->hBitmap1;
else
hBitmap = pMyItem->hBitmap2;
HDC hdcMem = CreateCompatibleDC(lpdis->hDC);
HBITMAP oldBitmap = (HBITMAP)SelectObject(hdcMem, hBitmap);
BITMAP bitmap;
GetObject(hBitmap, sizeof(bitmap), &bitmap);
BitBlt(lpdis->hDC, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oldBitmap);
DeleteDC(hdcMem);
}
When the mouse selects the menu, the bitmap of the menu will change.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…