This example illustrates how to save your application's position and window size on closing and how to restore the window to the same position and size on launch. The variables used are saved to the system registry.
Code will be added to your application object and main frame object implementation files. No other files are altered.
If your project is dialog-based, you will modify your application object and your main dialog class.
In the application object file, add the following line of code to the InitInstance(..) class member over-ride. This will create a "CustomSoft" key in the system registry. You should use a label that is appropriate to your needs. The first time that your application is closed, the system will create a subkey under the "CustomSoft" key using your application's file name less it's extension.
SetRegistryKey("CustomSoft");
In the main frame object, over-ride the OnClose(..) class member and save the current frame size and position in the system registry. If your project is dialog-based, over-ride OnClose(..) in your dialog class.
void CMainFrame::OnClose()
// create and fill a WINDOWPLACEMENT structure
WINDOWPLACEMENT wp;
GetWindowPlacement(&wp);
// write window position and size to the system registry
AfxGetApp()->WriteProfileInt("Initialization",
"VertPos",
wp.rcNormalPosition.top);
AfxGetApp()->WriteProfileInt("Initialization",
"HorizPos",
wp.rcNormalPosition.left);
AfxGetApp()->WriteProfileInt("Initialization",
"Height",
wp.rcNormalPosition.bottom - wp.rcNormalPosition.top);
AfxGetApp()->WriteProfileInt("Initialization",
"Width",
wp.rcNormalPosition.right - wp.rcNormalPosition.left);
CFrameWnd::OnClose();
Over-ride the OnShowWindow(...) class member in your main frame class. Retrieve the proper setting from the system registry to set the window size and position. If your project is dialog-based, insert the code below into your OnInitDialog(..) over-ride.
void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus)
{
CFrameWnd::OnShowWindow(bShow, nStatus);
// retrieve the window size and position settings from the system registry
int iVert = AfxGetApp()->GetProfileInt("Initialization","VertPos",0);
int iHoriz = AfxGetApp()->GetProfileInt("Initialization","HorizPos",0);
int Width = AfxGetApp()->GetProfileInt("Initialization","Width",400);;
int Height = AfxGetApp()->GetProfileInt("Initialization","Height",280);
// adjust the application's frame size and position
SetWindowPos(NULL,iHoriz,iVert,Width,Height, NULL);
}
Bookmark
Email This
Hits: 3121
Comments (0)

Write comment



