问题描述
我想在“所有者绘制”按钮上绘制程序的图标。这可以是资源文件中的图标,也可以是通用Windows图标。但是,即使经过无休止的搜索,我仍然无法找到相应的代码。我遇到了一些零碎的答案。但是,没有完整的解释。
对不起,我没有要发布的代码。我完全迷失了这一点。标准的Win API或GDI +都可以为我工作。
解决方法
创建按钮时,添加BS_ICON
样式,然后为图标添加手柄,即可使用LoadImage
。
最后将BM_SETIMAGE
带有句柄的消息发送到您的图标。
下面是代码示例:
HWND button1 = CreateWindow(TEXT("Button"),TEXT("OK"),WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_ICON,50,hwnd,(HMENU)1001,hInstance,NULL);
HICON hIcon = (HICON)LoadImage( // returns a HANDLE so we have to cast to HICON
NULL,// hInstance must be NULL when loading from a file
L"iconname.ico",// the icon file name
IMAGE_ICON,// specifies that the file is an icon
0,// width of the image (we'll specify default later on)
0,// height of the image
LR_LOADFROMFILE | // we want to load a file (as opposed to a resource)
LR_DEFAULTSIZE | // default metrics based on the type (IMAGE_ICON,32x32)
LR_SHARED // let the system release the handle when it's no longer used
);
SendMessage(button1,BM_SETIMAGE,IMAGE_ICON,(LPARAM)hIcon);