Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Posted
Updated 5-Sep-20 11:19am
Comments
Richard MacCutchan 5-Sep-17 2:41am    
Use the debugger to check which values are being saved and loaded in each case.
Member 13089825 5-Sep-17 2:46am    
i have debugged ,values are restoring correctly but GUI is not updating with restoring values

I found these 2 functions to work for me.

void MyDialog::LoadDialogPlacement()
{
	static WINDOWPLACEMENT last_wp = {};
	// Load last stored DB version
	WINDOWPLACEMENT *wp = new WINDOWPLACEMENT;
	GetStoredWindowPlacement(&wp);
	if (memcmp((void *)&last_wp, (const void *)wp, sizeof(WINDOWPLACEMENT)) == 0) return;
	memcpy((void *)&last_wp, (const void *)wp, sizeof(WINDOWPLACEMENT));
	SetWindowPlacement(wp);
	delete[] wp;

}
void MyDialog::SaveDialogPlacement()
{
	static WINDOWPLACEMENT last_wp = {};

	if (IsWindowVisible())
	{
		WINDOWPLACEMENT wp = {};
		wp.length = sizeof(WINDOWPLACEMENT);
		GetWindowPlacement(&wp);
		if (memcmp((void *)&last_wp, (const void *)&wp, wp.length) == 0) return;
		memcpy((void *)&last_wp, (const void *)&wp, wp.length);
		StoreWindowPlacement(&wp);
	}

}
 
Share this answer
 
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 variable bOnce to set the placement only when OnShowWindow is called the first time.

[EDIT: A different approach that I have used in the past]
Save the placement in CMainFrame::OnClose instead of OnDestroy.

Set the placement in CMainFrame::OnCreate instead of OnShowWindow. However, this will set only the position and the size will be set later when calling ShowWindow from InitInstance 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]
 
Share this answer
 
v6
Comments
Member 13089825 5-Sep-17 3:03am    
void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus)
{
CFrameWndEx::OnShowWindow(bShow, nStatus);

static bool bOnce = true;

if (bShow && !IsWindowVisible()
&& bOnce)
{
bOnce = false;

WINDOWPLACEMENT *lwp;
UINT nl;

if (AfxGetApp()->GetProfileBinary(_T("MainFrame"), _T("WP"), (LPBYTE*)&lwp, &nl))
{
SetWindowPlacement(lwp);
delete[] lwp;
}
}
}
Still same issue
Jochen Arndt 5-Sep-17 3:24am    
"the above method its returning old values but gui is positioning as per that values"
Can you try to explain that in another (better) way?

I read that as:
- Returning old values = Data saved at last program exit are read: OK
- But GUI is positioning as per that values = ?
"that" would then refer to the data read which are the correct ones.
Member 13089825 5-Sep-17 3:30am    
I read that as:
- Returning old values = Data saved at last program exit are read: OK
- But GUI is positioning as per that values = ? gui is not positioning as per the values
"that" would then refer to the data read which are the correct ones.
Jochen Arndt 5-Sep-17 3:41am    
I have updated my solution with code used by one of my applications.
If that does not work too, you have to use the debugger to check if your code reaches the SetWindowPlacement call and which values are set.
Member 13089825 5-Sep-17 3:41am    
x and y position is updating correctly but width and height not updating

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900