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