1 /////////////////////////////////////////////////////////////////////////////
3 // This file is part of ResizableLib
4 // http://sourceforge.net/projects/resizablelib
6 // Copyright (C) 2000-2004,2008 by Paolo Messina
7 // http://www.geocities.com/ppescher - mailto:ppescher@hotmail.com
9 // The contents of this file are subject to the Artistic License (the "License").
10 // You may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at:
12 // http://www.opensource.org/licenses/artistic-license.html
14 // If you find this code useful, credits would be nice!
16 /////////////////////////////////////////////////////////////////////////////
20 * @brief Implementation of the CResizableWndState class.
24 #include "ResizableWndState.h"
25 #include "..\..\src\Utils\DPIAware.h"
27 //////////////////////////////////////////////////////////////////////
28 // Construction/Destruction
29 //////////////////////////////////////////////////////////////////////
31 CResizableWndState::CResizableWndState()
36 CResizableWndState::~CResizableWndState()
41 // used to save/restore window's size and position
42 // either in the registry or a private .INI file
43 // depending on your application settings
45 #define PLACEMENT_ENT _T("WindowPlacement")
46 #define PLACEMENT_FMT _T("%d,%d,%d,%d,%d,%d,%d,%d")
49 * This function saves the current window position and size using the base
50 * class persist method. Minimized and maximized state is also optionally
52 * @sa CResizableState::WriteState
53 * @note Window coordinates are in the form used by the system functions
54 * GetWindowPlacement and SetWindowPlacement.
56 * @param pszName String that identifies stored settings
57 * @param bRectOnly Flag that specifies wether to ignore min/max state
59 * @return Returns @a TRUE if successful, @a FALSE otherwise
61 BOOL
CResizableWndState::SaveWindowRect(LPCTSTR pszName
, BOOL bRectOnly
)
66 SecureZeroMemory(&wp
, sizeof(WINDOWPLACEMENT
));
67 wp
.length
= sizeof(WINDOWPLACEMENT
);
68 if (!GetResizableWnd()->GetWindowPlacement(&wp
))
71 CDPIAware::Instance().UnscaleWindowPlacement(GetResizableWnd()->GetSafeHwnd(), &wp
);
73 // use workspace coordinates
74 const RECT
& rc
= wp
.rcNormalPosition
;
76 if (bRectOnly
) // save size/pos only (normal state)
78 data
.Format(PLACEMENT_FMT
, rc
.left
, rc
.top
,
79 rc
.right
, rc
.bottom
, SW_SHOWNORMAL
, 0, 0, 0);
81 else // save also min/max state
83 data
.Format(PLACEMENT_FMT
, rc
.left
, rc
.top
,
84 rc
.right
, rc
.bottom
, wp
.showCmd
, wp
.flags
,
85 wp
.ptMinPosition
.x
, wp
.ptMinPosition
.y
);
88 id
= CString(pszName
) + PLACEMENT_ENT
;
89 return WriteState(id
, data
);
93 * This function loads and set the current window position and size using
94 * the base class persist method. Minimized and maximized state is also
95 * optionally preserved.
96 * @sa CResizableState::WriteState
97 * @note Window coordinates are in the form used by the system functions
98 * GetWindowPlacement and SetWindowPlacement.
100 * @param pszName String that identifies stored settings
101 * @param bRectOnly Flag that specifies wether to ignore min/max state
103 * @return Returns @a TRUE if successful, @a FALSE otherwise
105 BOOL
CResizableWndState::LoadWindowRect(LPCTSTR pszName
, BOOL bRectOnly
, BOOL bHorzResize
, BOOL bVertResize
)
110 id
= CString(pszName
) + PLACEMENT_ENT
;
111 if (!ReadState(id
, data
)) // never saved before
114 SecureZeroMemory(&wp
, sizeof(WINDOWPLACEMENT
));
115 wp
.length
= sizeof(WINDOWPLACEMENT
);
116 if (!GetResizableWnd()->GetWindowPlacement(&wp
))
119 // use workspace coordinates
120 RECT
& rc
= wp
.rcNormalPosition
;
122 long min_width
= rc
.right
- rc
.left
;
123 long min_height
= rc
.bottom
- rc
.top
;
125 if (_stscanf(data
, PLACEMENT_FMT
, &rc
.left
, &rc
.top
,
126 &rc
.right
, &rc
.bottom
, &wp
.showCmd
, &wp
.flags
,
127 &wp
.ptMinPosition
.x
, &wp
.ptMinPosition
.y
) == 8)
129 CDPIAware::Instance().ScaleWindowPlacement(GetResizableWnd()->GetSafeHwnd(), &wp
);
130 if ((!bVertResize
) || (rc
.bottom
- rc
.top
< min_height
))
131 rc
.bottom
= rc
.top
+ min_height
;
132 if ((!bHorzResize
) || (rc
.right
- rc
.left
< min_width
))
133 rc
.right
= rc
.left
+ min_width
;
134 if (bRectOnly
) // restore size/pos only
136 wp
.showCmd
= SW_SHOWNORMAL
;
138 return GetResizableWnd()->SetWindowPlacement(&wp
);
140 else // restore minimized window to normal or maximized state
142 if (wp
.showCmd
== SW_SHOWMINIMIZED
&& wp
.flags
== 0)
143 wp
.showCmd
= SW_SHOWNORMAL
;
144 else if (wp
.showCmd
== SW_SHOWMINIMIZED
&& wp
.flags
== WPF_RESTORETOMAXIMIZED
)
145 wp
.showCmd
= SW_SHOWMAXIMIZED
;
146 return GetResizableWnd()->SetWindowPlacement(&wp
);