rlm@1
|
1 /*----------------------------------------------------------------------
|
rlm@1
|
2 Copyright (c) 1998 Gipsysoft. All Rights Reserved.
|
rlm@1
|
3 Please see the file "licence.txt" for licencing details.
|
rlm@1
|
4 File: WinHelper.h
|
rlm@1
|
5 Owner: russf@gipsysoft.com
|
rlm@1
|
6 Purpose: Windows helper functions, classes, structures and macros
|
rlm@1
|
7 that make life a little easier
|
rlm@1
|
8 These should all be zero impact classes etc. that is they
|
rlm@1
|
9 should *not* have a cpp file associated with them.
|
rlm@1
|
10 ----------------------------------------------------------------------*/
|
rlm@1
|
11 #ifndef WINHELPER_H
|
rlm@1
|
12 #define WINHELPER_H
|
rlm@1
|
13
|
rlm@1
|
14 //#ifndef DEBUGHLP_H
|
rlm@1
|
15 // #include <DebugHlp.h>
|
rlm@1
|
16 //#endif // DEBUGHLP_H
|
rlm@1
|
17
|
rlm@1
|
18 #ifndef FASTCALL
|
rlm@1
|
19 #define FASTCALL
|
rlm@1
|
20 #endif // FASTCALL
|
rlm@1
|
21
|
rlm@1
|
22 extern void AssertFailed(char *, int, char *);
|
rlm@1
|
23 extern void ApiFailure(char *, int, char *);
|
rlm@1
|
24
|
rlm@1
|
25 #define R_VERIFY(a) R_ASSERT(a)
|
rlm@1
|
26 #define R_ASSERT(a) \
|
rlm@1
|
27 do { \
|
rlm@1
|
28 if (!(a)) { \
|
rlm@1
|
29 AssertFailed(__FILE__, __LINE__, # a); \
|
rlm@1
|
30 } \
|
rlm@1
|
31 } while (0);
|
rlm@1
|
32
|
rlm@1
|
33 #define VAPI(a) \
|
rlm@1
|
34 do { \
|
rlm@1
|
35 if (!(a)) { \
|
rlm@1
|
36 ApiFailure(__FILE__, __LINE__, # a); \
|
rlm@1
|
37 } \
|
rlm@1
|
38 } while (0);
|
rlm@1
|
39
|
rlm@1
|
40 #define ASSERT_VALID_HWND(a) ASSERT(::IsWindow(a))
|
rlm@1
|
41
|
rlm@1
|
42 namespace WinHelper
|
rlm@1
|
43 {
|
rlm@1
|
44 class CSize : public tagSIZE
|
rlm@1
|
45 //
|
rlm@1
|
46 // Wrapper for the SIZE structure
|
rlm@1
|
47 {
|
rlm@1
|
48 public:
|
rlm@1
|
49 inline CSize() {};
|
rlm@1
|
50 inline explicit CSize(const SIZE &size) { cx = size.cx; cy = size.cy; }
|
rlm@1
|
51 inline explicit CSize(long nSizeX, long nSizeY) { cx = nSizeX; cy = nSizeY; }
|
rlm@1
|
52 inline void Set(long nSizeX, long nSizeY) { cx = nSizeX; cy = nSizeY; }
|
rlm@1
|
53 inline operator LPSIZE() { return this; };
|
rlm@1
|
54
|
rlm@1
|
55 inline bool operator !=(const SIZE &size) const { return cx != size.cx || cy != size.cy;}
|
rlm@1
|
56 inline CSize & operator =(const SIZE &size) { cx = size.cx; cy = size.cy; return *this; }
|
rlm@1
|
57 inline void Empty() { cx = cy = 0; }
|
rlm@1
|
58 };
|
rlm@1
|
59
|
rlm@1
|
60 class CRect : public tagRECT
|
rlm@1
|
61 //
|
rlm@1
|
62 // Wrapper for a RECT structure
|
rlm@1
|
63 {
|
rlm@1
|
64 public:
|
rlm@1
|
65 inline CRect() {}
|
rlm@1
|
66 // Initialisation constructor
|
rlm@1
|
67 inline explicit CRect(const RECT& rhs) { Set(rhs.left, rhs.top, rhs.right, rhs.bottom);}
|
rlm@1
|
68 inline CRect(int xLeft, int yTop, int xRight, int yBottom) { Set(xLeft, yTop, xRight, yBottom); }
|
rlm@1
|
69 // Get the width of the rectangle
|
rlm@1
|
70 inline int Width() const { return right - left; }
|
rlm@1
|
71 // Get the height of the rectangle
|
rlm@1
|
72 inline int Height() const { return bottom - top; }
|
rlm@1
|
73 // overloaded operator so you don't have to do &rc anymore
|
rlm@1
|
74 inline operator LPCRECT() const { return this; };
|
rlm@1
|
75 inline operator LPRECT() { return this; };
|
rlm@1
|
76 // Return the SIZE of the rectangle;
|
rlm@1
|
77 inline CSize Size() const { CSize s(Width(), Height()); return s; }
|
rlm@1
|
78 // Return the top left of the rectangle
|
rlm@1
|
79 inline POINT TopLeft() const { POINT pt = { left, top }; return pt; }
|
rlm@1
|
80 // Return the bottom right of the rectangle
|
rlm@1
|
81 inline POINT BottomRight() const { POINT pt = { right, bottom }; return pt; }
|
rlm@1
|
82 // Set the rectangles left, top, right and bottom
|
rlm@1
|
83 inline void Set(int xLeft, int yTop, int xRight, int yBottom) { top = yTop; bottom = yBottom; right = xRight; left =
|
rlm@1
|
84 xLeft; }
|
rlm@1
|
85 // Return true if the rectangle contains all zeros
|
rlm@1
|
86 inline bool IsEmpty() const { return left == 0 && right == 0 && top == 0 && bottom == 0 ? true : false; }
|
rlm@1
|
87 // Zero out our rectangle
|
rlm@1
|
88 inline void Empty() { left = right = top = bottom = 0; }
|
rlm@1
|
89 // Set the size of the rect but leave the top left position untouched.
|
rlm@1
|
90 inline void SetSize(const CSize &size) { bottom = top + size.cy; right = left + size.cx; }
|
rlm@1
|
91 inline void SetSize(const SIZE &size) { bottom = top + size.cy; right = left + size.cx; }
|
rlm@1
|
92 inline void SetSize(int cx, int cy) { bottom = top + cy; right = left + cx; }
|
rlm@1
|
93 // Move the rectangle by an offset
|
rlm@1
|
94 inline void Offset(int cx, int cy)
|
rlm@1
|
95 {
|
rlm@1
|
96 top += cy;
|
rlm@1
|
97 bottom += cy;
|
rlm@1
|
98 right += cx;
|
rlm@1
|
99 left += cx;
|
rlm@1
|
100 }
|
rlm@1
|
101
|
rlm@1
|
102 // Inflate the rectangle by the cx and cy, use negative to shrink the rectangle
|
rlm@1
|
103 inline void Inflate(int cx, int cy)
|
rlm@1
|
104 {
|
rlm@1
|
105 top -= cy;
|
rlm@1
|
106 bottom += cy;
|
rlm@1
|
107 right += cx;
|
rlm@1
|
108 left -= cx;
|
rlm@1
|
109 }
|
rlm@1
|
110
|
rlm@1
|
111 // Assignment from a RECT
|
rlm@1
|
112 inline CRect &operator =(const RECT&rhs)
|
rlm@1
|
113 {
|
rlm@1
|
114 left = rhs.left; top = rhs.top;
|
rlm@1
|
115 right = rhs.right; bottom = rhs.bottom;
|
rlm@1
|
116 return *this;
|
rlm@1
|
117 }
|
rlm@1
|
118
|
rlm@1
|
119 // Return true if the point passed is within the rectangle
|
rlm@1
|
120 inline bool PtInRect(const POINT &pt) const { return (pt.x >= left && pt.x < right && pt.y >= top && pt.y <
|
rlm@1
|
121 bottom); }
|
rlm@1
|
122 // Return true if the rectangle passed overlaps this rectangle
|
rlm@1
|
123 inline bool Intersect(const RECT &rc) const { return (rc.left < right &&
|
rlm@1
|
124 rc.right > left && rc.top < bottom && rc.bottom > top); }
|
rlm@1
|
125 };
|
rlm@1
|
126
|
rlm@1
|
127 class CPoint : public tagPOINT
|
rlm@1
|
128 //
|
rlm@1
|
129 // Wrapper for the POINT structure
|
rlm@1
|
130 {
|
rlm@1
|
131 public:
|
rlm@1
|
132 inline CPoint() {};
|
rlm@1
|
133 inline CPoint(LPARAM lParam) { x = LOWORD(lParam); y = HIWORD(lParam); }
|
rlm@1
|
134 inline CPoint(int nX, int nY) { x = nX; y = nY; }
|
rlm@1
|
135 inline CPoint(const POINT &pt) { x = pt.x; y = pt.y; }
|
rlm@1
|
136 inline bool operator ==(const CPoint &rhs) const { return x == rhs.x && y == rhs.y; }
|
rlm@1
|
137 inline bool operator !=(const CPoint &rhs) const { return x != rhs.x || y != rhs.y; }
|
rlm@1
|
138 inline operator LPPOINT() { return this; }
|
rlm@1
|
139 };
|
rlm@1
|
140
|
rlm@1
|
141 class CScrollInfo : public tagSCROLLINFO
|
rlm@1
|
142 {
|
rlm@1
|
143 public:
|
rlm@1
|
144 CScrollInfo(UINT fPassedMask) { cbSize = sizeof(tagSCROLLINFO); fMask = fPassedMask; }
|
rlm@1
|
145 };
|
rlm@1
|
146
|
rlm@1
|
147 class CCriticalSection
|
rlm@1
|
148 //
|
rlm@1
|
149 // Simple crtical section handler/wrapper
|
rlm@1
|
150 {
|
rlm@1
|
151 public:
|
rlm@1
|
152 inline CCriticalSection() { ::InitializeCriticalSection(&m_sect); }
|
rlm@1
|
153 inline ~CCriticalSection() { ::DeleteCriticalSection(&m_sect); }
|
rlm@1
|
154
|
rlm@1
|
155 // Blocking lock.
|
rlm@1
|
156 inline void Lock() { ::EnterCriticalSection(&m_sect); }
|
rlm@1
|
157 // Unlock
|
rlm@1
|
158 inline void Unlock() { ::LeaveCriticalSection(&m_sect); }
|
rlm@1
|
159
|
rlm@1
|
160 class CLock
|
rlm@1
|
161 //
|
rlm@1
|
162 // Simple lock class for the critcal section
|
rlm@1
|
163 {
|
rlm@1
|
164 public:
|
rlm@1
|
165 inline CLock(CCriticalSection §) : m_sect(sect) { m_sect.Lock(); }
|
rlm@1
|
166 inline ~CLock() { m_sect.Unlock(); }
|
rlm@1
|
167 private:
|
rlm@1
|
168 CCriticalSection &m_sect;
|
rlm@1
|
169
|
rlm@1
|
170 CLock();
|
rlm@1
|
171 CLock(const CLock &);
|
rlm@1
|
172 CLock & operator =(const CLock &);
|
rlm@1
|
173 };
|
rlm@1
|
174 private:
|
rlm@1
|
175 CRITICAL_SECTION m_sect;
|
rlm@1
|
176
|
rlm@1
|
177 CCriticalSection(const CCriticalSection &);
|
rlm@1
|
178 CCriticalSection & operator =(const CCriticalSection &);
|
rlm@1
|
179 };
|
rlm@1
|
180
|
rlm@1
|
181 #define ZeroStructure(t) ZeroMemory(&t, sizeof(t))
|
rlm@1
|
182 #define countof(t) (sizeof((t)) / sizeof((t)[0]))
|
rlm@1
|
183 #define UNREF(P) UNREFERENCED_PARAMETER(P)
|
rlm@1
|
184
|
rlm@1
|
185 inline bool IsShiftPressed()
|
rlm@1
|
186 {
|
rlm@1
|
187 return GetKeyState(VK_SHIFT) & 0x8000 ? true : false;
|
rlm@1
|
188 }
|
rlm@1
|
189
|
rlm@1
|
190 inline bool IsAltPressed()
|
rlm@1
|
191 {
|
rlm@1
|
192 return GetKeyState(VK_MENU) & 0x8000 ? true : false;
|
rlm@1
|
193 }
|
rlm@1
|
194
|
rlm@1
|
195 inline bool IsControlPressed()
|
rlm@1
|
196 {
|
rlm@1
|
197 return GetKeyState(VK_CONTROL) & 0x8000 ? true : false;
|
rlm@1
|
198 }
|
rlm@1
|
199
|
rlm@1
|
200 inline HICON LoadIcon16x16(HINSTANCE hInst, UINT uID)
|
rlm@1
|
201 //
|
rlm@1
|
202 // Load a 16x16 icon from the same resource as the other size icons.
|
rlm@1
|
203 {
|
rlm@1
|
204 return reinterpret_cast<HICON>(::LoadImage(hInst, MAKEINTRESOURCE(uID), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR));
|
rlm@1
|
205 }
|
rlm@1
|
206
|
rlm@1
|
207 class CDeferWindowPos
|
rlm@1
|
208 //
|
rlm@1
|
209 // Wrapper for the Begin, Defer and End WindowPos functions. Nothing glamorous.
|
rlm@1
|
210 {
|
rlm@1
|
211 public:
|
rlm@1
|
212 inline CDeferWindowPos(const int nWindows = 1) : m_hdlDef(::BeginDeferWindowPos(nWindows)) {}
|
rlm@1
|
213 inline ~CDeferWindowPos() { R_VERIFY(::EndDeferWindowPos(m_hdlDef)); }
|
rlm@1
|
214 inline HDWP DeferWindowPos(HWND hWnd, HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT uFlags)
|
rlm@1
|
215 {
|
rlm@1
|
216 return ::DeferWindowPos(m_hdlDef, hWnd, hWndInsertAfter, x, y, cx, cy, uFlags);
|
rlm@1
|
217 }
|
rlm@1
|
218
|
rlm@1
|
219 inline HDWP DeferWindowPos(HWND hWnd, HWND hWndInsertAfter, const CRect &rc, UINT uFlags)
|
rlm@1
|
220 {
|
rlm@1
|
221 return ::DeferWindowPos(m_hdlDef, hWnd, hWndInsertAfter, rc.left, rc.top, rc.Width(), rc.Height(), uFlags);
|
rlm@1
|
222 }
|
rlm@1
|
223
|
rlm@1
|
224 private:
|
rlm@1
|
225 HDWP m_hdlDef;
|
rlm@1
|
226 };
|
rlm@1
|
227 } // WinHelper
|
rlm@1
|
228
|
rlm@1
|
229 #endif //WINHELPER_H
|