一个帮助记忆对话框当前位置的C++类

在使用VC开发应用程序时经常遇到要一个窗口记住位置,经常重写几乎完全一样的代码非常繁琐,于是编写了一个类,简化了记忆和恢复窗口位置的操作。

class MEDICONEXPORT CUniRememberMe
{
public:
 CUniRememberMe(void);
 ~CUniRememberMe(void);
 // 记住窗口的位置 ,bRemember 1-保存窗口的位置到注册表中 0-加载窗口的位置
 CUniRememberMe(CWnd * pWnd , bool bRemember=true)

{

    ASSERT(pWnd && pWnd->GetSafeHwnd() );
 
 CString sSection;
 CString sItem;
 //计算注册表函数中的Section
 CString sText;
 pWnd->GetWindowText(sText);
 CString sParentText;
 CWnd * pParent=pWnd->GetParent();
 if(pParent)
  pParent->GetWindowText(sParentText);
 int nID=pWnd->GetDlgCtrlID();
 CString sID;
 sID.Format("%d",nID);
 sSection=sParentText+"_"+sText+"_"+sID;

 if(bRemember)
 {
  CRect rect;
  pWnd->GetWindowRect(&rect);
  sItem="Posleft";
  gbl.MWriteProfileInt(sSection,sItem,rect.left);
  sItem="PosTop";
  gbl.MWriteProfileInt(sSection,sItem,rect.top );
  sItem="PosRight";
  gbl.MWriteProfileInt(sSection,sItem,rect.right );
  sItem="PosBottom";
  gbl.MWriteProfileInt(sSection,sItem,rect.bottom );
 }
 else
 {
  CRect rect;
  pWnd->GetWindowRect(&rect);
  sItem="Posleft";
  rect.left= gbl.MGetProfileInt(sSection,sItem,rect.left);
  sItem="PosTop";
  rect.top =gbl.MGetProfileInt(sSection,sItem,rect.top );
  sItem="PosRight";
  rect.right =gbl.MGetProfileInt(sSection,sItem,rect.right );
  sItem="PosBottom";
  rect.bottom =gbl.MGetProfileInt(sSection,sItem,rect.bottom );
  pWnd->SetWindowPos(NULL,rect.left,rect.top,rect.Width(),rect.Height(),SWP_NOZORDER);

 }

}
};

上述类的使用非常简单,在对话框初始化函数中:

OOL CUniNewEntityDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 //对数据进行初始化
 Init();
 //恢复对话框的位置
 CUniRememberMe re(this,false);
 return TRUE;  // return TRUE unless you set the focus to a control
 // 异常: OCX 属性页应返回 FALSE
}

在对话框的OnDestroy函数中:

void CUniNewEntityDlg::OnDestroy()
{
 CDialog::OnDestroy();
 
 CUniRememberMe re(this,true);
 
 if(m_pGrid)
 {
  m_pGrid->DestroyWindow();
  delete m_pGrid;
  m_pGrid=NULL;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值