**【实验目的】**
一、 掌握使用编程方式和拖拉方式来建立一个按钮。
二、 掌握使用控件变量和DDX数据交换。编程
**【实验过程】**
步骤以下:less
BOOL CEx\_CreateDlg::OnInitDialog() { CDialog::OnInitDialog(); //… m\_btnWnd.Create("你好", WS\_CHILD | WS\_VISIBLE | BS\_PUSHBUTTON | WS\_TABSTOP, CRect(20, 20, 120, 40), this, 201); // 建立 CFont \*font = this->GetFont(); // 获取对话框的字体 m\_btnWnd.SetFont(font); // 设置控件字体 return TRUE; // return TRUE unless you set the focus to a control }
void CEx_CreateDlg::OnButton1() { MessageBox(_T("你按下了\"Button1\"按钮!")); }
BOOL CEx_CreateDlg::OnCommand(WPARAM wParam, LPARAM lParam) { WORD nCode = HIWORD(wParam); // 控件的通知消息 WORD nID = LOWORD(wParam); // 控件的ID if ((nID == 201)&&(nCode == BN_CLICKED)) MessageBox(_T("你按下了\"你好\"按钮!")); if ((nID == IDC_BUTTON1)&&(nCode == BN_CLICKED)) MessageBox(_T("这是在OnCommand处理的结果!")); return CDialog::OnCommand(wParam, lParam); }
步骤以下:函数
void CEx_MemberDlg::OnButton1() { UpdateData(); // 将控件的内容存放到变量中 // 没有参数,表示使用的是默认参数值TRUE m_Static1.SetWindowText(m_str1); } void CEx_MemberDlg::OnButton2() { // TODO: Add your control notification handler code here CString str; CStatic *pStatic = (CStatic *)GetDlgItem(IDC_STATIC1); pStatic->GetWindowText(str); m_Static2.SetWindowText(str); } void CEx_MemberDlg::OnButton3() { // TODO: Add your control notification handler code here UpdateData(TRUE); // 将控件的内容存放到变量中 // 没有参数,表示使用的是默认参数值TRUE m_str2=m_str1; UpdateData(false); // 将变量内容存放到控件中 // 没有参数,表示使用的是默认参数值TRUE } void CEx_MemberDlg::OnButton4() { // TODO: Add your control notification handler code here CString str; CStatic *pStatic = (CStatic *)GetDlgItem(IDC_STATIC1); pStatic->GetWindowText(str); m_Edit1.SetWindowText(str); }