forked from lioncash/ExtractData
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowBase.cpp
248 lines (203 loc) · 5.99 KB
/
WindowBase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "stdafx.h"
#include "Common.h"
#include "WindowBase.h"
CWindowBase::CWindowBase()
{
m_hWnd = NULL;
m_hInst = NULL;
m_oldWndProc = NULL;
m_bDialog = FALSE;
}
CWindowBase::~CWindowBase()
{
RemoveProp(m_hWnd, _T("CWindowBase"));
}
void CWindowBase::Init()
{
MoveWindowCenter();
}
void CWindowBase::Init(LONG cx, LONG cy)
{
MoveWindowCenter(cx, cy);
}
void CWindowBase::Init(UINT uID, LONG cx, LONG cy)
{
m_uID = uID;
YCIni clIni( SBL_STR_INI_EXTRACTDATA );
clIni.SetSection( m_uID );
RECT rc;
SetRect(&rc, 0, 0, cx, cy);
POINT pt = GetCenterPt(rc);
clIni.SetKey( _T("Left") );
clIni.ReadDec( &pt.x );
clIni.SetKey( _T("Top") );
clIni.ReadDec( &pt.y );
clIni.SetKey( _T("Width") );
clIni.ReadDec( &cx );
clIni.SetKey( _T("Height") );
clIni.ReadDec( &cy );
WINDOWPLACEMENT wndpl;
memset(&wndpl, 0, sizeof(WINDOWPLACEMENT));
wndpl.length = sizeof(WINDOWPLACEMENT);
wndpl.showCmd = SW_HIDE;
SetRect(&wndpl.rcNormalPosition, pt.x, pt.y, pt.x + cx, pt.y + cy);
SetWindowPlacement(m_hWnd, &wndpl);
clIni.SetKey( _T("showCmd") );
clIni.ReadDec<UINT>( &wndpl.showCmd, SW_SHOWNORMAL );
ShowWindow( m_hWnd, wndpl.showCmd );
}
void CWindowBase::Init(HWND hWnd)
{
MoveWindowCenter(hWnd);
}
void CWindowBase::Init(HWND hWnd, LONG cx, LONG cy)
{
MoveWindowCenter(hWnd, cx, cy);
}
void CWindowBase::SaveIni()
{
YCIni clIni( SBL_STR_INI_EXTRACTDATA );
clIni.SetSection( m_uID );
WINDOWPLACEMENT wndpl;
wndpl.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(m_hWnd, &wndpl);
// Save Position
clIni.SetKey( _T("Left") );
clIni.WriteDec( wndpl.rcNormalPosition.left );
clIni.SetKey( _T("Top") );
clIni.WriteDec( wndpl.rcNormalPosition.top );
// Save Size
clIni.SetKey( _T("Width") );
clIni.WriteDec( wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left );
clIni.SetKey( _T("Height") );
clIni.WriteDec( wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top );
if( wndpl.showCmd != SW_SHOWMINIMIZED )
{
clIni.SetKey( _T("showCmd") );
clIni.WriteDec( wndpl.showCmd );
}
}
BOOL CWindowBase::Attach(HWND hWnd)
{
if (!hWnd)
return FALSE;
m_hWnd = hWnd;
m_hInst = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
// Determine window or dialog
m_bDialog = GetWindowLong(hWnd, DWL_DLGPROC);
int tproc = m_bDialog ? DWL_DLGPROC : GWL_WNDPROC;
SetProp(m_hWnd, _T("CWindowBase"), (HANDLE)this);
// Subclass an existing window
if (GetWindowLong(m_hWnd, tproc) != (LONG)WndStaticProc)
m_oldWndProc = (WNDPROC)SetWindowLong(m_hWnd, tproc, (LONG)WndStaticProc);
return TRUE;
}
BOOL CWindowBase::Detach()
{
if (!m_hWnd)
return FALSE;
// Remove the subclass
if (m_oldWndProc)
{
int tproc = (m_bDialog == TRUE) ? DWL_DLGPROC : GWL_WNDPROC;
SetWindowLong(m_hWnd, tproc, (DWORD)m_oldWndProc);
}
RemoveProp(m_hWnd, _T("CWindowBase"));
return TRUE;
}
LRESULT CALLBACK CWindowBase::WndStaticProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
// Retrieve data from the property list
CWindowBase* tWnd = (CWindowBase*)GetProp(hWnd, _T("CWindowBase"));
// Could not be obtained during processing
if (tWnd == NULL)
{
if ((msg == WM_CREATE) || (msg == WM_NCCREATE))
tWnd = (CWindowBase*)((LPCREATESTRUCT)lp)->lpCreateParams;
else if (msg == WM_INITDIALOG)
tWnd = (CWindowBase*)lp;
if (tWnd)
tWnd->Attach(hWnd);
}
// Success getting the tWnd
if (tWnd)
{
LRESULT lResult = tWnd->WndProc(hWnd, msg, wp, lp);
if (msg == WM_DESTROY)
tWnd->Detach();
return lResult;
}
// If dialog, returns FALSE
if (GetWindowLong(hWnd, DWL_DLGPROC))
return FALSE;
return DefWindowProc(hWnd, msg, wp, lp);
}
LRESULT CWindowBase::WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
// If any subclasses of old window processing are left.
if (m_oldWndProc)
return CallWindowProc(m_oldWndProc, hWnd, msg, wp, lp);
// If there is a dialog, return FALSE
if (m_bDialog)
return FALSE;
// If it is not subclassed, call the default window procedure
return DefWindowProc(hWnd, msg, wp, lp);
}
void CWindowBase::MoveWindowCenter()
{
RECT rc;
GetWindowRect(m_hWnd, &rc);
POINT pt = GetCenterPt(rc);
MoveWindow(m_hWnd, pt.x, pt.y, rc.right - rc.left, rc.bottom - rc.top, TRUE);
}
void CWindowBase::MoveWindowCenter(LONG cx, LONG cy)
{
RECT rc;
SetRect(&rc, 0, 0, cx, cy);
POINT pt = GetCenterPt(rc);
MoveWindow(m_hWnd, pt.x, pt.y, rc.right - rc.left, rc.bottom - rc.top, TRUE);
}
void CWindowBase::MoveWindowCenter(HWND hWnd)
{
RECT rc;
GetWindowRect(hWnd, &rc);
POINT pt = GetCenterPt(hWnd, rc);
MoveWindow(hWnd, pt.x, pt.y, rc.right - rc.left, rc.bottom - rc.top, TRUE);
}
void CWindowBase::MoveWindowCenter(HWND hWnd, LONG cx, LONG cy)
{
RECT rc;
SetRect(&rc, 0, 0, cx, cy);
POINT pt = GetCenterPt(hWnd, rc);
MoveWindow(hWnd, pt.x, pt.y, rc.right - rc.left, rc.bottom - rc.top, TRUE);
}
POINT CWindowBase::GetCenterPt(RECT& dlgrc)
{
RECT ParentRect;
HWND hParentWnd = GetParent(m_hWnd);
if (hParentWnd == NULL)
hParentWnd = GetDesktopWindow();
GetWindowRect(hParentWnd, &ParentRect);
//CError error;
//error.Message(m_hWnd, _T("%d %d %d %d"), ParentRect.left, ParentRect.top, ParentRect.right, ParentRect.bottom);
POINT pt;
pt.x = ParentRect.left + ((ParentRect.right - ParentRect.left) - (dlgrc.right - dlgrc.left)) / 2;
pt.y = ParentRect.top + ((ParentRect.bottom - ParentRect.top) - (dlgrc.bottom - dlgrc.top)) / 2;
//error.Message(m_hWnd, _T("%d %d %d %d"), dlgrc.left, dlgrc.top, dlgrc.right, dlgrc.bottom);
return (pt);
}
POINT CWindowBase::GetCenterPt(HWND hWnd, RECT& dlgrc)
{
RECT ParentRect;
HWND hParentWnd = GetParent(hWnd);
if (hParentWnd == NULL)
hParentWnd = GetDesktopWindow();
GetWindowRect(hParentWnd, &ParentRect);
//CError error;
//error.Message(m_hWnd, _T("%d %d %d %d"), ParentRect.left, ParentRect.top, ParentRect.right, ParentRect.bottom);
POINT pt;
pt.x = ParentRect.left + ((ParentRect.right - ParentRect.left) - (dlgrc.right - dlgrc.left)) / 2;
pt.y = ParentRect.top + ((ParentRect.bottom - ParentRect.top) - (dlgrc.bottom - dlgrc.top)) / 2;
//error.Message(m_hWnd, _T("%d %d %d %d"), dlgrc.left, dlgrc.top, dlgrc.right, dlgrc.bottom);
return (pt);
}