LESSON 7:MFC 对话框的编写 ,关键代码;

void CMyboleView::OnDialog() { // TODO: Add your command handler code here if(m_dlg.GetSafeHwnd()==NULL) { m_dlg.Create(IDD_DIALOG1,this); } m_dlg.ShowWindow(SW_SHOWNORMAL); } 这是响应一个菜单消息,生成一个对话框并显示出来. 对话框对象为View类成员变量.

 

 

void CTestDlg::OnBtnAdd() { // TODO: Add your control notification handler code here /* GetDlgItemText(IDC_EDIT1,ch1,10); GetDlgItemText(IDC_EDIT2,ch2,10); SetDlgItemText(IDC_EDIT3,ch3); */ /* num1=GetDlgItemInt(IDC_EDIT1); num2=GetDlgItemInt(IDC_EDIT2); num3=num1+num2; SetDlgItemInt(IDC_EDIT3,num3); */ /* UpdateData(); m_num3=m_num1+m_num2; UpdateData(FALSE); *//* int num1,num2,num3; m_edit1.GetWindowText(ch1,10); m_edit2.GetWindowText(ch2,10); num1=atoi(ch1); num2=atoi(ch2); num3=num1+num2; itoa(num3,ch3,10); m_edit3.SetWindowText(ch3); */ int num1,num2,num3; char ch1[10],ch2[10],ch3[10]; // ::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd,WM_GETTEXT,10,(LPARAM)ch1); // ::SendMessage(m_edit1.m_hWnd,WM_GETTEXT,10,(LPARAM)ch1); /*m_edit1.SendMessage(WM_GETTEXT,10,(LPARAM)ch1); m_edit2.SendMessage(WM_GETTEXT,10,(LPARAM)ch2); num1=atoi(ch1); num2=atoi(ch2); num3=num1+num2; itoa(num3,ch3,10); m_edit3.SendMessage(WM_SETTEXT,0,(LPARAM)ch3); */ SendDlgItemMessage(IDC_EDIT1,WM_GETTEXT,10,(LPARAM)ch1); SendDlgItemMessage(IDC_EDIT2,WM_GETTEXT,10,(LPARAM)ch2); num1=atoi(ch1); num2=atoi(ch2); num3=num1+num2; itoa(num3,ch3,10); SendDlgItemMessage(IDC_EDIT3,WM_SETTEXT,0,(LPARAM)ch3); SendDlgItemMessage(IDC_EDIT3,EM_SETSEL,0,-1); m_edit3.SetFocus(); }

 

这是各种各样CWnd类获取子控件类的方法与编辑框内容修改关联的办法,主要以GetDlgItem() 与 SetDlgItem()等方法.

也可以将子控件与CWnd类基本类型成员变量关联起来, 或者将子控件与Contral型子控件类关联起来.

 

用基本类型关联时,需要用UpdateData(TRUE/FALSE)来刷新编辑框数据.

 

void CTestDlg::OnButton2() { static CRect rectLarge,rectSmall; if(rectLarge.IsRectNull()) { CRect rectSeparator; GetWindowRect(&rectLarge); GetDlgItem(IDC_SEPRATOR)->GetWindowRect(&rectSeparator); rectSmall.top=rectLarge.top; rectSmall.left=rectLarge.left; rectSmall.right=rectLarge.right; rectSmall.bottom=rectSeparator.bottom; } CString str; if(GetDlgItemText(IDC_BUTTON2,str),str=="收缩<<") { SetDlgItemText(IDC_BUTTON2,"扩展>>"); SetWindowPos(NULL,0,0,rectSmall.Width(),rectSmall.Height(),SWP_NOMOVE|SWP_NOZORDER); } else { SetDlgItemText(IDC_BUTTON2,"收缩<<"); SetWindowPos(NULL,0,0,rectLarge.Width(),rectLarge.Height(),SWP_NOMOVE|SWP_NOZORDER); } }

 

设置可以伸缩的对话框,主要就是通过CWnd::GetWindowRect与SetWindowRect()函数获取设置对话框的大小,通过SetWindowPos设置对话框大小.

void CTestDlg::OnOK() { // TODO: Add extra validation here //GetDlgItem(IDC_EDIT1)->GetNextWindow()->SetFocus(); //GetFocus()->GetNextWindow()->SetFocus(); GetNextDlgTabItem(GetFocus(),FALSE)->SetFocus(); //CDialog::OnOK(); }

 

响应对话框的IDOK消息,这是一个特殊的消息,通过它可以实现敲击回车时的响应操作,这里实现了敲击回车后,聚焦点的移动.

 

通过设置对话框的默认按钮,可以更改默认激活哪个按钮的消息响应函数.

你可能感兴趣的:(validation,command,null,mfc,dialog,button)