문제

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 ..

도움이 되었습니까?

해결책

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 );

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top