在编写对话框程序时,希望能在按钮控件里显示一个小插图,如图所示
最初使用的方法是使用 BM_SETIMAGE 消息,但却不能达到满意的效果,在网上查了一通资料,最后终于实现了,现在把实现的方法记下来。
首先来到MSDN上,查得如下资料
BM_SETIMAGE message
Associates a new image (icon or bitmap) with the button.
Parameters
wParam
The type of image to associate with the button. This parameter can be one of the following values:
- IMAGE_BITMAP
- IMAGE_ICON
lParam
A handle (HICON or HBITMAP) to the image to associate with the button.
Return value
不重要,略
Remarks
The appearance of text, an icon, or both on a button control depends on the BS_ICON and BS_BITMAP styles, and whether the BM_SETIMAGE message is called. The possible results are as follows:
BS_ICON or BS_BITMAP Set? | BM_SETIMAGE Called? | Result |
---|---|---|
Yes | Yes | Show icon only. |
No | Yes | Show icon and text. |
Yes | No | Show text only. |
No | No | Show text only |
既然希望同时显示图片和文字,那么就应该采取上表中的第二行设置(蓝色)。
故控件属性设置为:
然后在 OnInitDialog() 函数中添加如下语句:
SendDlgItemMessage(hwnd,IDC_BUTTON1,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM)LoadBitmap(hIn,MAKEINTRESOURCE(IDB_BITMAP1)));
但是效果却是这样的:
如果把控件属性中的Bitmap项改为 True ,效果又成了这个样子:
真的很让人抓狂。在一通搜索后,发现解决方案就在MSDN中。在BM_SETIMAGE词条的下面,有一条老外的评论,是这样的:
Community Additions
Icon with text solution
I had the same problem for the icon + text in the button and I solved using the version 6 of the common controls.
You can make this by adding the library (if I understand it, I'm not sure).
However I've added it with the preprocessor directive:
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='X86' publicKeyToken='6595b64144ccf1df' language='*'\"")
"EDIT:" This preprocessor directive go on a single line, I don't know why It force me to put a line break after "#pragma"
Linkinf94
1/14/2013
也就是说,使用6.0.0.0版本的 Common-Controls 就好了,所以在程序里加上这么一句话:
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='X86' publicKeyToken='6595b64144ccf1df' language='*'\"")
就万事OK了(64位平台,改为AMD64)
关于 Common-Controls 还可以参考:
http://blog.csdn.net/thirdprince/article/details/6336010