一、动态创建button
CButton *button=new CButton; button->Create(_T("start"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(50,50,100,100),this,1);//最后一个是ID
BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_BN_CLICKED(1,OnButtonClicked)
END_MESSAGE_MAP()
void CMainWindow::OnButtonClicked(){
AfxMessageBox(_T("clicked"));
}
二、对话框的使用
1、简单的edit控件值的传递与判断
void CMyDialog::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Text(pDX,IDC_NAME,m_name); //将edit控件与类成员联系起来 DDX_Text(pDX,IDC_PHONE,m_phone); DDX_Text(pDX,IDC_AGE,m_age); DDV_MinMaxInt(pDX,m_age,50,100); //判断值是否在50-100范围内 } void CMyDialog::OnClickedButton1() { CString str1=NULL,str2=NULL; GetDlgItemText(IDC_NAME,str1); //获取edit中的值 GetDlgItemText(IDC_PHONE,str2); SetDlgItemText(IDC_NAME,NULL); //设置edit的值 AfxMessageBox(str1+" "+str2); UpdateData(); //用于触发DDV的判断 }
void CMainWindow::OnTestDialog()
{
CMyDialog dlg;
if(dlg.DoModal()==IDOK){
AfxMessageBox(_T("OK"));
}
}