Win32 中菜单项使用bitmap 为什么显示确是黑色块

Win32 中菜单项使用bitmap 为什么显示确是黑色块

###代码如下:

#include<windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void AddHelpToSys(HINSTANCE ,HWND);
HBITMAP StretchBitmap(HBITMAP);
HMENU CreateMyMenu(HINSTANCE);
HBITMAP GetBitmapFont(int);

int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
    static TCHAR szAppName[]= TEXT("GrafMenu--demo");
    MSG msg;
    HWND hwnd;
    WNDCLASS wndclass;

    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hInstance = hInstance;
    wndclass.lpfnWndProc = WndProc;
    wndclass.lpszClassName = szAppName;
    wndclass.lpszMenuName = NULL;
    wndclass.style = CS_VREDRAW | CS_HREDRAW;

    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("registerclass fail!"), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd, nShowCmd);
    UpdateWindow(hwnd);

    while (GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HINSTANCE hinst;
    static int iCurrentFont= ID_FONT_COUR;
    HMENU hMenu;
    switch (message)
    {
    case WM_CREATE:

        AddHelpToSys(((LPCREATESTRUCT)lParam)->hInstance, hwnd);
        hMenu = CreateMyMenu(((LPCREATESTRUCT)lParam)->hInstance);

        SetMenu(hwnd, hMenu);

        return 0;
    case WM_CLOSE:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd, message, wParam, lParam);
}

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函数中,加载看起来都正常,但是运行起来,都是黑色块状......

如图

查看全部
xiongguang1111111
木兮₀₃₂₁
2019/04/21 23:28
  • c++
  • 点赞
  • 收藏
  • 回答
    私信

0个回复