2 // It it works contact Paul DiLascia
3 // if not you're own your own
6 #include "WindowPlacement.h"
8 CWindowPlacement::CWindowPlacement()
10 // Note: "length" is inherited from WINDOWPLACEMENT
11 length = sizeof(WINDOWPLACEMENT);
15 CWindowPlacement::~CWindowPlacement()
20 // Restore window placement from profile key
21 BOOL CWindowPlacement::Restore(LPCTSTR lpKeyName, CWnd* pWnd)
23 if (!GetProfileWP(lpKeyName))
25 // Only restore if window intersets the screen.
28 CRect rcScreen(0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
29 if (!::IntersectRect(&rcTemp, &rcNormalPosition, &rcScreen))
32 pWnd->SetWindowPlacement(this);
37 // Get window placement from profile.
38 BOOL CWindowPlacement::GetProfileWP(LPCTSTR lpKeyName)
40 CWinApp *pApp = AfxGetApp();
43 m_showCmd = pApp->GetProfileInt(lpKeyName, _T("wp.showCmd"), -1);
47 flags = pApp->GetProfileInt(lpKeyName, _T("wp.flags"), flags);
49 ptMinPosition.x = pApp->GetProfileInt(lpKeyName, _T("wp.ptMinPosition.x"),ptMinPosition.x);
50 ptMinPosition.y = pApp->GetProfileInt(lpKeyName, _T("wp.ptMinPosition.y"),ptMinPosition.y);
51 ptMaxPosition.x = pApp->GetProfileInt(lpKeyName, _T("wp.ptMaxPosition.x"),ptMaxPosition.x);
52 ptMaxPosition.y = pApp->GetProfileInt(lpKeyName, _T("wp.ptMaxPosition.y"),ptMaxPosition.y);
54 RECT& rc = rcNormalPosition; // because I hate typing
55 rc.left = pApp->GetProfileInt(lpKeyName, _T("wp.left"), rc.left);
56 rc.right = pApp->GetProfileInt(lpKeyName, _T("wp.right"), rc.right);
57 rc.top = pApp->GetProfileInt(lpKeyName, _T("wp.top"), rc.top);
58 rc.bottom= pApp->GetProfileInt(lpKeyName, _T("wp.bottom"), rc.bottom);
63 // Save window placement in app profile
64 void CWindowPlacement::Save(LPCTSTR lpKeyName, CWnd* pWnd)
66 pWnd->GetWindowPlacement(this);
67 WriteProfileWP(lpKeyName);
71 // Write window placement to app profile
72 void CWindowPlacement::WriteProfileWP(LPCTSTR lpKeyName)
74 CWinApp *pApp = AfxGetApp();
76 pApp->WriteProfileInt(lpKeyName, _T("wp.showCmd"), showCmd);
77 pApp->WriteProfileInt(lpKeyName, _T("wp.flags"), flags);
78 pApp->WriteProfileInt(lpKeyName, _T("wp.ptMinPosition.x"), ptMinPosition.x);
79 pApp->WriteProfileInt(lpKeyName, _T("wp.ptMinPosition.y"), ptMinPosition.y);
80 pApp->WriteProfileInt(lpKeyName, _T("wp.ptMaxPosition.x"), ptMaxPosition.x);
81 pApp->WriteProfileInt(lpKeyName, _T("wp.ptMaxPosition.y"), ptMaxPosition.y);
82 pApp->WriteProfileInt(lpKeyName, _T("wp.left"), rcNormalPosition.left);
83 pApp->WriteProfileInt(lpKeyName, _T("wp.right"), rcNormalPosition.right);
84 pApp->WriteProfileInt(lpKeyName, _T("wp.top"), rcNormalPosition.top);
85 pApp->WriteProfileInt(lpKeyName, _T("wp.bottom"),rcNormalPosition.bottom);
88 // The ugly casts are required to help the VC++ 3.0 compiler decide which
89 // operator<< or operator>> to use. If you're using VC++ 4.0 or later, you
90 // can delete this stuff.
92 #if (_MSC_VER < 1000) // 1000 = VC++ 4.0
93 #define UINT_CAST (LONG)
94 #define UINT_CASTREF (LONG&)
101 // Write window placement to archive
102 // WARNING: archiving functions are untested.
103 CArchive& operator<<(CArchive& ar, const CWindowPlacement& wp)
105 ar << UINT_CAST wp.length;
106 ar << UINT_CAST wp.flags;
107 ar << UINT_CAST wp.showCmd;
108 ar << wp.ptMinPosition;
109 ar << wp.ptMaxPosition;
110 ar << wp.rcNormalPosition;
115 // Read window placement from archive
116 // WARNING: archiving functions are untested.
117 CArchive& operator>>(CArchive& ar, CWindowPlacement& wp)
119 ar >> UINT_CASTREF wp.length;
120 ar >> UINT_CASTREF wp.flags;
121 ar >> UINT_CASTREF wp.showCmd;
122 ar >> wp.ptMinPosition;
123 ar >> wp.ptMaxPosition;
124 ar >> wp.rcNormalPosition;