Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1974445
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类: C/C++

2010-12-19 12:34:18

1、设置被控对象的焦点。创建一个基于对话框的应用程序demo,在cdemodlg中踢啊年一个int型的成员变量m_nfocus。在构造函数中添加m_nfocus=0; 为按键添加消息BN_CLICKED的消息处理函数: void CDemoDlg::OnButton1() { // TODO: Add your control notification handler code here CEdit *pedit1=(CEdit *)GetDlgItem(IDC_EDIT1); CEdit *pedit2=(CEdit *)GetDlgItem(IDC_EDIT2); if(m_nfocus==0) { if(pedit2->SetFocus()) { m_nfocus=1; } } else if(m_nfocus==1) { if(pedit1->SetFocus()) { m_nfocus=0; } } } 效果如下: 2、通过控件ID获取控件窗口指针。将控件ID转换成CWnd类指针可以调用CWnd::GetDlgItem函数。为CEdit添加变量: CEdit m_ctrledit;为button添加按键消息处理函数: void CDemo1Dlg::OnButton1() { // TODO: Add your control notification handler code here //获取窗口指针 CEdit* pedit=(CEdit *)GetDlgItem(IDC_EDIT1); CString str=_T(""); str.Format("pedit=0x%X\n&m_ctrledit=0x%X\n",pedit,&m_ctrledit); AfxMessageBox(str); } 效果如下: 3、通过控件窗口指针获取控件ID。为对话框类添加EDit控件的成员变量: CEdit m_ctrledit;为button添加消息处理函数: void CDemo2Dlg::OnButton1() { // TODO: Add your control notification handler code here int nid=m_ctrledit.GetDlgCtrlID(); CString str=_T(""); str.Format("nid=%d\nIDC_EDIT1=%d",nid,IDC_EDIT1); AfxMessageBox(str); } 4、遍历控件。遍历控件可以首先通过CWnd::GetTopWindow获取第一个属于CWnd对象的子窗口,然后调用CWnd::GetNextWindow函数获取下一个子窗口。创建一个基于对话框的应用程序。在CDemo4Dlg类中重载CDialog::OnInitDialog函数,代码如下: BOOL CDemo4Dlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here //...... //获取第一个子窗口 CWnd* pchildwnd=GetTopWindow(); while(pchildwnd!=NULL) { //获取控件id int nid=pchildwnd->GetDlgCtrlID(); CString str=_T(""); str.Format(_T("%d"),nid); pchildwnd->SetWindowText(str); //获取下一个子窗口 pchildwnd=pchildwnd->GetNextWindow(); } return TRUE; // return TRUE unless you set the focus to a control }

阅读(1076) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~