C++ 将图标加载到按钮上
C++ Loading an Icon on to a Button
我正在尝试将图标加载到我的按钮上。我正在使用 Code::Blocks。我真的
不关心图像是 BITMAP (.bmp) 还是 ICON (.ico)。下面是案例 WM_CREATE.
的代码
case WM_CREATE:
HWND button;
button = CreateWindow("BUTTON", "My Button", WS_VISIBLE | WS_CHILD,
225, 225, 100, 25, hwnd, NULL, NULL, NULL);
break;
是否可以在不复杂的情况下向按钮添加图标?感谢您的宝贵时间:)
您可以像这样向您的按钮发送 BM_SETIMAGE
消息:
case WM_CREATE:
// load your bitmap from the executable, you should also be able
// to load the bitmap / or icon from disk using `LoadImage`
HBITMAP hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE("<ResourceID>"));
// notice the BS_BITMAP flag (you could also use `BS_ICON`)
// perhaps you also would like to add BS_PUSHBUTTTON style
HWND button = CreateWindow("BUTTON",
"My Button",
WS_VISIBLE | WS_CHILD | BS_BITMAP,
225, 225, 100, 25,
hwnd,
NULL,
hInstance,
NULL);
// set the bitmap of your window, you could use `BM_SETICON` in
// case you're using an icon rather than a bitmap
SendMessage(button, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);
break;
要从磁盘上的文件加载位图,您可以这样做:
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,
"NameOfBitmap.bmp",
IMAGE_BITMAP,
0, 0,
LR_LOADFROMFILE);
参考资料
Button Messages
LoadBitmap()
LoadIcon()
LoadImage()
Button Styles
我正在尝试将图标加载到我的按钮上。我正在使用 Code::Blocks。我真的 不关心图像是 BITMAP (.bmp) 还是 ICON (.ico)。下面是案例 WM_CREATE.
的代码case WM_CREATE:
HWND button;
button = CreateWindow("BUTTON", "My Button", WS_VISIBLE | WS_CHILD,
225, 225, 100, 25, hwnd, NULL, NULL, NULL);
break;
是否可以在不复杂的情况下向按钮添加图标?感谢您的宝贵时间:)
您可以像这样向您的按钮发送 BM_SETIMAGE
消息:
case WM_CREATE:
// load your bitmap from the executable, you should also be able
// to load the bitmap / or icon from disk using `LoadImage`
HBITMAP hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE("<ResourceID>"));
// notice the BS_BITMAP flag (you could also use `BS_ICON`)
// perhaps you also would like to add BS_PUSHBUTTTON style
HWND button = CreateWindow("BUTTON",
"My Button",
WS_VISIBLE | WS_CHILD | BS_BITMAP,
225, 225, 100, 25,
hwnd,
NULL,
hInstance,
NULL);
// set the bitmap of your window, you could use `BM_SETICON` in
// case you're using an icon rather than a bitmap
SendMessage(button, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);
break;
要从磁盘上的文件加载位图,您可以这样做:
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,
"NameOfBitmap.bmp",
IMAGE_BITMAP,
0, 0,
LR_LOADFROMFILE);
参考资料
Button Messages
LoadBitmap()
LoadIcon()
LoadImage()
Button Styles