How to Insert Bitmap for a MenuItem

Discussion in 'MFC' started by fantasy1212, Jun 26, 2010.

  1. fantasy1212

    fantasy1212 New Member

    Joined:
    Jun 26, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Goal: insert an icon resource to the popup menu, my code is below:
    code1:
    Code:
    HMENU hMenuPopup = CreatePopupMenu();
    if (hMenuPopup)
    {
    	HBITMAP hbmQuit = (HBITMAP)LoadImage(AfxGetResourceHandle(),
    		MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, LR_DEFAULTSIZE);
    
    	MENUITEMINFO mii;
    	ZeroMemory(&mii, sizeof(MENUITEMINFO));
    	mii.cbSize = sizeof(mii);
    	mii.fMask = MIIM_CHECKMARKS|MIIM_STRING|MIIM_ID;
    	mii.wID = MENU_ID;
    	mii.dwTypeData = TEXT("Quit");
    	mii.hbmpChecked = (HBITMAP)hbmQuit;
    	mii.hbmpUnchecked = (HBITMAP)hbmQuit;
    	InsertMenuItem(hMenuPopup, MENU_ID, FALSE, &mii);
    
    	POINT ptCursor;
    	GetCursorPos(&ptCursor);
    	TrackPopupMenu(hMenuPopup, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
    		ptCursor.x, ptCursor.y, 0, GetSafeHwnd(), 0);
    	//::DeleteObject(hbmQuit);
    	[color=red]//---->I search the forum questions, someone said, should not delete the bitmap, should I?[/color]
    }
    
    The code1 neither shows bitmap nor the text. Can you explain where am I wrong?

    Then I replace 'InsertMenuItem' to 'AppendMenu' and 'SetMenuItemBitmaps', it shows the text but still no bitmap.
    code2:
    Code:
    	AppendMenu(hMenuPopup, MF_STRING, MENU_ID,
    		TEXT("Quit"));
    	SetMenuItemBitmaps(hMenuPopup, MENU_ID, MF_BYCOMMAND,
    		hbmQuit, hbmQuit);
    
    Please first explain why my code1 and code2 not working(All I searched answers say it should work), Then how can I show bitmap in the menu correctly!

    Thanks in advance!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice