MFC1、动态创建CButton

源码下载 :http://download.csdn.net/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>

函数


6,给button添加消息处理函数:

<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 
);

lpszCaption

指定按钮控件的文本。

dwStyle

指定按钮控件的样式。适用于 按钮样式 的任意组合按钮。

rect

指定按钮控件的大小和位置。它可以是 CRect 对象或 RECT 结构。

pParentWnd

指定按钮控件的父窗口,通常 CDialog。不能为 NULL。

nID

指定按钮控件的ID.

返回值

如果成功,非零;否则为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);



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Car12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值