Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi,

I tried to implement the default MFC-Icon in an Menu. Got it, but unfortunatly, the background of the icon is NOT transparent. why not and how can I set it to transparent?

my source code:
CMenu* mmenu = GetMenu();  
 
HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
mmenu->SetMenuItemBitmaps(ID_TEST, MF_BYCOMMAND, ConvertIconToBitmap(hIcon), NULL);

the converting function:
C++
CBitmap* CTestDlg::ConvertIconToBitmap(HICON hIcon)
{
    CDC dc;
    CBitmap bmp;
    CClientDC ClientDC(this);
    dc.CreateCompatibleDC(&ClientDC);
    bmp.CreateCompatibleBitmap(&ClientDC, 16, 16);
    CBitmap* pOldBmp = (CBitmap*)dc.SelectObject(&bmp);
    ::DrawIconEx(dc.GetSafeHdc(), 0, 0, hIcon, 16, 16, 0, (HBRUSH)RGB(255, 255, 255), DI_NORMAL);
    dc.SelectObject(pOldBmp);
    dc.DeleteDC();
    HBITMAP hBitmap = (HBITMAP)::CopyImage((HANDLE)((HBITMAP)bmp), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE);
 
    return CBitmap::FromHandle(hBitmap);
}


I tried to change the HBRUSH within the DrawIconEx, but the background changes only to black when RGB is (0,0,0). All other combinations paint the background white :(
Posted
Comments
Richard MacCutchan 9-Dec-14 4:55am    
I think you need to set it in the icon editor.
Member 10730024 9-Dec-14 6:19am    
I'm sorry I dont understand what u mean
Richard MacCutchan 9-Dec-14 6:25am    
Try using the icon editor in Visual Studio to change the background.
Member 10730024 9-Dec-14 6:30am    
I've load the default MFC-Icon which appears also in the guis headline. the background is demonstrably transparent, but for some reason the background of the same icon in the submenu is white...
Richard MacCutchan 9-Dec-14 6:43am    
That is because of the white brush you are using to paint the background. I am not sure if it will help, but take a look at http://msdn.microsoft.com/en-us/library/dd162965(v=vs.85).aspx.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900