几何尺寸与公差论坛

 找回密码
 注册
查看: 3265|回复: 0

【转帖】用vc制作可扩展对话框

[复制链接]
发表于 2008-5-5 12:03:21 | 显示全部楼层 |阅读模式
为了说明用VC制作可扩展对话框的方法,我们将制作如图1效果的对话框。

  创建一个基于对话框、名为Expand的工程。编辑工程中对话框的内容并不重要,重要的是必须添加一个作分界线的控件和一个“高级”按钮。我们使用Picture控件(也可换为其它控件)当作分界线。类型 id property button idc_advance "高级(&&a)〉〉" picture idc_divider not visible
  为IDC_ADVANCE按钮添加BN_CLICKED消息处理函数:

  void CExpandDlg::OnAdvance()

  {

   static BOOL bExpand=TRUE;//记录扩展或收缩状态

   ExpandDialog(IDC_DIVIDER,bExpand);//扩展或收缩对话框

   bExpand=!bExpand;

  }

  添加用于扩展或收缩的保护成员函数ExpandDialog():

  void CExpandDlg::ExpandDialog(int nResourceID, BOOL bExpand)

  {

   static CRect rcLarge;

  //记录扩展后对话框的位置和大小

   static CRect rcSmall;

  //记录收缩后对话框的位置和大小

   CString sExpand;

   if(rcLarge.IsRectNull())

   {

   CRect rcLandmark;

  //记录分界线的位置和大小

   CWnd? pWndLandmark=GetDlgItem(nResourceID);

   ASSERT(pWndLandmark);

   GetWindowRect(rcLarge);

   pWndLandmark->GetWindowRect(rcLandmark);

   rcSmall=rcLarge;

   rcSmall.bottom=rcLandmark.top;// 如果将扩展部分放在对话框的右边,只需将此句改为rcSmall.right=rcLandmark.left;即可

   }

   if(bExpand)

   {

   SetWindowPos(NULL,0,0,rcLarge.Width(),rcLarge.Height(),

  SWP_NOMOVE|SWP_NOZORDER);

   sExpand="常规(&&L)<<";

   EnableVisibleChildren();

   }

   else

   {

   SetWindowPos(NULL,0,0,rcSmall.Width(),rcSmall.Height(),SWP_NOMOVE|SWP_NOZORDER);

   sExpand="高级(&&M)>>";

   EnableVisibleChildren();

   }

   SetDlgItemText(IDC_ADVANCE,sExpand);

  }

  扩展和收缩情况下均调用保护成员函数EnableVisibleChildren()。此函数遍历对话框中各控件,如果某一控件在当前对话框范围之外则使其失效,防止用Tab键或快捷键仍能访问它。

  void CExpandDlg::EnableVisibleChildren()

  {

   CWnd? pWndCtl=GetWindow(GW_CHILD);

   CRect rcTest;

   CRect rcControl;


  图1 扩展后的对话框

  //记录各控件的位置和大小

   CRect rcShow;

  //记录目前显示对话框的位置和大小

   GetWindowRect(rcShow);

   while(pWndCtl!=NULL)

   {

   pWndCtl->GetWindowRect(rcControl);

   if(rcTest.IntersectRect(rcShow,rcControl))

   pWndCtl->EnableWindow(TRUE);

   else

   pWndCtl->EnableWindow(FALSE);

   pWndCtl=pWndCtl->GetWindow(GW_HWNDNEXT);

   }

  }

  最后,在对话框类的OnInitDialog函数的return语句之前加入:

  ExpandDialog(IDC_DIVIDER,FALSE);

  以上程序在Windows 98、Visual C++ 6.0下调试通过。  

文章来源于http://www.lupaworld.com
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-5-20 16:41 , Processed in 0.036861 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表