0

I'm on Windows 7, and i am trying to display an icon with transparency on my contextual menu but it doesn't work.

I am trying to use LoadImage like this :

    m_hMenuBmp = (HBITMAP)::LoadImage(g_hInst, L"C:\\Users\\nicolas\\AppData\\Roaming\\MyApp\\icon.bmp", IMAGE_BITMAP, 16, 16, LR_LOADFROMFILE | LR_LOADTRANSPARENT );

and my icon.bmp is set to 256 colors with white ( 255, 255, 255 ) on background ... I don't know why this isn't working ...

I tried the ARGB Method of Raymon Chen but it didn't work neither !

int cx = GetSystemMetrics(SM_CXSMICON);
int cy = GetSystemMetrics(SM_CYSMICON);


BITMAPINFO bmi = {0};
bmi.bmiHeader.biSize =sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = cx;
bmi.bmiHeader.biHeight = cy;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;

DWORD *pBits;
m_hMenuBmp = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void **)&pBits, NULL , 0);

if (m_hMenuBmp)
{
    for (int y = 0; y < cy ; y++ )
    {
        for (int x = 0; x < cx; x++)
        {
            BYTE bAlpha = x * x * 255 / cx / cx;
            DWORD dv = (bAlpha << 24) | (bAlpha << 16) | bAlpha ;
            pBits[y *cx + x] - dv;
        }
    }
}

And I don't know why ... my icon isn't displayed with this method ..

kavaliero
  • 389
  • 1
  • 4
  • 22
  • 2
    `LR_LOADTRANSPARENT` and `LR_LOADMAP3DCOLORS` [do not actually load transparent bitmaps](http://blogs.msdn.com/b/oldnewthing/archive/2006/11/15/1081320.aspx). They merely simulate transparency by mapping the background color. For true transparency, you need either an icon (which has a mask), or an ARGB bitmap (which has an alpha channel). Example code is available in [this talk from 2008](http://channel9.msdn.com/Blogs/pdc2008/PC43). – Raymond Chen May 15 '12 at 13:04
  • I created an ARGB bitmap and try to load it with LoadImage but it didn't work. And my icon wasn't displayed . have you got an example code ? because the talk is very long – kavaliero May 15 '12 at 15:05
  • 1
    Skip to timecode 37:24 for discussion and 39:30 for code. Note in particular that it must be a 32bpp DIB section. – Raymond Chen May 15 '12 at 16:29
  • I tried but it didn't work i don't saw the pink icon . And if I want to load a file ? how can i do this with DibSection ??? – kavaliero May 16 '12 at 08:29

2 Answers2

3

I found a way to did this easily :

 HICON hIcon = (HICON)LoadImage( NULL,  L"icon.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE );

HDC hDC = ::GetDC( NULL );
m_hMenuBmp = ::CreateCompatibleBitmap( hDC, 16, 16 );

HDC hDCTemp = ::CreateCompatibleDC( hDC );
::ReleaseDC( NULL, hDC );

HBITMAP hBitmapOld = ( HBITMAP ) ::SelectObject( hDCTemp, m_hMenuBmp );
::DrawIconEx( hDCTemp, 0, 0, hIcon, 16, 16, 0, ::GetSysColorBrush( COLOR_MENU ), DI_NORMAL );

::SelectObject( hDCTemp, hBitmapOld );
::DeleteDC( hDCTemp );
kavaliero
  • 389
  • 1
  • 4
  • 22
  • Well, that doesn't make a lot of sense. You are now using a real icon instead of a bitmap. So not a lot of point in turning the icon back into a bitmap. – Hans Passant May 16 '12 at 10:04
  • I have my HBITMAP load with the HICON. This allows me to have transparency in my HBITMAP . – kavaliero May 16 '12 at 12:20
1

I was able to get this to work:

HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL, "C:\\moo\\res\\bitmap1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS);
m_pic.SetBitmap(hBitmap);

The trick was LR_LOADMAP3DCOLORS together with LR_LOADTRANSPARENT. This was for a dialog box, by the way. Without LR_LOADMAP3DCOLORS, my white background stayed white.

Darrell
  • 265
  • 2
  • 4
  • 13