ResizableState.cpp

00001 // ResizableState.cpp: implementation of the CResizableState class.
00002 //
00004 //
00005 // Copyright (C) 2000-2002 by Paolo Messina
00006 // (http://www.geocities.com/ppescher - [email protected])
00007 //
00008 // The contents of this file are subject to the Artistic License (the "License").
00009 // You may not use this file except in compliance with the License. 
00010 // You may obtain a copy of the License at:
00011 // http://www.opensource.org/licenses/artistic-license.html
00012 //
00013 // If you find this code useful, credits would be nice!
00014 //
00016 
00017 #include "stdafx.h"
00018 #include "ResizableState.h"
00019 
00020 #ifdef _DEBUG
00021 #undef THIS_FILE
00022 static char THIS_FILE[]=__FILE__;
00023 #define new DEBUG_NEW
00024 #endif
00025 
00027 // Construction/Destruction
00029 
00030 CResizableState::CResizableState()
00031 {
00032 
00033 }
00034 
00035 CResizableState::~CResizableState()
00036 {
00037 
00038 }
00039 
00040 
00041 // used to save/restore window's size and position
00042 // either in the registry or a private .INI file
00043 // depending on your application settings
00044 
00045 #define PLACEMENT_ENT   _T("WindowPlacement")
00046 #define PLACEMENT_FMT   _T("%d,%d,%d,%d,%d,%d")
00047 
00048 BOOL CResizableState::SaveWindowRect(LPCTSTR pszSection, BOOL bRectOnly)
00049 {
00050         CString data;
00051         WINDOWPLACEMENT wp;
00052 
00053         ZeroMemory(&wp, sizeof(WINDOWPLACEMENT));
00054         wp.length = sizeof(WINDOWPLACEMENT);
00055         if (!GetResizableWnd()->GetWindowPlacement(&wp))
00056                 return FALSE;
00057         
00058         RECT& rc = wp.rcNormalPosition; // alias
00059 
00060         if (bRectOnly)  // save size/pos only (normal state)
00061         {
00062                 // use screen coordinates
00063                 GetResizableWnd()->GetWindowRect(&rc);
00064 
00065                 data.Format(PLACEMENT_FMT, rc.left, rc.top,
00066                         rc.right, rc.bottom, SW_NORMAL, 0);
00067         }
00068         else    // save also min/max state
00069         {
00070                 // use workspace coordinates
00071                 data.Format(PLACEMENT_FMT, rc.left, rc.top,
00072                         rc.right, rc.bottom, wp.showCmd, wp.flags);
00073         }
00074 
00075         return AfxGetApp()->WriteProfileString(pszSection, PLACEMENT_ENT, data);
00076 }
00077 
00078 BOOL CResizableState::LoadWindowRect(LPCTSTR pszSection, BOOL bRectOnly)
00079 {
00080         CString data;
00081         WINDOWPLACEMENT wp;
00082 
00083         data = AfxGetApp()->GetProfileString(pszSection, PLACEMENT_ENT);
00084         
00085         if (data.IsEmpty())     // never saved before
00086                 return FALSE;
00087         
00088         ZeroMemory(&wp, sizeof(WINDOWPLACEMENT));
00089         wp.length = sizeof(WINDOWPLACEMENT);
00090         if (!GetResizableWnd()->GetWindowPlacement(&wp))
00091                 return FALSE;
00092 
00093         RECT& rc = wp.rcNormalPosition; // alias
00094 
00095         if (_stscanf(data, PLACEMENT_FMT, &rc.left, &rc.top,
00096                 &rc.right, &rc.bottom, &wp.showCmd, &wp.flags) == 6)
00097         {
00098                 if (bRectOnly)  // restore size/pos only
00099                 {
00100                         CRect rect(rc);
00101                         return GetResizableWnd()->SetWindowPos(NULL, rect.left, rect.top,
00102                                 rect.Width(), rect.Height(), SWP_NOACTIVATE | SWP_NOZORDER |
00103                                 SWP_NOREPOSITION);
00104                 }
00105                 else    // restore also min/max state
00106                 {
00107                         return GetResizableWnd()->SetWindowPlacement(&wp);
00108                 }
00109         }
00110         return FALSE;
00111 }

Generated on Tue Dec 13 14:47:57 2005 for guliverkli by  doxygen 1.4.5