使用GDI +的PNG按钮

时间:2014-12-27 02:11:17

标签: c++ winapi graphics gdi+

我试图将png加载到我的按钮,但它没有在我的按钮中显示png。

我使用此功能创建我的按钮:

HWND MyControls::CreateButton(LPCTSTR text, int x, int y, int w, int h, HMENU id, HWND hwnd, HINSTANCE hInst, bool png){
        if (png){
            return CreateWindow("BUTTON", text, BS_BITMAP | WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
                x, y, w, h, hwnd, id, hInst, NULL);
        }
        else{
            return CreateWindow("BUTTON", text, WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
                x, y, w, h, hwnd, id, hInst, NULL);
        }   
}    

将PNG添加到按钮(WM_CREATE):

void MyControls::AddPngBtn(HWND hwnd, const WCHAR* fileName){
    Bitmap bmp(fileName);
    HBITMAP tBmp;
    bmp.GetHBITMAP(Color(0, 0, 0, 0), &tBmp);
    SendMessage(hwnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)tBmp);
    ShowWindow(hwnd, SW_SHOW);
    DeleteObject(tBmp);
}    

我如何初始化GDI +:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(szCmdLine);
    UNREFERENCED_PARAMETER(iCmdShow);

    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    if (!Wnd.RegClassEx(hInstance)) return 0;
    if (!Wnd.CreateMyWindow(hInstance)) return 0;

    GdiplusShutdown(gdiplusToken);

    return Wnd.MyMsg();
}    

The LPARAM is not NULL;
My button size: 25x25;
My png size: 24x24;
And i don't have a error in the "Error List".

我该怎么办,或者我做错了什么?

1 个答案:

答案 0 :(得分:2)

正如乔纳森·波特在评论中指出的那样,STM_SETIMAGE需要HBITMAP来坚持下去。清理语义为somewhat convoluted,但您通常不能在DeleteObject之前调用WM_DESTROY