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