MFC中,实现往按钮中添加位图
对话框宣称一个成员变量
CBitmapButton m_BmpBtn;
m_BmpBtn.AutoLoad(*** , ***);
首先,我们创建一个基于对话框的应用程序CmyDialog ;
Ι.MFC的CBitmapButton类,这也是最简单的功能最强的位图按钮。我们可以采取如下的步骤:
1.为按钮指定唯一的按钮标题(此例子为OK按钮,这里设置按钮标题为Ensure)并选中Ownerdraw属性,然后在项目中加一些位图资源,并用名字标示这些资源而不要用数字ID,其ID分别为"EnsureU"、"EnsureD"、"EnsureF"、"EnsureX"(一定要加双引号),分别对应于按钮的“松开(Up)”、“按下 (Down)”、“获得输入焦点(focused)”和“禁止(Disable)”状态。
2. 我们还要在对话框类中加入CBitmapButton m_BmpBtn数据成员。
3. 在OnInitDialog()为这个成员函数中调用:(添加WM_INITDIALOG消息处理函数)
(注意不可以在构造函数中调用,否则会出错.)
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_BmpBtn.AutoLoad(IDOK,this);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
点击编译按钮,成功后运行程序
/*如果以上方法不行请检查你的BITMAP 资源,APPSTUDIO中,"EnsureU"和 "EnsureD" 等的资源名称都是需要用引号引起来的, AutoLoad不成功,很可能就是由此产生的。 */
下面是MSDN的英文资料
Use the CBitmapButton class to create pushbutton controls labeled with bitmapped images instead of text. CBitmapButton objects contain up to four bitmaps, which contain images for the different states a button can assume: up (or normal), down (or selected), focused, and disabled. Only the first bitmap is required; the others are optional.
Bitmap-button images include the border around the image as well as the image itself. The border typically plays a part in showing the state of the button. For example, the bitmap for the focused state usually is like the one for the up state but with a dashed rectangle inset from the border or a thick solid line at the border. The bitmap for the disabled state usually resembles the one for the up state but has lower contrast (like a dimmed or grayed menu selection).
These bitmaps can be of any size, but all are treated as if they were the same size as the bitmap for the up state.
Various applications demand different combinations of bitmap images:
Up | Down | Focused | Disabled | Application |
× | Bitmap | |||
× | × | Button without WS_TABSTOP style | ||
× | × | × | × | Dialog button with all states |
× | × | × | Dialog button with WS_TABSTOP style |
When creating a bitmap-button control, set the BS_OWNERDRAW style to specify that the button is owner-drawn. This causes Windows to send the WM_MEASUREITEM and WM_DRAWITEM messages for the button; the framework handles these messages and manages the appearance of the button for you.
To create a bitmap-button control in a window’s client area, follow these steps:
我们使用的方法
To include a bitmap-button control in a dialog box, follow these steps:
找不到类似与 #define EnsureU 1000 的定义.
9. In your application’s dialog class (derived from CDialog), add a CBitmapButton member object.
10.In the CDialog object’s OnInitDialog routine, call the CBitmapButton object’s AutoLoad function, using as parameters the button’s control ID and the CDialog object’s this pointer.
If you want to handle Windows notification messages, such as BN_CLICKED, sent by a bitmap-button control to its parent (usually a class derived from CDialog), add to the CDialog-derived object a message-map entry and message-handler member function for each message. The notifications sent by a CBitmapButton object are the same as those sent by a CButton object.
The class CToolBar takes a different approach to bitmap buttons.
For more information on CBitmapButton, seeControl Topics in Visual C++ Programmer's Guide.
使用图标制作按钮
1. 打开按钮的属性页,在Style中选中Icon 。
2. 在对话框类的头文件中定义成员变量(
注意一定要使用ClassWizard加入这个成员变量)
CButton m_IconBtn;;//对应于图标按钮
3. 创建相应的图标或者位图资源:
图标资源:IDI_ICONBTN
4.在初始化中加入如下代码:
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
HICON Icon=AfxGetApp()->LoadIcon(IDI_ICONBTN);
m_IconBtn.SetIcon(Icon);
m_BmpBtn.AutoLoad(IDOK,this);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
…
重新编译运行我们的程序.OK.