1 // Windows/Window.h
2 
3 #ifndef __WINDOWS_WINDOW_H
4 #define __WINDOWS_WINDOW_H
5 
6 #include "../Common/MyString.h"
7 
8 #include "Defs.h"
9 
10 #ifndef UNDER_CE
11 
12 #define MY__WM_CHANGEUISTATE  0x0127
13 #define MY__WM_UPDATEUISTATE  0x0128
14 #define MY__WM_QUERYUISTATE   0x0129
15 
16 // LOWORD(wParam) values in WM_*UISTATE
17 #define MY__UIS_SET         1
18 #define MY__UIS_CLEAR       2
19 #define MY__UIS_INITIALIZE  3
20 
21 // HIWORD(wParam) values in WM_*UISTATE
22 #define MY__UISF_HIDEFOCUS  0x1
23 #define MY__UISF_HIDEACCEL  0x2
24 #define MY__UISF_ACTIVE     0x4
25 
26 #endif
27 
28 namespace NWindows {
29 
MyRegisterClass(CONST WNDCLASS * wndClass)30 inline ATOM MyRegisterClass(CONST WNDCLASS *wndClass)
31   { return ::RegisterClass(wndClass); }
32 
33 #ifndef _UNICODE
34 ATOM MyRegisterClass(CONST WNDCLASSW *wndClass);
35 #endif
36 
37 #ifdef _UNICODE
MySetWindowText(HWND wnd,LPCWSTR s)38 inline bool MySetWindowText(HWND wnd, LPCWSTR s) { return BOOLToBool(::SetWindowText(wnd, s)); }
39 #else
40 bool MySetWindowText(HWND wnd, LPCWSTR s);
41 #endif
42 
43 
44 #ifdef UNDER_CE
45 #define GWLP_USERDATA GWL_USERDATA
46 #define GWLP_WNDPROC GWL_WNDPROC
47 #define BTNS_BUTTON TBSTYLE_BUTTON
48 #define WC_COMBOBOXW L"ComboBox"
49 #define DWLP_MSGRESULT DWL_MSGRESULT
50 #endif
51 
52 class CWindow
53 {
54 private:
55    // bool ModifyStyleBase(int styleOffset, DWORD remove, DWORD add, UINT flags);
56 protected:
57   HWND _window;
58 public:
_window(newWindow)59   CWindow(HWND newWindow = NULL): _window(newWindow){};
60   CWindow& operator=(HWND newWindow)
61   {
62     _window = newWindow;
63     return *this;
64   }
HWND()65   operator HWND() const { return _window; }
Attach(HWND newWindow)66   void Attach(HWND newWindow) { _window = newWindow; }
Detach()67   HWND Detach()
68   {
69     HWND window = _window;
70     _window = NULL;
71     return window;
72   }
73 
Foreground()74   bool Foreground() { return BOOLToBool(::SetForegroundWindow(_window)); }
75 
GetParent()76   HWND GetParent() const { return ::GetParent(_window); }
GetWindowRect(LPRECT rect)77   bool GetWindowRect(LPRECT rect) const { return BOOLToBool(::GetWindowRect(_window,rect)); }
78   #ifndef UNDER_CE
IsZoomed()79   bool IsZoomed() const { return BOOLToBool(::IsZoomed(_window)); }
80   #endif
ClientToScreen(LPPOINT point)81   bool ClientToScreen(LPPOINT point) const { return BOOLToBool(::ClientToScreen(_window, point)); }
ScreenToClient(LPPOINT point)82   bool ScreenToClient(LPPOINT point) const { return BOOLToBool(::ScreenToClient(_window, point)); }
83 
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)84   bool CreateEx(DWORD exStyle, LPCTSTR className,
85       LPCTSTR windowName, DWORD style,
86       int x, int y, int width, int height,
87       HWND parentWindow, HMENU idOrHMenu,
88       HINSTANCE instance, LPVOID createParam)
89   {
90     _window = ::CreateWindowEx(exStyle, className, windowName,
91       style, x, y, width, height, parentWindow,
92       idOrHMenu, instance, createParam);
93     return (_window != NULL);
94   }
95 
Create(LPCTSTR className,LPCTSTR windowName,DWORD style,int x,int y,int width,int height,HWND parentWindow,HMENU idOrHMenu,HINSTANCE instance,LPVOID createParam)96   bool Create(LPCTSTR className,
97       LPCTSTR windowName, DWORD style,
98       int x, int y, int width, int height,
99       HWND parentWindow, HMENU idOrHMenu,
100       HINSTANCE instance, LPVOID createParam)
101   {
102     _window = ::CreateWindow(className, windowName,
103       style, x, y, width, height, parentWindow,
104       idOrHMenu, instance, createParam);
105     return (_window != NULL);
106   }
107 
108   #ifndef _UNICODE
109   bool Create(LPCWSTR className,
110       LPCWSTR windowName, DWORD style,
111       int x, int y, int width, int height,
112       HWND parentWindow, HMENU idOrHMenu,
113       HINSTANCE instance, LPVOID createParam);
114   bool CreateEx(DWORD exStyle, LPCWSTR className,
115       LPCWSTR windowName, DWORD style,
116       int x, int y, int width, int height,
117       HWND parentWindow, HMENU idOrHMenu,
118       HINSTANCE instance, LPVOID createParam);
119   #endif
120 
121 
Destroy()122   bool Destroy()
123   {
124     if (_window == NULL)
125       return true;
126     bool result = BOOLToBool(::DestroyWindow(_window));
127     if (result)
128       _window = NULL;
129     return result;
130   }
IsWindow()131   bool IsWindow() {  return BOOLToBool(::IsWindow(_window)); }
132   bool Move(int x, int y, int width, int height, bool repaint = true)
133     { return BOOLToBool(::MoveWindow(_window, x, y, width, height, BoolToBOOL(repaint))); }
134 
ChangeSubWindowSizeX(HWND hwnd,int xSize)135   bool ChangeSubWindowSizeX(HWND hwnd, int xSize)
136   {
137     RECT rect;
138     ::GetWindowRect(hwnd, &rect);
139     POINT p1;
140     p1.x = rect.left;
141     p1.y = rect.top;
142     ScreenToClient(&p1);
143     return BOOLToBool(::MoveWindow(hwnd, p1.x, p1.y, xSize, rect.bottom - rect.top, TRUE));
144   }
145 
ScreenToClient(RECT * rect)146   void ScreenToClient(RECT *rect)
147   {
148     POINT p1, p2;
149     p1.x = rect->left;
150     p1.y = rect->top;
151     p2.x = rect->right;
152     p2.y = rect->bottom;
153     ScreenToClient(&p1);
154     ScreenToClient(&p2);
155 
156     rect->left = p1.x;
157     rect->top = p1.y;
158     rect->right = p2.x;
159     rect->bottom = p2.y;
160   }
161 
GetClientRect(LPRECT rect)162   bool GetClientRect(LPRECT rect) { return BOOLToBool(::GetClientRect(_window, rect)); }
Show(int cmdShow)163   bool Show(int cmdShow) { return BOOLToBool(::ShowWindow(_window, cmdShow)); }
Show_Bool(bool show)164   bool Show_Bool(bool show) { return Show(show ? SW_SHOW: SW_HIDE); }
165 
166   #ifndef UNDER_CE
SetPlacement(CONST WINDOWPLACEMENT * placement)167   bool SetPlacement(CONST WINDOWPLACEMENT *placement) { return BOOLToBool(::SetWindowPlacement(_window, placement)); }
GetPlacement(WINDOWPLACEMENT * placement)168   bool GetPlacement(WINDOWPLACEMENT *placement) { return BOOLToBool(::GetWindowPlacement(_window, placement)); }
169   #endif
Update()170   bool Update() { return BOOLToBool(::UpdateWindow(_window)); }
171   bool InvalidateRect(LPCRECT rect, bool backgroundErase = true)
172     { return BOOLToBool(::InvalidateRect(_window, rect, BoolToBOOL(backgroundErase))); }
173   void SetRedraw(bool redraw = true) { SendMessage(WM_SETREDRAW, BoolToBOOL(redraw), 0); }
174 
SetStyle(LONG_PTR style)175   LONG_PTR SetStyle(LONG_PTR style) { return SetLongPtr(GWL_STYLE, style); }
GetStyle()176   LONG_PTR GetStyle() const { return GetLongPtr(GWL_STYLE); }
177   // bool MyIsMaximized() const { return ((GetStyle() & WS_MAXIMIZE) != 0); }
178 
SetLong(int index,LONG newLongPtr)179   LONG_PTR SetLong(int index, LONG newLongPtr) { return ::SetWindowLong(_window, index, newLongPtr); }
GetLong(int index)180   LONG_PTR GetLong(int index) const { return ::GetWindowLong(_window, index); }
SetUserDataLong(LONG newLongPtr)181   LONG_PTR SetUserDataLong(LONG newLongPtr) { return SetLong(GWLP_USERDATA, newLongPtr); }
GetUserDataLong()182   LONG_PTR GetUserDataLong() const { return GetLong(GWLP_USERDATA); }
183 
184 
185   #ifdef UNDER_CE
186 
SetLongPtr(int index,LONG_PTR newLongPtr)187   LONG_PTR SetLongPtr(int index, LONG_PTR newLongPtr) { return SetLong(index, newLongPtr); }
GetLongPtr(int index)188   LONG_PTR GetLongPtr(int index) const { return GetLong(index); }
189 
SetUserDataLongPtr(LONG_PTR newLongPtr)190   LONG_PTR SetUserDataLongPtr(LONG_PTR newLongPtr) { return SetUserDataLong(newLongPtr); }
GetUserDataLongPtr()191   LONG_PTR GetUserDataLongPtr() const { return GetUserDataLong(); }
192 
193   #else
194 
SetLongPtr(int index,LONG_PTR newLongPtr)195   LONG_PTR SetLongPtr(int index, LONG_PTR newLongPtr)
196     { return ::SetWindowLongPtr(_window, index,
197           #ifndef _WIN64
198           (LONG)
199           #endif
200           newLongPtr); }
201   #ifndef _UNICODE
SetLongPtrW(int index,LONG_PTR newLongPtr)202   LONG_PTR SetLongPtrW(int index, LONG_PTR newLongPtr)
203     { return ::SetWindowLongPtrW(_window, index,
204           #ifndef _WIN64
205           (LONG)
206           #endif
207           newLongPtr); }
208   #endif
209 
GetLongPtr(int index)210   LONG_PTR GetLongPtr(int index) const { return ::GetWindowLongPtr(_window, index); }
SetUserDataLongPtr(LONG_PTR newLongPtr)211   LONG_PTR SetUserDataLongPtr(LONG_PTR newLongPtr) { return SetLongPtr(GWLP_USERDATA, newLongPtr); }
GetUserDataLongPtr()212   LONG_PTR GetUserDataLongPtr() const { return GetLongPtr(GWLP_USERDATA); }
213 
214   #endif
215 
216   /*
217   bool ModifyStyle(HWND hWnd, DWORD remove, DWORD add, UINT flags = 0)
218     {  return ModifyStyleBase(GWL_STYLE, remove, add, flags); }
219   bool ModifyStyleEx(HWND hWnd, DWORD remove, DWORD add, UINT flags = 0)
220     { return ModifyStyleBase(GWL_EXSTYLE, remove, add, flags); }
221   */
222 
SetFocus()223   HWND SetFocus() { return ::SetFocus(_window); }
224 
225   LRESULT SendMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0)
226     { return ::SendMessage(_window, message, wParam, lParam) ;}
227   #ifndef _UNICODE
228   LRESULT SendMessageW(UINT message, WPARAM wParam = 0, LPARAM lParam = 0)
229     { return ::SendMessageW(_window, message, wParam, lParam) ;}
230   #endif
231 
232   bool PostMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0)
233     {  return BOOLToBool(::PostMessage(_window, message, wParam, lParam)) ;}
234   #ifndef _UNICODE
235   LRESULT PostMessageW(UINT message, WPARAM wParam = 0, LPARAM lParam = 0)
236     { return ::PostMessageW(_window, message, wParam, lParam) ;}
237   #endif
238 
SetText(LPCTSTR s)239   bool SetText(LPCTSTR s) { return BOOLToBool(::SetWindowText(_window, s)); }
240   #ifndef _UNICODE
SetText(LPCWSTR s)241   bool SetText(LPCWSTR s) { return MySetWindowText(_window, s); }
242   #endif
243 
GetTextLength()244   int GetTextLength() const
245     { return GetWindowTextLength(_window); }
GetText(LPTSTR string,int maxCount)246   UINT GetText(LPTSTR string, int maxCount) const
247     { return GetWindowText(_window, string, maxCount); }
248   bool GetText(CSysString &s);
249   #ifndef _UNICODE
250   /*
251   UINT GetText(LPWSTR string, int maxCount) const
252     { return GetWindowTextW(_window, string, maxCount); }
253   */
254   bool GetText(UString &s);
255   #endif
256 
Enable(bool enable)257   bool Enable(bool enable)
258     { return BOOLToBool(::EnableWindow(_window, BoolToBOOL(enable))); }
259 
IsEnabled()260   bool IsEnabled()
261     { return BOOLToBool(::IsWindowEnabled(_window)); }
262 
263   #ifndef UNDER_CE
GetSystemMenu(bool revert)264   HMENU GetSystemMenu(bool revert)
265     { return ::GetSystemMenu(_window, BoolToBOOL(revert)); }
266   #endif
267 
268   UINT_PTR SetTimer(UINT_PTR idEvent, UINT elapse, TIMERPROC timerFunc = 0)
269     { return ::SetTimer(_window, idEvent, elapse, timerFunc); }
KillTimer(UINT_PTR idEvent)270   bool KillTimer(UINT_PTR idEvent)
271     {return BOOLToBool(::KillTimer(_window, idEvent)); }
272 
SetIcon(WPARAM sizeType,HICON icon)273   HICON SetIcon(WPARAM sizeType, HICON icon) { return (HICON)SendMessage(WM_SETICON, sizeType, (LPARAM)icon); }
274 };
275 
276 #define RECT_SIZE_X(r) ((r).right - (r).left)
277 #define RECT_SIZE_Y(r) ((r).bottom - (r).top)
278 
IsKeyDown(int virtKey)279 inline bool IsKeyDown(int virtKey) { return (::GetKeyState(virtKey) & 0x8000) != 0; }
280 
281 }
282 
283 #endif
284