Click here to Skip to main content
15,881,744 members
Articles / Desktop Programming / WTL
Article

Saving and restoring window appearance in WTL

Rate me:
Please Sign up or sign in to vote.
4.71/5 (9 votes)
27 Jun 2001 98.9K   1.4K   24   14
Simple, but useful classes to save/restore window appearance

Overview

Now you can use simple classes CWindowSettings, CReBarSettings, CSplitterSettings with your WTL projects to quickly save/restore window appearance (size, position). Settings are saved in registry. Additional properties saved for ReBar Control including bands sequence and position. So your window will appear next time when opened as user customized him.

How to use these classes in your WTL App

  1. Add two files RegSettings.h and RegSettings.cpp to your project.
  2. Add the header file RegSettings.h to the Frame or Window source code that will be using the classes.
  3. Load window settings from registry by adding the following code to the method that shows the window (the global Run function for a main frame):

    CWindowSettings ws;
    if(ws.Load("Software\\WTLApps\\DemoApp", "MainFrame"))
       ws.ApplyTo(wndMain, nCmdShow);
    else
       wndMain.ShowWindow(nCmdShow);
  4. Load rebar settings from registry by adding the following code to the OnCreate method of the frame class:

    CReBarSettings rs;
    CReBarCtrl rbc = m_hWndToolBar;
    if(rs.Load("Software\\WTLApps\\DemoApp", "ReBar"))
      rs.ApplyTo(rbc);
  5. Save window and rebar settings to registry by adding the following code to the OnDestroy method of the frame class:

    CWindowSettings ws;
    ws.GetFrom(*this);
    ws.Save("Software\\WTLApps\\DemoApp", "MainFrame");
    
    CReBarSettings rs;
    CReBarCtrl rbc = m_hWndToolBar;
    rs.GetFrom(rbc);
    rs.Save("Software\\WTLApps\\DemoApp", "ReBar");

    Also you can save/restore splitter position using CSplitterSettings class

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSmall adjustment to CreateEx when using splitter bars Pin
Jonathan Davies16-Mar-09 6:00
Jonathan Davies16-Mar-09 6:00 
Hi,
Used the classes which worked well for me so thanks. Though I had one small problem I thought I'd better document as it may help someone out. In my WTL 8.0 app I created horizontal and vertical splitters in CMainFrame::OnCreate as mentioned in your point 5 but had to modify the window creation because of this.

Each time the main window got created the horizontal splitter irritatingly always got restored slightly lower down and the vertical splitter bar always got restored slightly to the right. This was found to be due to the code in atlsplit.h/CSplitterImpl::SetSplitterPos which did an "Adjust if out of valid range" and because the CMainFrame window size hadn't yet been restored it always found it to be "out of valid range" and aways adjusted the position.

This was solved by changing the code creating the CMainFrame window to pass in the position when calling OnCreateEx as below:

int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
	CMessageLoop theLoop;
	_Module.AddMessageLoop(&theLoop);

	CWindowSettings ws;
	CMainFrame wndMain;
	if(ws.Load(_T("Software\\Domain\\Application"), _T("MainFrame")))
	{
		if(wndMain.CreateEx(0,ws.m_WindowPlacement.rcNormalPosition) == NULL)
		{
			...

The creation of splitters which now don't be seen as "out of valid range":
CMainFrame::OnCreate(...)
{
	...

	// Create the vertical splitter
	CRect rcVert;
	GetClientRect(&rcVert);
	m_vSplit.Create(m_hWnd, rcVert, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
	m_vSplit.SetSplitterExtendedStyle(SPLIT_PROPORTIONAL); 

	// Restore the vertical splitter position if it's saved in the registry
	CSplitterSettings vSet;
	if(vSet.Load(_T("Software\\Domain\\Application"), _T("VertSplit")))
		vSet.ApplyTo(m_vSplit);
	else
		m_vSplit.SetSplitterPos(330);	// from left
	m_vSplit.m_bFullDrag = false;	// ghost bar enabled
 	
	...
}

GeneralRe: Small adjustment to CreateEx when using splitter bars Pin
Magomed Abdurakhmanov16-Mar-09 23:50
Magomed Abdurakhmanov16-Mar-09 23:50 
GeneralRe: Small adjustment to CreateEx when using splitter bars Pin
Jonathan Davies17-Mar-09 2:48
Jonathan Davies17-Mar-09 2:48 
GeneralThis is really good Pin
beaverdown10-Mar-04 10:58
beaverdown10-Mar-04 10:58 
GeneralAnother suggestion Pin
yarp4-Jun-03 5:11
yarp4-Jun-03 5:11 
QuestionHow about.. Pin
30-May-01 12:22
suss30-May-01 12:22 
AnswerRe: How about.. Pin
Magomed Abdurakhmanov30-May-01 22:32
Magomed Abdurakhmanov30-May-01 22:32 
GeneralRe: How about.. Pin
31-May-01 6:00
suss31-May-01 6:00 
GeneralRe: How about.. Pin
[James Pullicino]3-Jun-01 22:31
[James Pullicino]3-Jun-01 22:31 
GeneralSuggestions Pin
Maximilian Hänel30-May-01 7:56
Maximilian Hänel30-May-01 7:56 
GeneralRe: Suggestions Pin
Magomed Abdurakhmanov30-May-01 22:38
Magomed Abdurakhmanov30-May-01 22:38 
GeneralRe: Suggestions Pin
Philippe Mori31-May-01 4:08
Philippe Mori31-May-01 4:08 
GeneralRe: Suggestions Pin
Magomed Abdurakhmanov1-Jun-01 5:56
Magomed Abdurakhmanov1-Jun-01 5:56 
GeneralRe: Suggestions Pin
Maximilian Hänel2-Jun-01 12:16
Maximilian Hänel2-Jun-01 12:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.