在使用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;
}
}

250

被折叠的 条评论
为什么被折叠?



