【转】Win32 SDK 实现带图片的 Button 控件BM_SETIMAGE(Disable时图片不显示)
在編寫對話框程序時,希望能在按鈕控件里顯示一個小插圖,如圖所示
最初使用的方法是使用 BM_SETIMAGE 消息,但卻不能達到滿意的效果,在網(wǎng)上查了一通資料,最后終于實現(xiàn)了,現(xiàn)在把實現(xiàn)的方法記下來。
首先來到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:
?
| Yes | Yes | Show icon only. | 
| No | Yes | Show icon and text. | 
| Yes | No | Show text only. | 
| No | No | Show text only | 
?
既然希望同時顯示圖片和文字,那么就應該采取上表中的第二行設置(藍色)。
故控件屬性設置為:
?
然后在 OnInitDialog() 函數(shù)中添加如下語句:
?
SendDlgItemMessage(hwnd,IDC_BUTTON1,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM)LoadBitmap(hIn,MAKEINTRESOURCE(IDB_BITMAP1)));?
但是效果卻是這樣的:
如果把控件屬性中的Bitmap項改為 True ,效果又成了這個樣子:
真的很讓人抓狂。在一通搜索后,發(fā)現(xiàn)解決方案就在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
總結
以上是生活随笔為你收集整理的【转】Win32 SDK 实现带图片的 Button 控件BM_SETIMAGE(Disable时图片不显示)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 高通最强Soc骁龙8+跑分曝光:多核成绩
 - 下一篇: 公积金封存满6个月当天就能取?