在MFC中保存窗口大小的位置和状态 [英] Saving a windows size position and state in MFC
问题描述
如果我调出GUI,然后调整大小并关闭它,当我再次启动它时它会恢复原来的大小。
它应该记住旧值
我尝试了什么:
以下方法将在关闭GUI时启动它会写出大小
void CMainFrame :: OnDestroy()
{
m_bShuttingDown = true;
WINDOWPLACEMENT wp;
GetWindowPlacement(& wp);
AfxGetApp() - > WriteProfileBinary(_T(MainFrame),_ T( WP),(LPBYTE)& wp,sizeof(wp));
CFrameWndEx :: OnDestroy();
}
以下方法将在加载GUI时调用
If I bring up the GUI, and then I resize and close it, when I start it up again it reverts back to the original size.
it should remember the old values
What I have tried:
the below method will get intiate while closing GUI and it will write the size
void CMainFrame::OnDestroy()
{
m_bShuttingDown = true;
WINDOWPLACEMENT wp;
GetWindowPlacement(&wp);
AfxGetApp()->WriteProfileBinary(_T("MainFrame"), _T("WP"), (LPBYTE)&wp, sizeof(wp));
CFrameWndEx::OnDestroy();
}
the below method will get call while loading GUI
oid CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus)
{
CFrameWndEx::OnShowWindow(bShow, nStatus);
if (bShow && !IsWindowVisible()) //The formal parameter bShow is true when the window is about to be shown,
//and false when it is about to be hidden
{
WINDOWPLACEMENT *lwp;
UINT nl;
if (AfxGetApp()->GetProfileBinary(_T("MainFrame"), _T("WP"), (LPBYTE*)&lwp, &nl))
{
SetWindowPlacement(lwp);
UpdateWindow();
delete[] lwp;
}
}the above method its returning old values but gui is positioning as per that values
could you please tell me if anything is wrong here
推荐答案
看起来您使用了CodeProject文章中的代码在MFC中保存窗口大小的位置和状态 [ ^ ]但未完全阅读。
OnShowWindow
也可能在程序执行期间被调用,但您可能希望仅在程序启动时调用它。文章提到并使用状态变量bOnce
提供代码,仅在第一次调用OnShowWindow
时设置位置。
将展示位置保存在CMainFrame中:: OnClose
而不是OnDestroy
。
在<$ c中设置展示位置$ c> CMainFrame :: OnCreate 而不是OnShowWindow
。但是,这将只设置位置,并且稍后从InitInstance
调用ShowWindow
时需要该命令传递存储:
It looks like you have used the code from the CodeProject article Saving a windows size position and state in MFC[^] but did not read it completely.
OnShowWindow
might be also called during program execution but you probably want it to be called only at program start. The article mentions that and provides code using the state variablebOnce
to set the placement only whenOnShowWindow
is called the first time.
Save the placement inCMainFrame::OnClose
instead ofOnDestroy
.
Set the placement inCMainFrame::OnCreate
instead ofOnShowWindow
. However, this will set only the position and the size will be set later when callingShowWindow
fromInitInstance
requiring that the command is passed stored:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// ... (call OnCreate of base class)
WINDOWPLACEMENT *lwp;
UINT nl;
if (AfxGetApp()->GetProfileBinary(_T("MainFrame"), _T("WP"), (LPBYTE*)&lwp, &nl))
{
// save show state
m_nCmdShow = lwp->showCmd;
// must set this to normal here
lwp->showCmd = SW_SHOWNORMAL;
SetWindowPlacement(lwp);
delete[] lwp;
}
// ...
}
// To be called from InitInstance()
void CMainFrame::ShowWindowInitial()
{
ShowWindow(m_nCmdShow);
UpdateWindow();
}
BOOL CMyApp::InitInstance()
{
// ...
//CMainFrame pMainFrame = new CMainFrame;
pMainFrame->ShowWindowInitial();
// ...
}
[/ EDIT]
这篇关于在MFC中保存窗口大小的位置和状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!