Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cWndHandle.h
Go to the documentation of this file.
1 //
6 
7 #ifndef _INC_cWndHandle_H
8 #define _INC_cWndHandle_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 #include "../WinAPI/WinTypes.h"
13 #include "../WinAPI/cClipboard.h"
14 #include "../WinAPI/cWinResource.h"
15 #include "../Geometry/cRectI.h"
20 
21 namespace GrayLib
22 {
23  typedef UINT DLGID_t;
24 
25  class GRAYLIB_LINK cWndHandle : public CObject // similar to Std MFC class CWnd
26  {
32 
33  typedef CObject SUPER_t;
34 
35  protected:
36  HWND m_hWnd;
37 
38  public:
39  cWndHandle(HWND hWnd = WINHANDLE_NULL) noexcept
40  : m_hWnd(hWnd)
41  {
42  }
43  cWndHandle(cWndHandle* pWnd) noexcept
44  : m_hWnd(pWnd->GetSafeHwnd())
45  {
46  }
47 #ifdef _WIN32
48  cWndHandle(cWndHandle* pWndParent, DLGID_t id)
49  : m_hWnd(pWndParent->GetDlgItem(id))
50  {
51  ASSERT(pWndParent != nullptr);
52  }
53 #endif
54 #ifdef _MFC_VER
55  cWndHandle(CWnd* pWnd)
56  : m_hWnd(pWnd->GetSafeHwnd())
57  {
58  }
59 #endif
60  virtual ~cWndHandle()
61  {
63  }
64 
65  bool isValidHwnd() const noexcept
66  {
68 #ifdef _WIN32
69  return ::IsWindow(m_hWnd); // How expensive is this?
70 #else
71  return(m_hWnd != WINHANDLE_NULL);
72 #endif
73  }
74  virtual bool isValidCheck() const noexcept // override
75  {
77 #ifndef _MFC_VER
78  if (!SUPER_t::isValidCheck())
79  return false;
80 #endif
81  if (!IS_TYPE_OF(cWndHandle, this)) // structure definitions are valid.
82  return false;
83  if (m_hWnd == WINHANDLE_NULL)
84  return true;
85  return isValidHwnd();
86  }
87 
88  operator HWND() const noexcept // cast as a HWND
89  {
90  DEBUG_CHECK(isValidCheck());
91  return m_hWnd;
92  }
93  HWND get_Hwnd() const noexcept
94  {
95  DEBUG_CHECK(isValidCheck());
96  return m_hWnd;
97  }
98  HWND GetSafeHwnd() const noexcept
99  {
101  if (!cMem::IsValidApp(this))
102  return WINHANDLE_NULL;
103  return m_hWnd;
104  }
105  bool AttachHwnd(HWND hWnd)
106  {
108  m_hWnd = hWnd;
109  return isValidHwnd();
110  }
111  void DetachHwnd()
112  {
114  m_hWnd = WINHANDLE_NULL;
115  }
116 
117 #ifdef _WIN32
118 
119  bool CreateWindowX(DWORD dwExStyle, const GChar_t* pszClassName, const GChar_t* pszText,
120  DWORD dwStyle, int x, int y, int iSizeX, int iSizeY,
121  HWND hWndParent, HMENU hMenuOrChildId = WINHANDLE_NULL)
122  {
126  return AttachHwnd(_GTN(::CreateWindowEx)(dwExStyle, pszClassName, pszText,
127  dwStyle, x, y, iSizeX, iSizeY,
128  hWndParent, hMenuOrChildId, cAppState::get_HModule(), this));
129  }
130 
131  // Standard message handlers.
132  virtual BOOL OnCreate(HWND hwnd, _GTN(CREATESTRUCT)* lpCreateStruct = nullptr)
133  {
136  UNREFERENCED_PARAMETER(lpCreateStruct);
137  ASSERT_VALID(this);
138  if (!this->AttachHwnd(hwnd))
139  return false;
140  this->put_WindowLongPtr(GWLP_USERDATA, (LONG_PTR)this); // GWL_USERDATA
141  return true;
142  }
143  virtual void OnDestroy()
144  {
145  // Called internally. WM_DESTROY.
146  // DON't call WM_DESTROY via HANDLE_MSG(m_hWnd, WM_DESTROY, OnDestroy)
147  DetachHwnd();
148  }
149 
150  // Basic window functions.
151  BOOL IsWindow(void) const
152  {
154  if (!cMem::IsValidApp(this))
155  return false;
156  ASSERT_VALID(this);
157  if (!isValidHwnd())
158  return false;
159  return ::IsWindow(m_hWnd); // Ask OS if this is truly valid HWND.
160  }
161  HWND GetParent() const
162  {
163  ASSERT_VALID(this);
164  ASSERT(isValidHwnd());
165  return ::GetParent(m_hWnd);
166  }
167  BOOL IsChild(HWND hWndChild) const
168  {
170  ASSERT_VALID(this);
171  ASSERT(isValidHwnd());
172  return ::IsChild(m_hWnd, hWndChild);
173  }
174 
175  LRESULT SendMessageX(WINMSG_t uMsg, WPARAM wParam = 0, LPARAM lParam = 0) const
176  {
179  ASSERT_VALID(this);
180  ASSERT(isValidHwnd());
181  return _GTN(::SendMessage)(m_hWnd, uMsg, wParam, lParam);
182  }
183  BOOL PostMessageX(WINMSG_t uMsg, WPARAM wParam = 0, LPARAM lParam = 0) const
184  {
187  ASSERT_VALID(this);
188  ASSERT(isValidHwnd());
189  return _GTN(::PostMessage)(m_hWnd, uMsg, wParam, lParam);
190  }
191 
192  // Create/Destroy
193  void DestroyWindow(void)
194  {
195  if (!isValidHwnd())
196  return;
197  ::DestroyWindow(m_hWnd);
198  DetachHwnd();
199  }
200  void CloseWindow()
201  {
202 #ifdef UNDER_CE
203  ::DestroyWindow(m_hWnd);
204 #else
205  ::CloseWindow(m_hWnd);
206 #endif
207  }
208 
209  // Area and location
210  bool GetClientRect(RECT& rcClient) const
211  {
215  ASSERT(isValidHwnd());
216  return ::GetClientRect(m_hWnd, &rcClient);
217  }
218  bool GetWindowRect(RECT& rcWnd) const
219  {
224 
225  ASSERT(isValidHwnd());
226  return ::GetWindowRect(m_hWnd, &rcWnd);
227  }
228  cRectI get_ClientRect() const
229  {
232  cRectI rect;
233  GetClientRect(rect);
234  return rect;
235  }
236  cRectI get_WindowRect() const
237  {
241 
242  cRectI rect;
243  GetWindowRect(rect);
244  return rect;
245  }
246 
247  BOOL GetWindowPlacement(::WINDOWPLACEMENT* lpwndpl) const
248  {
249  // Like MFC. cWindowPlacement.
250  return ::GetWindowPlacement(m_hWnd, lpwndpl);
251  }
252 
253  BOOL SetWindowPlacement(const ::WINDOWPLACEMENT* lpwndpl)
254  {
255  // Like MFC.
256  return ::SetWindowPlacement(m_hWnd, lpwndpl);
257  }
258 
259  BOOL MoveWindow(int X, int Y, int nWidth, int nHeight, bool bRepaint = true)
260  {
261  ASSERT(isValidHwnd());
262  return(::MoveWindow(m_hWnd, X, Y, nWidth, nHeight, bRepaint));
263  }
264  BOOL SetWindowPos(HWND hWndAfter, int x, int y, int cx, int cy, UINT nFlags)
265  {
268  ASSERT(isValidHwnd());
269  return(::SetWindowPos(m_hWnd, hWndAfter, x, y, cx, cy, nFlags));
270  }
271 
272  THREADID_t get_WindowThread() const
273  {
276  return ::GetWindowThreadProcessId(m_hWnd, nullptr);
277  }
278 
279  BOOL SetForegroundWindow()
280  {
284  ASSERT_VALID(this);
285  ASSERT(isValidHwnd());
286  return(::SetForegroundWindow(m_hWnd));
287  }
288  HWND SetActiveWindow()
289  {
293  ASSERT_VALID(this);
294  ASSERT(isValidHwnd());
295  return(::SetActiveWindow(m_hWnd));
296  }
297  HWND SetFocus()
298  {
303  ASSERT_VALID(this);
304  ASSERT(isValidHwnd());
305  return(::SetFocus(m_hWnd));
306  }
307  void ClientToScreen(POINT* pPoint) const
308  {
310  ASSERT_VALID(this);
311  ::ClientToScreen(m_hWnd, (POINT*)pPoint);
312  }
313  void ClientToScreen(RECT* pRect) const
314  {
316  ClientToScreen((POINT*)&(pRect->left));
317  ClientToScreen((POINT*)&(pRect->right));
318  }
319  void ScreenToClient(POINT* pPoint) const
320  {
322  ASSERT_VALID(this);
323  ::ScreenToClient(m_hWnd, (POINT*)pPoint);
324  }
325  void ScreenToClient(RECT* pRect) const
326  {
328  ScreenToClient((POINT*)&(pRect->left));
329  ScreenToClient((POINT*)&(pRect->right));
330  }
331  bool IsWindowVisible(void) const
332  {
333  return ::IsWindowVisible(m_hWnd);
334  }
335  bool IsZoomed(void) const
336  {
338 #ifdef UNDER_CE
339  return IsWindowVisible();
340 #else
341  return ::IsZoomed(m_hWnd);
342 #endif
343  }
344  bool IsIconic(void) const
345  {
347 #ifdef UNDER_CE
348  return false;
349 #else
350  return ::IsIconic(m_hWnd);
351 #endif
352  }
353  bool ShowWindow(SHOWWINDOW_t nCmdShow)
354  {
357  ASSERT(isValidHwnd());
358  return ::ShowWindow(m_hWnd, nCmdShow);
359  }
360  bool ShowWindow(bool bCmdShow)
361  {
363  return ShowWindow(bCmdShow ? SW_SHOWNORMAL : SW_HIDE);
364  }
365 
366  bool IsWindowEnabled() const
367  {
368  if (!isValidHwnd())
369  return false;
370  return ::IsWindowEnabled(m_hWnd);
371  }
372  bool EnableWindow(bool bState = true)
373  {
375  ASSERT(isValidHwnd());
376  return ::EnableWindow(m_hWnd, bState);
377  }
378 
379  // Drawing.
380  void SetRedraw(bool bRedraw)
381  {
383  SendMessageX(WM_SETREDRAW, bRedraw);
384  }
385  HDC GetDC(void) const
386  {
387  ASSERT_VALID(this);
388  return(::GetDC(m_hWnd));
389  }
390  void ReleaseDC(HDC hDC) const
391  {
392  ASSERT_VALID(this);
393  ::ReleaseDC(m_hWnd, hDC);
394  }
395  void ValidateRect(RECT* pRect = nullptr) const
396  {
397  ASSERT_VALID(this);
398  if (!isValidHwnd())
399  return;
400  ::ValidateRect(m_hWnd, pRect);
401  }
402  void InvalidateRect(RECT* pRect = nullptr, bool bErase = false) const
403  {
404  ASSERT_VALID(this);
405  if (!isValidHwnd())
406  return;
407  ::InvalidateRect(m_hWnd, pRect, bErase);
408  }
409  void InvalidateRect(int sx, int sy, int iWidth, int iHeight) const
410  {
411  cRectI rect(sx, sy, sx + iWidth, sy + iHeight);
412  InvalidateRect(rect);
413  }
414  bool UpdateWindow()
415  {
417  return(::UpdateWindow(m_hWnd));
418  }
419  bool RedrawWindow(const RECT* pRectUpdate = nullptr, HRGN hRgn = WINHANDLE_NULL, UINT flags = RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE)
420  {
423  return(::RedrawWindow(m_hWnd, pRectUpdate, hRgn, flags));
424  }
425 
426  // Standard windows props.
427  StrLen_t GetWindowTextLengthX() const
428  {
429  // name used in #define
430  ASSERT_VALID(this);
431  ASSERT(isValidHwnd());
432  return(_GTN(::GetWindowTextLength)(m_hWnd));
433  }
434  StrLen_t GetWindowTextX(GChar_t* lpszText, StrLen_t iLen) const
435  {
436  // name used in #define
437  ASSERT_VALID(this);
438  ASSERT(isValidHwnd());
439  return(_GTN(::GetWindowText)(m_hWnd, lpszText, iLen));
440  }
441  cString GetWindowTextX() const;
442  BOOL SetWindowTextX(const GChar_t* lpszText)
443  {
444  // name used in #define
445  ASSERT_VALID(this);
446  ASSERT(isValidHwnd());
447  return(_GTN(::SetWindowText)(m_hWnd, lpszText));
448  }
449 
450 #ifndef UNDER_CE
451  HMENU GetMenu(void) const
452  {
453  ASSERT_VALID(this);
454  return(::GetMenu(m_hWnd));
455  }
456  HMENU GetSystemMenu(bool bReset = false) const
457  {
458  ASSERT_VALID(this);
459  return(::GetSystemMenu(m_hWnd, bReset));
460  }
461 #endif
462 
463  HFONT GetFont() const
464  {
465  return((HFONT)(UINT_PTR)SendMessageX(WM_GETFONT));
466  }
467  void SetFont(HFONT hFont, bool bRedraw = false)
468  {
469  SendMessageX(WM_SETFONT, (WPARAM)hFont, MAKELPARAM(bRedraw, 0));
470  }
471 
472  HICON GetIcon(int fType = ICON_SMALL) const
473  {
475  return((HICON)(UINT_PTR)SendMessageX(WM_GETICON, (WPARAM)fType));
476  }
477  HICON SetIcon(HICON hIcon, int fType = ICON_SMALL)
478  {
480  return((HICON)(UINT_PTR)SendMessageX(WM_SETICON, (WPARAM)fType, (LPARAM)hIcon));
481  }
482 
483  UINT_PTR SetTimer(DLGID_t uTimerID, TIMESYSD_t uWaitmSec)
484  {
485  ASSERT_VALID(this);
486  ASSERT(isValidHwnd());
487  return(::SetTimer(m_hWnd, uTimerID, uWaitmSec, nullptr));
488  }
489  BOOL KillTimer(DLGID_t uTimerID)
490  {
491  ASSERT_VALID(this);
492  ASSERT(isValidHwnd());
493  return(::KillTimer(m_hWnd, uTimerID));
494  }
495  int ShowMessageBox(const GChar_t* pszText, const GChar_t* pszTitle, UINT fuStyle = MB_OK) const
496  {
499  return(_GTN(::MessageBox)(m_hWnd, pszText, pszTitle, fuStyle));
500  }
501  LONG SetLongValue(int nIndex, LONG dwNewLong)
502  {
503  // X is used in #define
504  ASSERT_VALID(this);
505  ASSERT(isValidHwnd());
506  return(_GTN(::SetWindowLong)(m_hWnd, nIndex, dwNewLong));
507  }
508  LONG GetLongValue(int nIndex) const
509  {
510  // X is used in #define
511  ASSERT_VALID(this);
512  ASSERT(isValidHwnd());
513  return(_GTN(::GetWindowLong)(m_hWnd, nIndex));
514  }
515 
516  static inline LONG_PTR SetLongPtr(HWND hWnd, int nIndex, LONG_PTR dwNewLong)
517  {
519  // X is used in #define
520 #ifndef GWLP_USERDATA
521 #define GWLP_USERDATA GWL_USERDATA
522 #define GWLP_WNDPROC GWL_WNDPROC
523 #endif
524 #if defined(USE_64BIT) && ! defined(UNDER_CE)
525  return(_GTN(::SetWindowLongPtr)(hWnd, nIndex, dwNewLong));
526 #else
527  return(_GTN(::SetWindowLong)(hWnd, nIndex, (LONG)dwNewLong));
528 #endif
529  }
530 
531  static inline LONG_PTR GetLongPtr(HWND hWnd, int nIndex)
532  {
533  // X is used in #define
534 #if defined(USE_64BIT) && ! defined(UNDER_CE)
535  return(_GTN(::GetWindowLongPtr)(hWnd, nIndex));
536 #else
537  return(_GTN(::GetWindowLong)(hWnd, nIndex));
538 #endif
539  }
540 
541  LONG_PTR put_WindowLongPtr(int nIndex, LONG_PTR dwNewLong)
542  {
543  ASSERT_VALID(this);
544  ASSERT(isValidHwnd());
545  return SetLongPtr(m_hWnd, nIndex, dwNewLong);
546  }
547 
548  LONG_PTR get_WindowLongPtr(int nIndex)
549  {
550  ASSERT_VALID(this);
551  ASSERT(isValidHwnd());
552  return GetLongPtr(m_hWnd, nIndex);
553  }
554  LONG_PTR GetClassLongPtrX(int nIndex) const
555  {
556  // X is used in #define
557  ASSERT_VALID(this);
558  ASSERT(isValidHwnd());
559 #if defined(USE_64BIT)
560  return(_GTN(::GetClassLongPtr)(m_hWnd, nIndex));
561 #else
562  return(_GTN(::GetClassLong)(m_hWnd, nIndex));
563 #endif
564  }
565 
566  DWORD GetStyle() const
567  {
569  return((DWORD)GetLongValue(GWL_STYLE));
570  }
571  DWORD SetStyle(DWORD dwStyle)
572  {
573  return((DWORD)SetLongValue(GWL_STYLE, dwStyle));
574  }
575  DWORD GetExStyle() const
576  {
578  return((DWORD)GetLongValue(GWL_EXSTYLE));
579  }
580 
581  // Child Windows
582  virtual DLGID_t GetDlgCtrlID() const
583  {
586 
587  ASSERT_VALID(this);
588  ASSERT(isValidHwnd());
589  return ::GetDlgCtrlID(m_hWnd);
590  }
591 
592  cWndHandle GetDlgItem(DLGID_t id) const
593  {
594  ASSERT(isValidHwnd());
595  return ::GetDlgItem(m_hWnd, id);
596  }
597  LRESULT SendDlgItemMessageX(DLGID_t nIDDlgItem, WINMSG_t Msg, WPARAM wParam = 0, LPARAM lParam = 0) const
598  {
600  ASSERT(isValidHwnd());
601  return(_GTN(::SendDlgItemMessage)(m_hWnd, nIDDlgItem, Msg, wParam, lParam));
602  }
603  UINT GetDlgItemTextX(DLGID_t nIDDlgItem, GChar_t* lpString, StrLen_t nMaxCount) const
604  {
606  ASSERT(isValidHwnd());
607  return(_GTN(::GetDlgItemText)(m_hWnd, nIDDlgItem, lpString, nMaxCount));
608  }
609  cString GetDlgItemTextX(DLGID_t nIDDlgItem) const;
610 
611  BOOL SetDlgItemTextX(DLGID_t nIDDlgItem, const GChar_t* lpszText) const
612  {
614  ASSERT_VALID(this);
615  return(_GTN(::SetDlgItemText)(m_hWnd, nIDDlgItem, lpszText));
616  }
617 
618  void SetDlgItemInt(DLGID_t nIDDlgItem, int iVal) const;
619  int GetDlgItemInt(DLGID_t nIDDlgItem) const;
620 
621  UINT IsDlgButtonChecked(DLGID_t nIDDlgItem) const
622  {
625  ASSERT_VALID(this);
626  return(::IsDlgButtonChecked(m_hWnd, nIDDlgItem));
627  }
628  bool CheckDlgButton(DLGID_t nIDButton, UINT uCheck)
629  {
631  ASSERT_VALID(this);
632  return(::CheckDlgButton(m_hWnd, nIDButton, uCheck));
633  }
634  bool CheckRadioButton(DLGID_t nIDFirstButton, DLGID_t nIDLastButton, DLGID_t nIDCheckButton)
635  {
636  ASSERT_VALID(this);
637  return(::CheckRadioButton(m_hWnd, nIDFirstButton, nIDLastButton, nIDCheckButton));
638  }
639 
640  HWND SetCapture()
641  {
642  return(::SetCapture(m_hWnd));
643  }
644 
645  // Special methods.
646  // Cursor selection stuff.
647  static HWND GRAYCALL FindWindowAtPoint(HWND hWndParent, const POINT& ptScreen);
648 
649  void CenterWindow(HWND hWndOrder = HWND_NOTOPMOST);
650  void EnableWindowItem(DLGID_t id, bool bState);
651  void EnableWindowRange(DLGID_t idChildStart, DLGID_t idChildEnd, bool bState);
652  void ShowWindowItem(DLGID_t id, bool bState);
653  void ShowWindowRange(DLGID_t idChildStart, DLGID_t idChildEnd, bool bState);
654  ITERATE_t FillComboBox(DLGID_t idChild, const cArrayStringA& aValues);
655 
656  cString GetClipboardText(int iSizeMax = StrT::k_LEN_MAX - 1)
657  {
658  return cClipboard::GetClipboardText(get_Hwnd(), iSizeMax);
659  }
660  bool SetClipboardText(cString sText)
661  {
662  return cClipboard::SetClipboardText(get_Hwnd(), sText);
663  }
664 #endif // _WIN32
665  };
666 
667 
668 #ifndef _MFC_VER
669  typedef cWndHandle CWnd;
670 #define HANDLE_DLGMSG HANDLE_MSG
671 #endif
672 
673 }
674 
675 #endif
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define IS_TYPE_OF(t, p)
Definition: PtrCast.h:23
#define _GTN(c)
_WIN32 name has a A or W for UTF8 or UNICODE (like _FNF)
Definition: StrConst.h:28
#define UNREFERENCED_PARAMETER(P)
< _WIN32 type thing. get rid of stupid warning.
Definition: SysTypes.h:299
UINT WINMSG_t
Supplement _WIN32 "windows.h".
Definition: WinTypes.h:111
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define DEBUG_CHECK(exp)
Definition: cDebugAssert.h:90
#define WINHANDLE_NULL
HWND, HPEN, etc are NOT OS Handles. like HWND_DESKTOP. like HANDLEPTR_NULL. This is a WINAPI void* ha...
Definition: cOSHandle.h:23
#define ASSERT_VALID(p)
Definition: cObject.h:61
Definition: cWndHandle.h:26
cWndHandle(HWND hWnd=WINHANDLE_NULL) noexcept
Definition: cWndHandle.h:39
bool AttachHwnd(HWND hWnd)
Definition: cWndHandle.h:105
HWND get_Hwnd() const noexcept
Definition: cWndHandle.h:93
void DetachHwnd()
Definition: cWndHandle.h:111
bool isValidHwnd() const noexcept
Definition: cWndHandle.h:65
cWndHandle(cWndHandle *pWnd) noexcept
Definition: cWndHandle.h:43
HWND GetSafeHwnd() const noexcept
Definition: cWndHandle.h:98
virtual bool isValidCheck() const noexcept
< memory allocation and structure definitions are valid.
Definition: cWndHandle.h:74
virtual ~cWndHandle()
Definition: cWndHandle.h:60
HWND m_hWnd
Handle to window (i may or may not have to destroy this)
Definition: cWndHandle.h:36
Definition: cObject.h:67
static HMODULE __stdcall get_HModule()
Definition: cAppState.cpp:782
Definition: cMesh.h:22
cWndHandle CWnd
Definition: cWndHandle.h:669
UINT DLGID_t
old window format this was a WORD, EX format this is UINT32.
Definition: cResDialog.h:68
interface const RECTQ_t & rect
Definition: cQuadtree.h:44
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28
int ITERATE_t
like size_t but signed
Definition: Index.h:28
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26
cArrayString< char > cArrayStringA
Definition: cArrayString.h:174
cStringT< GChar_t > cString
Definition: cString.h:636
static const StrLen_t k_LEN_MAX
arbitrary max size for Format() etc. NOTE: _MSC_VER says stack frame should be at least 16384
Definition: StrT.h:75
static bool IsValidApp(const void *pData) noexcept
Definition: cMem.h:42