按钮窗口(控件)在MFC中使用CButton表示,CButton包含了三种样式的按钮,Push Button,Check Box,Radio Box。所以在利用CButton对象生成按钮窗口时需要指明按钮的风格。
创建按钮:BOOL CButton::Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );其中lpszCaption是按钮上显示的文字,dwStyle为按钮风格,除了Windows风格能够使用外(如WS_CHILD|WS_VISUBLE|WS_BORDER)更有按钮专用的一些风格。
BS_AUTOCHECKBOX 检查框,按钮的状态会自动改变 Same as a check box, except that a check mark appears in the check box when the user selects the box; the check mark disappears the next time the user selects the box.
BS_AUTORADIOBUTTON 圆形选择按钮,按钮的状态会自动改变 Same as a radio button, except that when the user selects it, the button automatically highlights itself and removes the selection from any other radio buttons with the same style in the same group.
BS_AUTO3STATE 允许按钮有三种状态即:选中,未选中,未定 Same as a three-state check box, except that the box changes its state when the user selects it.
BS_CHECKBOX 检查框 Creates a small square that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style).
BS_DEFPUSHBUTTON 默认普通按钮 Creates a button that has a heavy black border. The user can select this button by pressing the ENTER key. This style enables the user to quickly select the most likely option (the default option).
BS_LEFTTEXT 左对齐文字 When combined with a radio-button or check-box style, the text appears on the left side of the radio button or check box.
BS_OWNERDRAW 自绘按钮 Creates an owner-drawn button. The framework calls the DrawItem member function when a visual aspect of the button has changed. This style must be set when using the CBitmapButton class.
BS_PUSHBUTTON 普通按钮 Creates a pushbutton that posts a WM_COMMAND message to the owner window when the user selects the button.
BS_RADIOBUTTON 圆形选择按钮 Creates a small circle that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style). Radio buttons are usually used in groups of related but mutually exclusive choices.
BS_3STATE 允许按钮有三种状态即:选中,未选中,未定 Same as a check box, except that the box can be dimmed as well as checked. The dimmed state typically is used to show that a check box has been disabled.
rect为窗口所占据的矩形区域,pParentWnd为父窗口指针,nID为该窗口的ID值。
获取/改变按钮状态:对于检查按钮和圆形按钮可能有两种状态,选中和未选中,假如配置了BS_3STATE或BS_AUTO3STATE风格就可能出现第三种状态:未定,这时按钮显示灰色。通过调用int CButton::GetCheck( ) 得到当前是否被选中,返回0:未选中,1:选中,2:未定。调用void CButton::SetCheck( int nCheck );配置当前选中状态。
处理按钮消息:要处理按钮消息需要在父窗口中进行消息映射,映射宏为ON_BN_CLICKED( id, memberFxn )id为按钮的ID值,就是创建时指定的nID值。处理函数原型为afx_msg void memberFxn( );
按钮窗口(控件)在MFC中使用CButton表示,CButton包含了三种样式的按钮,Push Button,Check Box,Radio Box。所以在利用CButton对象生成按钮窗口时需要指明按钮的风格。
创建按钮:BOOL CButton::Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );其中lpszCaption是按钮上显示的文字,dwStyle为按钮风格,除了Windows风格能够使用外(如WS_CHILD|WS_VISUBLE|WS_BORDER)更有按钮专用的一些风格。
BS_AUTOCHECKBOX 检查框,按钮的状态会自动改变 Same as a check box, except that a check mark appears in the check box when the user selects the box; the check mark disappears the next time the user selects the box.
BS_AUTORADIOBUTTON 圆形选择按钮,按钮的状态会自动改变 Same as a radio button, except that when the user selects it, the button automatically highlights itself and removes the selection from any other radio buttons with the same style in the same group.
BS_AUTO3STATE 允许按钮有三种状态即:选中,未选中,未定 Same as a three-state check box, except that the box changes its state when the user selects it.
BS_CHECKBOX 检查框 Creates a small square that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style).
BS_DEFPUSHBUTTON 默认普通按钮 Creates a button that has a heavy black border. The user can select this button by pressing the ENTER key. This style enables the user to quickly select the most likely option (the default option).
BS_LEFTTEXT 左对齐文字 When combined with a radio-button or check-box style, the text appears on the left side of the radio button or check box.
BS_OWNERDRAW 自绘按钮 Creates an owner-drawn button. The framework calls the DrawItem member function when a visual aspect of the button has changed. This style must be set when using the CBitmapButton class.
BS_PUSHBUTTON 普通按钮 Creates a pushbutton that posts a WM_COMMAND message to the owner window when the user selects the button.
BS_RADIOBUTTON 圆形选择按钮 Creates a small circle that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style). Radio buttons are usually used in groups of related but mutually exclusive choices.
BS_3STATE 允许按钮有三种状态即:选中,未选中,未定 Same as a check box, except that the box can be dimmed as well as checked. The dimmed state typically is used to show that a check box has been disabled.
rect为窗口所占据的矩形区域,pParentWnd为父窗口指针,nID为该窗口的ID值。
获取/改变按钮状态:对于检查按钮和圆形按钮可能有两种状态,选中和未选中,假如配置了BS_3STATE或BS_AUTO3STATE风格就可能出现第三种状态:未定,这时按钮显示灰色。通过调用int CButton::GetCheck( ) 得到当前是否被选中,返回0:未选中,1:选中,2:未定。调用void CButton::SetCheck( int nCheck );配置当前选中状态。
处理按钮消息:要处理按钮消息需要在父窗口中进行消息映射,映射宏为ON_BN_CLICKED( id, memberFxn )id为按钮的ID值,就是创建时指定的nID值。处理函数原型为afx_msg void memberFxn( );
在C#.net中确实是一件很容易的和事情,可是我还不知道如何在VC中实现呢!
在MFC中,倒是比较好实现
方案1:
宣称一个成员变量
CBitmapButton cbBtn1;
两种方法使用
1。cbBtn1.AutoLoad(xxx,xxx,xxx,xxx);
2. cbBtn1.SubclassDlgItem(IDC_BUTTON_1,this);
cbBtn1.LoadBitmaps(BUTTON_LED17_UP,BUTTON_LED17_DOWN,NULL,NULL);
cbBtn1.SizeToContent();
方案2:
一个例子
首先,创建一个基于对话框的应用程序CmyDialog ;
Ι.MFC的CBitmapButton类,这也是最简单的功能最强的位图按钮。我们可以采取如下的步骤:
1.为按钮指定唯一的按钮标题(此例子为OK按钮,这里设置按钮标题为OK)并选中Ownerdraw属性,然后在项目中加一些位图资源,并用名字标示这些资源而不要用数字ID,其ID分别为”OKU”、”OKD”、”OKF”、”OKX”(一定要加双引号),分别对应于按钮的“松开(Up)”、“按下 (Down)”、“获得输入焦点(focused)”和“禁止(Disable)”状态。
2. 我们还要在对话框类中加入CBitmapButton m_aBmpBtn;数据成员。
3. 在初始化中为这个成员调用:
…
m_aBmpBtn. AutoLoad(IDOK,this);
…
点击编译按钮,成功后运行程序,哈哈,看看效果,我们的位图按钮已经建立了。
/*如果以上方法不行请检查你的BITMAP 资源,APPSTUDIO中,"OKU"和 "OKD" 等的资源名称都是需要用引号引起来的, AutoLoad不成功,很可能就是由此产生的。 */
改变CANCLE按钮的标题,可以设置其标题为ICON或者BITMAP :(这里我们演示了bitmap的用法,Icon按钮读者可以按照下面的代码处理)
Ⅱ.使用图标制作按钮
1. 打开ICON按钮的属性页,在Style中选中Icon 。
2. 在对话框类的头文件中定义成员变量(使用ClassWizard加入这个成员变量)
CButton m_ IconBtn;//对应于图标按钮
3. 创建相应的图标或者位图资源:
图标资源:IDI_ICONBUTTON
4.在初始化中加入如下代码:
…
//对应于图标按钮
HICON hIcon=AfxGetApp()->LoadIcon(IDI_ ICONBUTTON);
m_IconBtn.SetIcon(hIcon);
…
重新编译运行我们的程序,奇妙的图像按钮呈现在我们的眼前了。
Ⅲ.使用位图制作按钮
1. 打开BITMAP按钮的属性页,在Style中选中Bitmap。
2. 对话框类的头文件中定义成员变量(使用ClassWizard加入这个成员变量)
CButton m_IconBtn;
3.创建位图资源:
位图资源:IDB_BITMAPBUTTON
4.在初始化中加入如下代码:
//对应于位图按钮
…
HBITMAP hBmp=::LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_ BITMAPBUTTON));
m_BmpBtn.SetBitmap(hBmp);
使按钮失效
按钮变量名.EnableWindow(FALSE);
下面是自己封装的button类,有改按钮文字的前景色,背景色,字体以及按钮本身的背景色和背景图片。
bb.h:
#if !defined(AFX_BB_H__E2D72529_DDA6_4CB2_B212_AB7319736D8E__INCLUDED_)
#define AFX_BB_H__E2D72529_DDA6_4CB2_B212_AB7319736D8E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// bb.h : header file
//
/
// bb window
class bb : public CButton
{
// Construction
public:
bb();
// Attributes
public:
void settextcolor(COLORREF);
void settextbkcolor(COLORREF);
void setbkcolor(COLORREF);
void setimage(char *i);
void setbkmode(int m);
void setfont(int f1,char *f2);
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(bb)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~bb();
// Generated message map functions
protected:
//{{AFX_MSG(bb)
afx_msg void OnPaint();
//}}AFX_MSG
COLORREF textcolor;
COLORREF textbkcolor;
COLORREF bkcolor;
char *image;
int bkmode;
HBITMAP m_hBitmap;
CFont *f;
DECLARE_MESSAGE_MAP()
};
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_BB_H__E2D72529_DDA6_4CB2_B212_AB7319736D8E__INCLUDED_)
bb.cpp:
// bb.cpp : implementation file
//
#include "stdafx.h"
#include "1.h"
#include "bb.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// bb
bb::bb()
{
textbkcolor=::GetSysColor(COLOR_3DFACE);
textcolor=RGB(220,0,0);
bkcolor=::GetSysColor(COLOR_3DFACE);
bkmode=1;
image=0;
f=new CFont;
}
bb::~bb()
{
delete []image;
f->DeleteObject();
}
BEGIN_MESSAGE_MAP(bb, CButton)
//{{AFX_MSG_MAP(bb)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// bb message handlers
void bb::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
CRect rect;
dc.Attach(lpDrawItemStruct ->hDC); // Get the Button DC to CDC
rect=lpDrawItemStruct->rcItem;
rect = lpDrawItemStruct->rcItem; //Store the Button rect to our local rect.
rect = lpDrawItemStruct -> rcItem;
CBitmap m_Bitmap;
CDC dcMem;
BITMAP s_Bmp;
if(image){
m_Bitmap.Attach(m_hBitmap);
dcMem.CreateCompatibleDC(&dc); // 创建于内存设备环境相兼容的设备环境
dcMem.SelectObject(&m_Bitmap);
// 将位图对象选入到内存设备环境中
m_Bitmap.GetBitmap(&s_Bmp); // 获取位图信息
dc.StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),
&dcMem,0,0,s_Bmp.bmWidth,s_Bmp.bmHeight,SRCCOPY);
}else{
//dc.Draw3dRect(&rect,RGB(2,25,25),RGB(10,0,10));
dc.FillSolidRect(&rect,bkcolor);//Here you can define the required color to appear on the Button.
}
UINT state=lpDrawItemStruct->itemState; //This defines the state of the Push button either pressed or not.
if((state & ODS_SELECTED))
{
dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT);
}
else
{
dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);
}
dc.SetBkMode(TRANSPARENT);
dc.SetBkColor(textbkcolor); //Setting the Text Background color
dc.SetTextColor(textcolor); //Setting the Text Color
TCHAR buffer[MAX_PATH]; //To store the Caption of the button.
ZeroMemory(buffer,MAX_PATH ); //Intializing the buffer to zero
::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH); //Get the Caption of Button Window
dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);//Redraw the Caption of Button Window
dc.Detach(); // Detach the Button DC
}
void bb::settextcolor(COLORREF a){
this->textcolor=a;
this->Invalidate();
}
void bb::settextbkcolor(COLORREF a){
this->textbkcolor=a;
this->Invalidate();
}
void bb::setbkcolor(COLORREF a){
this->bkcolor=a;
delete[] image;
image=0;
this->Invalidate();
}
void bb::setbkmode(int a){
this->bkmode=a;
this->Invalidate();
}
void bb::setimage(char *i){
delete[] image;
image=new char[strlen(i)+1];
::strcpy(image,i);
m_hBitmap=(HBITMAP)::LoadImage(NULL,_T(image),IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
this->Invalidate();
}
void bb::setfont(int f2,char *f1){
if(f){
delete f;
}
f=new CFont;
f->CreatePointFont(f2,f1);
this->SetFont(f);
this->Invalidate();
}
关键是对于按钮的重绘,要重载drawitem函数,还要将button设为所有者绘制。其他的都是对于DC的用法,平时多联系就能掌握了。给出个例子:
void CMy1Dlg::OnButton1()
{
m.setfont(900,"黑体");
m.setbkcolor(RGB(166,166,66));
}
void CMy1Dlg::OnButton3()
{
m.setimage("C://Inetpub//wwwroot//三下乡//else//logo.bmp");
m.setfont(400,"隶书");
}
m为bb对象,设置图片只能为bmp格式的。