Win32 menu 项使用bitmap为什么显示确实黑色块?


void AddHelpToSys(HINSTANCE hInstance,HWND hwnd)
{
    HMENU hMenu;
    HBITMAP hBitmap;
    hMenu=GetSystemMenu(hwnd, FALSE);
    hBitmap=LoadBitmap(hInstance, TEXT("bighelp"));

    AppendMenu(hMenu, MF_SEPARATOR, 0, 0);

    AppendMenu(hMenu, MF_BITMAP, ID_HELP, (PTSTR)(LONG)hBitmap);
}

HBITMAP StretchBitmap(HBITMAP hBitmap1)
{
    BITMAP bm1,bm2;
    HBITMAP hBitmap2;
    int cxChar, cyChar;
    HDC hdc, hdcMem1,hdcMem2;

    // ´´½¨É豸»·¾³
    hdc = CreateIC(TEXT("DISPLAY"), 0, 0, 0);
    hdcMem1 = CreateCompatibleDC(hdc);
    hdcMem2 = CreateCompatibleDC(hdc);
    DeleteDC(hdc);
    //»ñµÃ¶Ô»°¿ò»ù±¾µ¥Ôª£¬in pixel
    cxChar = LOWORD(GetDialogBaseUnits());
    cyChar = HIWORD(GetDialogBaseUnits());

    //»ñµÃhbitmap1µÄÊý¾Ý
    GetObject(hBitmap1, sizeof(BITMAP), &bm1);
    bm2 = bm1;
    //ÉèÖÃbm2µÄ´óС
    bm2.bmWidth = (cxChar*bm2.bmWidth) / 4;
    bm2.bmHeight=(cyChar*bm2.bmHeight) / 8;
    bm2.bmWidthBytes = ((bm2.bmWidth + 15) / 16) * 2;
    //´´½¨hbitmap2
    hBitmap2=CreateBitmapIndirect(&bm2);
    //¼ÓÔØbitmapµ½É豸»·¾³
    SelectObject(hdcMem1, hBitmap1);
    SelectObject(hdc, hBitmap2);

    StretchBlt(hdcMem2, 0, 0, bm2.bmWidth, bm2.bmHeight, hdcMem1, 0, 0, bm1.bmWidth, bm1.bmHeight, SRCCOPY);
    DeleteObject(hBitmap1);
    DeleteDC(hdcMem1);
    DeleteDC(hdcMem2);
    return hBitmap2;
}

HMENU CreateMyMenu(HINSTANCE hinst)
{
    HBITMAP hBitmap;
    HMENU hMenu, hMenuPopup;
    int i;
    //´´½¨Ò»¸ö¿ÕµÄMenu
    hMenu = CreateMenu();

    hMenuPopup = LoadMenu(hinst, TEXT("MenuFile"));
    hBitmap = StretchBitmap(LoadBitmap(hinst, TEXT("FILE")));
    AppendMenu(hMenu, MF_BITMAP | MF_POPUP, (int)hMenuPopup, (PTSTR)(LONG)hBitmap);
    hMenuPopup = LoadMenu(hinst, TEXT("MenuEdit"));
    hBitmap = StretchBitmap(LoadBitmap(hinst, TEXT("EDIT")));
    AppendMenu(hMenu, MF_POPUP | MF_BITMAP, (int)hMenuPopup, (PTSTR)(LONG)hBitmap);

    hMenuPopup = CreateMenu();
    for (i=0;i<3;i++)
    {
        hBitmap = GetBitmapFont(i);
        AppendMenu(hMenuPopup, MF_BITMAP, ID_FONT_COUR + i, (PTSTR)(LONG)hBitmap);
    }
    hBitmap = StretchBitmap(LoadBitmap(hinst, TEXT("FONT")));
    AppendMenu(hMenu, MF_BITMAP | MF_POPUP, (int)hMenuPopup, (PTSTR)(LONG)hBitmap);
    return hMenu;

}


HBITMAP GetBitmapFont(int i)
{
    static TCHAR *szFaceName[3] = { TEXT("Courier New"),TEXT("Arial"),TEXT("Times New Roman") };

    HBITMAP hBitmap;
    HDC hdc, hdcMem;
    HFONT hFont;
    SIZE size;
    TEXTMETRIC tm;

    hdc = CreateIC(TEXT("DISPLAY"), 0, 0, 0);
    GetTextMetrics(hdc, &tm);

    hdcMem = CreateCompatibleDC(hdc);
    hFont = CreateFont(2 * tm.tmHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, szFaceName[i]);

    hFont = (HFONT)SelectObject(hdcMem, hFont);
    GetTextExtentPoint32(hdcMem, szFaceName[i], lstrlen(szFaceName[i]), &size);
    hBitmap = CreateBitmap(size.cx, size.cy, 1, 1, NULL);
    SelectObject(hdcMem, hBitmap);

    TextOut(hdcMem, 0, 0, szFaceName[i], lstrlen(szFaceName[i]));
    DeleteObject(SelectObject(hdcMem, hFont));
    DeleteDC(hdcMem);
    DeleteDC(hdc);
    return hBitmap;
}






在CreateMyMenu函数中,加载bitmap 最后显示确实黑色块为什么?

c++
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问