我正在尝试将 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".
我应该怎么做,或者我做错了什么?
正如乔纳森·波特(Jonathan Potter)在评论中指出的那样,STM_SETIMAGE
需要HBITMAP
坚持下去。 清理语义有些复杂,但您通常不能在WM_DESTROY
之前调用DeleteObject
。