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/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 
     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
     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:
     59   CWindow(HWND newWindow = NULL): _window(newWindow){};
     60   CWindow& operator=(HWND newWindow)
     61   {
     62     _window = newWindow;
     63     return *this;
     64   }
     65   operator HWND() const { return _window; }
     66   void Attach(HWND newWindow) { _window = newWindow; }
     67   HWND Detach()
     68   {
     69     HWND window = _window;
     70     _window = NULL;
     71     return window;
     72   }
     73 
     74   bool Foreground() { return BOOLToBool(::SetForegroundWindow(_window)); }
     75 
     76   HWND GetParent() const { return ::GetParent(_window); }
     77   bool GetWindowRect(LPRECT rect) const { return BOOLToBool(::GetWindowRect(_window,rect)); }
     78   #ifndef UNDER_CE
     79   bool IsZoomed() const { return BOOLToBool(::IsZoomed(_window)); }
     80   #endif
     81   bool ClientToScreen(LPPOINT point) const { return BOOLToBool(::ClientToScreen(_window, point)); }
     82   bool ScreenToClient(LPPOINT point) const { return BOOLToBool(::ScreenToClient(_window, point)); }
     83 
     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 
     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 
    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   }
    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 
    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 
    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 
    162   bool GetClientRect(LPRECT rect) { return BOOLToBool(::GetClientRect(_window, rect)); }
    163   bool Show(int cmdShow) { return BOOLToBool(::ShowWindow(_window, cmdShow)); }
    164   bool Show_Bool(bool show) { return Show(show ? SW_SHOW: SW_HIDE); }
    165 
    166   #ifndef UNDER_CE
    167   bool SetPlacement(CONST WINDOWPLACEMENT *placement) { return BOOLToBool(::SetWindowPlacement(_window, placement)); }
    168   bool GetPlacement(WINDOWPLACEMENT *placement) { return BOOLToBool(::GetWindowPlacement(_window, placement)); }
    169   #endif
    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 
    175   LONG_PTR SetStyle(LONG_PTR style) { return SetLongPtr(GWL_STYLE, style); }
    176   LONG_PTR GetStyle() const { return GetLongPtr(GWL_STYLE); }
    177   // bool MyIsMaximized() const { return ((GetStyle() & WS_MAXIMIZE) != 0); }
    178 
    179   LONG_PTR SetLong(int index, LONG newLongPtr) { return ::SetWindowLong(_window, index, newLongPtr); }
    180   LONG_PTR GetLong(int index) const { return ::GetWindowLong(_window, index); }
    181   LONG_PTR SetUserDataLong(LONG newLongPtr) { return SetLong(GWLP_USERDATA, newLongPtr); }
    182   LONG_PTR GetUserDataLong() const { return GetLong(GWLP_USERDATA); }
    183 
    184 
    185   #ifdef UNDER_CE
    186 
    187   LONG_PTR SetLongPtr(int index, LONG_PTR newLongPtr) { return SetLong(index, newLongPtr); }
    188   LONG_PTR GetLongPtr(int index) const { return GetLong(index); }
    189 
    190   LONG_PTR SetUserDataLongPtr(LONG_PTR newLongPtr) { return SetUserDataLong(newLongPtr); }
    191   LONG_PTR GetUserDataLongPtr() const { return GetUserDataLong(); }
    192 
    193   #else
    194 
    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
    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 
    210   LONG_PTR GetLongPtr(int index) const { return ::GetWindowLongPtr(_window, index); }
    211   LONG_PTR SetUserDataLongPtr(LONG_PTR newLongPtr) { return SetLongPtr(GWLP_USERDATA, newLongPtr); }
    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 
    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 
    239   bool SetText(LPCTSTR s) { return BOOLToBool(::SetWindowText(_window, s)); }
    240   #ifndef _UNICODE
    241   bool SetText(LPCWSTR s) { return MySetWindowText(_window, s); }
    242   #endif
    243 
    244   int GetTextLength() const
    245     { return GetWindowTextLength(_window); }
    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 
    257   bool Enable(bool enable)
    258     { return BOOLToBool(::EnableWindow(_window, BoolToBOOL(enable))); }
    259 
    260   bool IsEnabled()
    261     { return BOOLToBool(::IsWindowEnabled(_window)); }
    262 
    263   #ifndef UNDER_CE
    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); }
    270   bool KillTimer(UINT_PTR idEvent)
    271     {return BOOLToBool(::KillTimer(_window, idEvent)); }
    272 
    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 
    279 inline bool IsKeyDown(int virtKey) { return (::GetKeyState(virtKey) & 0x8000) != 0; }
    280 
    281 }
    282 
    283 #endif
    284