• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Windows/Window.h
2 
3 #ifndef ZIP7_INC_WINDOWS_WINDOW_H
4 #define ZIP7_INC_WINDOWS_WINDOW_H
5 
6 #include "../Common/MyWindows.h"
7 #include "../Common/MyString.h"
8 
9 #include "Defs.h"
10 
11 #ifndef UNDER_CE
12 #ifdef WM_CHANGEUISTATE
13 #define Z7_WIN_WM_CHANGEUISTATE  WM_CHANGEUISTATE
14 #define Z7_WIN_WM_UPDATEUISTATE  WM_UPDATEUISTATE
15 #define Z7_WIN_WM_QUERYUISTATE   WM_QUERYUISTATE
16 #else
17 // these are defined for (_WIN32_WINNT >= 0x0500):
18 #define Z7_WIN_WM_CHANGEUISTATE  0x0127
19 #define Z7_WIN_WM_UPDATEUISTATE  0x0128
20 #define Z7_WIN_WM_QUERYUISTATE   0x0129
21 #endif
22 
23 #ifdef UIS_SET
24 
25 #define Z7_WIN_UIS_SET         UIS_SET
26 #define Z7_WIN_UIS_CLEAR       UIS_CLEAR
27 #define Z7_WIN_UIS_INITIALIZE  UIS_INITIALIZE
28 
29 #define Z7_WIN_UISF_HIDEFOCUS  UISF_HIDEFOCUS
30 #define Z7_WIN_UISF_HIDEACCEL  UISF_HIDEACCEL
31 
32 #else
33 // these are defined for (_WIN32_WINNT >= 0x0500):
34 // LOWORD(wParam) values in WM_*UISTATE
35 #define Z7_WIN_UIS_SET         1
36 #define Z7_WIN_UIS_CLEAR       2
37 #define Z7_WIN_UIS_INITIALIZE  3
38 
39 // HIWORD(wParam) values in WM_*UISTATE
40 #define Z7_WIN_UISF_HIDEFOCUS  0x1
41 #define Z7_WIN_UISF_HIDEACCEL  0x2
42 // defined for for (_WIN32_WINNT >= 0x0501):
43 // #define Z7_WIN_UISF_ACTIVE     0x4
44 
45 #endif
46 
47 #endif // UNDER_CE
48 
49 
50 #ifdef Z7_OLD_WIN_SDK
51 
52 // #define VK_OEM_1          0xBA   // ';:' for US
53 #define VK_OEM_PLUS       0xBB   // '+' any country
54 // #define VK_OEM_COMMA      0xBC   // ',' any country
55 #define VK_OEM_MINUS      0xBD   // '-' any country
56 // #define VK_OEM_PERIOD     0xBE   // '.' any country
57 // #define VK_OEM_2          0xBF   // '/?' for US
58 // #define VK_OEM_3          0xC0   // '`~' for US
59 
60 // #ifndef GWLP_USERDATA
61 #define GWLP_WNDPROC        (-4)
62 #define GWLP_USERDATA       (-21)
63 // #endif
64 #define DWLP_MSGRESULT  0
65 // #define DWLP_DLGPROC    DWLP_MSGRESULT + sizeof(LRESULT)
66 // #define DWLP_USER       DWLP_DLGPROC + sizeof(DLGPROC)
67 
68 #define BTNS_BUTTON     TBSTYLE_BUTTON      // 0x0000
69 
70 /*
71 vc6 defines INT_PTR via long:
72   typedef long INT_PTR, *PINT_PTR;
73   typedef unsigned long UINT_PTR, *PUINT_PTR;
74 but newer sdk (sdk2003+) defines INT_PTR via int:
75   typedef _W64 int INT_PTR, *PINT_PTR;
76   typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
77 */
78 
79 #define IS_INTRESOURCE(_r) (((ULONG_PTR)(_r) >> 16) == 0)
80 
81 #define GetWindowLongPtrA   GetWindowLongA
82 #define GetWindowLongPtrW   GetWindowLongW
83 #ifdef UNICODE
84 #define GetWindowLongPtr  GetWindowLongPtrW
85 #else
86 #define GetWindowLongPtr  GetWindowLongPtrA
87 #endif // !UNICODE
88 
89 #define SetWindowLongPtrA   SetWindowLongA
90 #define SetWindowLongPtrW   SetWindowLongW
91 #ifdef UNICODE
92 #define SetWindowLongPtr  SetWindowLongPtrW
93 #else
94 #define SetWindowLongPtr  SetWindowLongPtrA
95 #endif // !UNICODE
96 
97 #define ListView_SetCheckState(hwndLV, i, fCheck) \
98   ListView_SetItemState(hwndLV, i, INDEXTOSTATEIMAGEMASK((fCheck)?2:1), LVIS_STATEIMAGEMASK)
99 
100 #endif // Z7_OLD_WIN_SDK
101 
LRESULTToBool(LRESULT v)102 inline bool LRESULTToBool(LRESULT v) { return (v != FALSE); }
103 
104 #define MY_int_TO_WPARAM(i) ((WPARAM)(INT_PTR)(i))
105 
106 namespace NWindows {
107 
MyRegisterClass(CONST WNDCLASS * wndClass)108 inline ATOM MyRegisterClass(CONST WNDCLASS *wndClass)
109   { return ::RegisterClass(wndClass); }
110 
111 #ifndef _UNICODE
112 ATOM MyRegisterClass(CONST WNDCLASSW *wndClass);
113 #endif
114 
115 #ifdef _UNICODE
MySetWindowText(HWND wnd,LPCWSTR s)116 inline bool MySetWindowText(HWND wnd, LPCWSTR s) { return BOOLToBool(::SetWindowText(wnd, s)); }
117 #else
118 bool MySetWindowText(HWND wnd, LPCWSTR s);
119 #endif
120 
121 
122 #ifdef UNDER_CE
123 #define GWLP_USERDATA GWL_USERDATA
124 #define GWLP_WNDPROC GWL_WNDPROC
125 #define BTNS_BUTTON TBSTYLE_BUTTON
126 #define WC_COMBOBOXW L"ComboBox"
127 #define DWLP_MSGRESULT DWL_MSGRESULT
128 #endif
129 
130 class CWindow
131 {
Z7_CLASS_NO_COPY(CWindow)132   Z7_CLASS_NO_COPY(CWindow)
133 private:
134   // bool ModifyStyleBase(int styleOffset, DWORD remove, DWORD add, UINT flags);
135 protected:
136   HWND _window;
137 public:
138   CWindow(HWND newWindow = NULL): _window(newWindow) {}
139   CWindow& operator=(HWND newWindow)
140   {
141     _window = newWindow;
142     return *this;
143   }
HWND()144   operator HWND() const { return _window; }
Attach(HWND newWindow)145   void Attach(HWND newWindow) { _window = newWindow; }
Detach()146   HWND Detach()
147   {
148     HWND window = _window;
149     _window = NULL;
150     return window;
151   }
152 
Foreground()153   bool Foreground() { return BOOLToBool(::SetForegroundWindow(_window)); }
154 
GetParent()155   HWND GetParent() const { return ::GetParent(_window); }
GetWindowRect(LPRECT rect)156   bool GetWindowRect(LPRECT rect) const { return BOOLToBool(::GetWindowRect(_window,rect)); }
157   #ifndef UNDER_CE
IsZoomed()158   bool IsZoomed() const { return BOOLToBool(::IsZoomed(_window)); }
159   #endif
ClientToScreen(LPPOINT point)160   bool ClientToScreen(LPPOINT point) const { return BOOLToBool(::ClientToScreen(_window, point)); }
ScreenToClient(LPPOINT point)161   bool ScreenToClient(LPPOINT point) const { return BOOLToBool(::ScreenToClient(_window, point)); }
162 
CreateEx(DWORD exStyle,LPCTSTR className,LPCTSTR windowName,DWORD style,int x,int y,int width,int height,HWND parentWindow,HMENU idOrHMenu,HINSTANCE instance,LPVOID createParam)163   bool CreateEx(DWORD exStyle, LPCTSTR className,
164       LPCTSTR windowName, DWORD style,
165       int x, int y, int width, int height,
166       HWND parentWindow, HMENU idOrHMenu,
167       HINSTANCE instance, LPVOID createParam)
168   {
169     _window = ::CreateWindowEx(exStyle, className, windowName,
170       style, x, y, width, height, parentWindow,
171       idOrHMenu, instance, createParam);
172     return (_window != NULL);
173   }
174 
Create(LPCTSTR className,LPCTSTR windowName,DWORD style,int x,int y,int width,int height,HWND parentWindow,HMENU idOrHMenu,HINSTANCE instance,LPVOID createParam)175   bool Create(LPCTSTR className,
176       LPCTSTR windowName, DWORD style,
177       int x, int y, int width, int height,
178       HWND parentWindow, HMENU idOrHMenu,
179       HINSTANCE instance, LPVOID createParam)
180   {
181     _window = ::CreateWindow(className, windowName,
182       style, x, y, width, height, parentWindow,
183       idOrHMenu, instance, createParam);
184     return (_window != NULL);
185   }
186 
187   #ifndef _UNICODE
188   bool Create(LPCWSTR className,
189       LPCWSTR windowName, DWORD style,
190       int x, int y, int width, int height,
191       HWND parentWindow, HMENU idOrHMenu,
192       HINSTANCE instance, LPVOID createParam);
193   bool CreateEx(DWORD exStyle, LPCWSTR className,
194       LPCWSTR windowName, DWORD style,
195       int x, int y, int width, int height,
196       HWND parentWindow, HMENU idOrHMenu,
197       HINSTANCE instance, LPVOID createParam);
198   #endif
199 
200 
Destroy()201   bool Destroy()
202   {
203     if (_window == NULL)
204       return true;
205     bool result = BOOLToBool(::DestroyWindow(_window));
206     if (result)
207       _window = NULL;
208     return result;
209   }
IsWindow()210   bool IsWindow() {  return BOOLToBool(::IsWindow(_window)); }
211   bool Move(int x, int y, int width, int height, bool repaint = true)
212     { return BOOLToBool(::MoveWindow(_window, x, y, width, height, BoolToBOOL(repaint))); }
213 
ChangeSubWindowSizeX(HWND hwnd,int xSize)214   bool ChangeSubWindowSizeX(HWND hwnd, int xSize)
215   {
216     RECT rect;
217     ::GetWindowRect(hwnd, &rect);
218     POINT p1;
219     p1.x = rect.left;
220     p1.y = rect.top;
221     ScreenToClient(&p1);
222     return BOOLToBool(::MoveWindow(hwnd, p1.x, p1.y, xSize, rect.bottom - rect.top, TRUE));
223   }
224 
ScreenToClient(RECT * rect)225   void ScreenToClient(RECT *rect)
226   {
227     POINT p1, p2;
228     p1.x = rect->left;
229     p1.y = rect->top;
230     p2.x = rect->right;
231     p2.y = rect->bottom;
232     ScreenToClient(&p1);
233     ScreenToClient(&p2);
234 
235     rect->left = p1.x;
236     rect->top = p1.y;
237     rect->right = p2.x;
238     rect->bottom = p2.y;
239   }
240 
GetClientRect(LPRECT rect)241   bool GetClientRect(LPRECT rect) { return BOOLToBool(::GetClientRect(_window, rect)); }
Show(int cmdShow)242   bool Show(int cmdShow) { return BOOLToBool(::ShowWindow(_window, cmdShow)); }
Show_Bool(bool show)243   bool Show_Bool(bool show) { return Show(show ? SW_SHOW: SW_HIDE); }
244 
245   #ifndef UNDER_CE
SetPlacement(CONST WINDOWPLACEMENT * placement)246   bool SetPlacement(CONST WINDOWPLACEMENT *placement) { return BOOLToBool(::SetWindowPlacement(_window, placement)); }
GetPlacement(WINDOWPLACEMENT * placement)247   bool GetPlacement(WINDOWPLACEMENT *placement) { return BOOLToBool(::GetWindowPlacement(_window, placement)); }
248   #endif
Update()249   bool Update() { return BOOLToBool(::UpdateWindow(_window)); }
250   bool InvalidateRect(LPCRECT rect, bool backgroundErase = true)
251     { return BOOLToBool(::InvalidateRect(_window, rect, BoolToBOOL(backgroundErase))); }
252   void SetRedraw(bool redraw = true) { SendMsg(WM_SETREDRAW, (WPARAM)BoolToBOOL(redraw), 0); }
253 
SetStyle(LONG_PTR style)254   LONG_PTR SetStyle(LONG_PTR style) { return SetLongPtr(GWL_STYLE, style); }
255   // LONG_PTR SetStyle(DWORD style) { return SetLongPtr(GWL_STYLE, (LONG_PTR)style); }
GetStyle()256   LONG_PTR GetStyle() const { return GetLongPtr(GWL_STYLE); }
257   // bool MyIsMaximized() const { return ((GetStyle() & WS_MAXIMIZE) != 0); }
258 
SetLong(int index,LONG newLongPtr)259   LONG_PTR SetLong(int index, LONG newLongPtr) { return ::SetWindowLong(_window, index, newLongPtr); }
GetLong(int index)260   LONG_PTR GetLong(int index) const { return ::GetWindowLong(_window, index); }
SetUserDataLong(LONG newLongPtr)261   LONG_PTR SetUserDataLong(LONG newLongPtr) { return SetLong(GWLP_USERDATA, newLongPtr); }
GetUserDataLong()262   LONG_PTR GetUserDataLong() const { return GetLong(GWLP_USERDATA); }
263 
264 
265   #ifdef UNDER_CE
266 
SetLongPtr(int index,LONG_PTR newLongPtr)267   LONG_PTR SetLongPtr(int index, LONG_PTR newLongPtr) { return SetLong(index, newLongPtr); }
GetLongPtr(int index)268   LONG_PTR GetLongPtr(int index) const { return GetLong(index); }
269 
SetUserDataLongPtr(LONG_PTR newLongPtr)270   LONG_PTR SetUserDataLongPtr(LONG_PTR newLongPtr) { return SetUserDataLong(newLongPtr); }
GetUserDataLongPtr()271   LONG_PTR GetUserDataLongPtr() const { return GetUserDataLong(); }
272 
273   #else
274 
SetLongPtr(int index,LONG_PTR newLongPtr)275   LONG_PTR SetLongPtr(int index, LONG_PTR newLongPtr)
276     { return ::SetWindowLongPtr(_window, index,
277           #ifndef _WIN64
278           (LONG)
279           #endif
280           newLongPtr); }
281   #ifndef _UNICODE
SetLongPtrW(int index,LONG_PTR newLongPtr)282   LONG_PTR SetLongPtrW(int index, LONG_PTR newLongPtr)
283     { return ::SetWindowLongPtrW(_window, index,
284           #ifndef _WIN64
285           (LONG)
286           #endif
287           newLongPtr); }
288   #endif
289 
GetLongPtr(int index)290   LONG_PTR GetLongPtr(int index) const { return ::GetWindowLongPtr(_window, index); }
SetUserDataLongPtr(LONG_PTR newLongPtr)291   LONG_PTR SetUserDataLongPtr(LONG_PTR newLongPtr) { return SetLongPtr(GWLP_USERDATA, newLongPtr); }
GetUserDataLongPtr()292   LONG_PTR GetUserDataLongPtr() const { return GetLongPtr(GWLP_USERDATA); }
293 
294   #endif
295 
296   /*
297   bool ModifyStyle(HWND hWnd, DWORD remove, DWORD add, UINT flags = 0)
298     {  return ModifyStyleBase(GWL_STYLE, remove, add, flags); }
299   bool ModifyStyleEx(HWND hWnd, DWORD remove, DWORD add, UINT flags = 0)
300     { return ModifyStyleBase(GWL_EXSTYLE, remove, add, flags); }
301   */
302 
SetFocus()303   HWND SetFocus() { return ::SetFocus(_window); }
304 
305   LRESULT SendMsg(UINT message, WPARAM wParam = 0, LPARAM lParam = 0)
306     { return ::SendMessage(_window, message, wParam, lParam); }
307   #ifndef _UNICODE
308   LRESULT SendMsgW(UINT message, WPARAM wParam = 0, LPARAM lParam = 0)
309     { return ::SendMessageW(_window, message, wParam, lParam); }
310   #endif
311 
312   bool PostMsg(UINT message, WPARAM wParam = 0, LPARAM lParam = 0)
313     { return BOOLToBool(::PostMessage(_window, message, wParam, lParam)); }
314   #ifndef _UNICODE
315   bool PostMsgW(UINT message, WPARAM wParam = 0, LPARAM lParam = 0)
316     { return BOOLToBool(::PostMessageW(_window, message, wParam, lParam)); }
317   #endif
318 
SetText(LPCTSTR s)319   bool SetText(LPCTSTR s) { return BOOLToBool(::SetWindowText(_window, s)); }
320   #ifndef _UNICODE
SetText(LPCWSTR s)321   bool SetText(LPCWSTR s) { return MySetWindowText(_window, s); }
322   #endif
323 
GetTextLength()324   int GetTextLength() const
325     { return GetWindowTextLength(_window); }
GetText(LPTSTR string,int maxCount)326   int GetText(LPTSTR string, int maxCount) const
327     { return GetWindowText(_window, string, maxCount); }
328   bool GetText(CSysString &s) const;
329   #ifndef _UNICODE
330   /*
331   UINT GetText(LPWSTR string, int maxCount) const
332     { return GetWindowTextW(_window, string, maxCount); }
333   */
334   bool GetText(UString &s) const;
335   #endif
336 
Enable(bool enable)337   bool Enable(bool enable)
338     { return BOOLToBool(::EnableWindow(_window, BoolToBOOL(enable))); }
339 
IsEnabled()340   bool IsEnabled() const
341     { return BOOLToBool(::IsWindowEnabled(_window)); }
342 
343   #ifndef UNDER_CE
GetSystemMenu(bool revert)344   HMENU GetSystemMenu(bool revert)
345     { return ::GetSystemMenu(_window, BoolToBOOL(revert)); }
346   #endif
347 
348   UINT_PTR SetTimer(UINT_PTR idEvent, UINT elapse, TIMERPROC timerFunc = NULL)
349     { return ::SetTimer(_window, idEvent, elapse, timerFunc); }
KillTimer(UINT_PTR idEvent)350   bool KillTimer(UINT_PTR idEvent)
351     {return BOOLToBool(::KillTimer(_window, idEvent)); }
352 
SetIcon(WPARAM sizeType,HICON icon)353   HICON SetIcon(WPARAM sizeType, HICON icon) { return (HICON)SendMsg(WM_SETICON, sizeType, (LPARAM)icon); }
354 };
355 
356 #define RECT_SIZE_X(r) ((r).right - (r).left)
357 #define RECT_SIZE_Y(r) ((r).bottom - (r).top)
358 
IsKeyDown(int virtKey)359 inline bool IsKeyDown(int virtKey) { return (::GetKeyState(virtKey) & 0x8000) != 0; }
360 
361 }
362 
363 #endif
364