MFC中为按钮添加图片,图标

1,在dlg中添加button,并设置按钮属性如下图:
MFC中为按钮添加图片,图标_第1张图片

 

2,完成之后,进入classwizard 添加button的成员变量:

MFC中为按钮添加图片,图标_第2张图片

3进入::OnInitDialog()函数中添加代码:

 HICON m_hicn1=AfxGetApp()->LoadIcon(IDI_ICON1);  
 m_button1.SetIcon(m_hicn1);

完成,运行即可。

 

我在csdn中看到了其他认得例子,如http://download.csdn.net/detail/yantao8612/3398429(vc6.0)和

http://download.csdn.net/detail/yangzhenglun/2452217(vc++ 2005)

这两个例子,跟我上面提到的不是一个原理,他们在里面新建一个button类的派生类,新类里添加了很多成员函数,这两个例子的效果更好(我上面写的方法,按钮上只能是图片,不能添加文字)。

      我照他们做的却遇到了错误,在为button添加成员变量时,编译出错。错误如下:

c:\users\administrator\documents\vc 6.0\pic2\pic2dlg.h(27) : error C2146: syntax error : missing ';' before identifier 'm_button1'
c:\users\administrator\documents\vc 6.0\pic2\pic2dlg.h(27) : error C2501: 'ImageButton' : missing storage-class or type specifiers
c:\users\administrator\documents\vc 6.0\pic2\pic2dlg.h(27) : error C2501: 'm_button1' : missing storage-class or type specifiers

一直以为是button类的派生类的问题,今天细致一看,恍然大悟,我把派生类的头文件添加在了dlg.cpp文件里了,而不是放在dlg.h头文件中,试想在dlg.h中声明派生类的对象而不包含它的头文件,当然报错啦!(一件简单的c++问题)

 

呜呼,哀哉!这么一个问题折磨了我好几天!

另外:http://hi.baidu.com/lechie/blog/item/008c103b868cf5e815cecb37.html中写的很不错。我在上面是照着他第一个方法写的。

 

 

 

你可能感兴趣的:(c,mfc,button)