【转】Win32 SDK 实现带图片的 Button 控件BM_SETIMAGE(Disable时图片不显示)

在编写对话框程序时,希望能在按钮控件里显示一个小插图,如图所示

最初使用的方法是使用 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
YesYesShow icon only.
NoYesShow icon and text.
YesNoShow text only.
NoNoShow 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值