源码下载 :http://download.youkuaiyun.com/detail/u010261063/9507552
1、打开资源文件String table文件添加IDC_MYBUTTON字符串资源caption一定要写控件的标题和注解:
2、在头文件生命指针:CButton *mp_mybutton
<span style="font-size:18px;">class CDBNDlg : public CDialog
{
// Construction
public:
CDBNDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CDBNDlg)
enum { IDD = IDD_DBN_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDBNDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
CButton m_btn; // 定义一个按钮对象
<strong>CButton *mp_mybutton;</strong>
// Generated message map functions
//{{AFX_MSG(CDBNDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
//}}AFX_MSG
<strong>afx_msg void OnBtnClick(); //声明一个消息处理函数</strong>
DECLARE_MESSAGE_MAP()
};</span>
3、采用 mp_mybutton = new CButton();
<span style="font-size:18px;">BOOL CDBNDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// 动态创建一个按钮
<strong>RECT rect;
mp_mybutton = new CButton();
rect.top = 10;
rect.left = 10;
rect.right =100;
rect.bottom = 50;
mp_mybutton->Create("CH",WS_CHILD|WS_VISIBLE|WS_BORDER|BS_PUSHBOX,rect,this,IDS_MYBUTTON);</strong>
return TRUE; // return TRUE unless you set the focus to a control
}</span>
5,声明消息处理函数
6、定义消息处理
<span style="font-size:18px;">void CDBNDlg::OnBtnClick()
{
// 响应单击按钮
MessageBox("点击了我!!");
}
</span>
函数
<span style="font-size:18px;">BEGIN_MESSAGE_MAP(CDBNDlg, CDialog)
//{{AFX_MSG_MAP(CDBNDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
<strong>ON_BN_CLICKED(IDS_MYBUTTON,OnBtnClick) //在此加入消息映射宏</strong>
END_MESSAGE_MAP()</span>
注:
创建Windows按钮控件并将它附加到 CButton 对象。
virtual BOOL Create(
LPCTSTR lpszCaption,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID
);
如果成功,非零;否则为0。
在构造在两个步骤的一 CButton 对象。首先,调用构造函数然后调用 Create,创建Windows按钮控件并将它附加到 CButton 对象。
如果给定 WS_VISIBLE 样式,Windows发送按钮控件所需的所有信息激活和显示按钮。
将以下 窗口样式 于按钮控件:
-
始终WS_CHILD
-
通常WS_VISIBLE
-
少见WS_DISABLED
-
对控件分组的WS_GROUP
-
包含按钮的WS_TABSTOP 按tab键顺序
-
CButton myButton1, myButton2, myButton3, myButton4; // Create a push button. myButton1.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(10,10,100,30), pParentWnd, 1); // Create a radio button. myButton2.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON, CRect(10,40,100,70), pParentWnd, 2); // Create an auto 3-state button. myButton3.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_AUTO3STATE, CRect(10,70,100,100), pParentWnd, 3); // Create an auto check box. myButton4.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX, CRect(10,100,100,130), pParentWnd, 4);