WinapiExtension
 All Classes Files Functions Variables Typedefs Pages
msgBase.h
1 #ifndef WINAPI_EX_MSGBASE_H
2 #define WINAPI_EX_MSGBASE_H
3 
5 
7 struct msgBase
8 {
9  msgBase()
10  : hwnd(NULL)
11  , message(0)
12  , wp(0)
13  , lp(0)
14  , result(0)
15  , resultSet(false)
16  , messageHandler(NULL)
17  {}
18 
22  void Init(HWND pHwnd, UINT pMessage, WPARAM pWP, LPARAM pLP)
23  {
24  hwnd = pHwnd;
25  message = pMessage;
26  wp = pWP;
27  lp = pLP;
28  result = 0;
29  resultSet = false;
30  }
31 
37  void Result(LRESULT res)
38  {
39  // warn if the result is set more than once
40  assert(!resultSet);
41  result = res;
42  resultSet = true;
43  }
44 
46  LRESULT Default();
48  void ResultDefault() { Result(Default()); }
49 
50  HWND hwnd;
51  UINT message;
52  WPARAM wp;
53  LPARAM lp;
54 
55  LRESULT result;
56  bool resultSet;
57  WindowMessageHandler* messageHandler;
58 };
59 
60 struct msgCaptureChanged : public msgBase
61 {
62  HWND WindowGainingCapture() { return (HWND)lp; }
63 };
64 
65 struct msgCommand : public msgBase
66 {
67  WORD ID() { return LOWORD(wp); }
68  WORD NotifyCode() { return HIWORD(wp); }
69  HWND Control() { return (HWND)lp; }
70 };
71 
72 struct msgCreate : public msgBase
73 {
74  CREATESTRUCT* CreateStruct() { return (CREATESTRUCT*)lp; }
75 };
76 
77 struct msgCtlColor : public msgBase
78 {
79  HDC DC() { return (HDC)wp; }
80  HWND Control() { return (HWND)lp; }
81 
83  void Result(HBRUSH brush) { return msgBase::Result((LRESULT)brush); }
84 };
85 
86 struct msgDropFiles : public msgBase
87 {
91  HDROP HandleDrop() { return (HDROP)wp; }
92 
96  UINT NumFiles() { return DragQueryFile(HandleDrop(), 0xFFFFFFFF, NULL, 0); }
97 
102  tstring GetFile(UINT i)
103  {
104  UINT requiredBufferSize = DragQueryFile(HandleDrop(), i, NULL, 0) + 1;
105 
106  std::vector<TCHAR> dropped;
107  dropped.resize(requiredBufferSize);
108 
109  DragQueryFile(HandleDrop(), i, &dropped[0], requiredBufferSize);
110 
111  return &dropped[0];
112  }
113 };
114 
115 struct msgEnable : public msgBase
116 {
117  bool Enabled() { return wp == TRUE; }
118 };
119 
120 struct msgEraseBkgnd : public msgBase
121 {
122  HDC DC() { return (HDC)wp; }
123 
125  void Result(BOOL erased) { msgBase::Result(erased); }
126 };
127 
128 struct msgGetIcon : public msgBase
129 {
130  WPARAM Type() { return wp; }
131 
133  void Result(HICON icon) { msgBase::Result((LRESULT)icon); }
134 };
135 
136 struct msgGetMinMaxInfo : public msgBase
137 {
138  MINMAXINFO* MinMaxInfo() { return (MINMAXINFO*)lp; }
139 };
140 
141 struct msgKey : public msgBase
142 {
151  WPARAM KeyCode() { return wp; }
152  SHORT Repeat() { return LOWORD(lp); }
153  BYTE ScanCode() { return BYTE((lp & 0x00FF0000) >> 16); }
154  bool ExtendedKey() { return (lp & 0x01000000) > 0; }
155  bool AltDown() { return (lp & 0x20000000) > 0; }
156  bool KeyWasDown() { return (lp & 0x40000000) > 0; }
157  bool KeyWasUp() { return !KeyWasDown(); }
158  bool KeyReleased() { return (lp & 0x80000000) > 0; }
159  bool KeyPressed() { return !KeyReleased(); }
160 };
161 
162 struct msgMouse : public msgBase
163 {
164  bool Ctrl() { return (wp & MK_CONTROL) > 0; }
165  bool Shift() { return (wp & MK_SHIFT) > 0; }
166  bool LButton() { return (wp & MK_LBUTTON) > 0; }
167  bool MButton() { return (wp & MK_MBUTTON) > 0; }
168  bool RButton() { return (wp & MK_RBUTTON) > 0; }
169  bool XButton1() { return (wp & MK_XBUTTON1) > 0; }
170  bool XButton2() { return (wp & MK_XBUTTON2) > 0; }
171  SHORT X() { return MAKEPOINTS(lp).x; }
172  SHORT Y() { return MAKEPOINTS(lp).y; }
173  Point GetPoint() { return Point(X(), Y()); }
175  double WheelMoveSteps() { return double(SHORT(HIWORD(wp))) / double(WHEEL_DELTA); }
177  bool FromXButton1() { return HIWORD(wp) == XBUTTON1; }
179  bool FromXButton2() { return HIWORD(wp) == XBUTTON2; }
180 };
181 
182 struct msgMove : public msgBase
183 {
184  SHORT X() { return MAKEPOINTS(lp).x; }
185  SHORT Y() { return MAKEPOINTS(lp).y; }
186  Point GetPoint() { return Point(X(), Y()); }
187 };
188 
189 struct msgMoving : public msgBase
190 {
191  RECT* Rect() { return (RECT*)lp; }
192 };
193 
194 struct msgNotify : public msgBase
195 {
196  WPARAM ID() { return wp; }
197  NMHDR* Header() { return (NMHDR*)lp; }
198 };
199 
200 struct msgPrint : public msgBase
201 {
202  HDC DC() { return (HDC)wp; }
203  LPARAM Options() { return lp; }
204 };
205 
206 struct msgScroll : public msgBase
207 {
208  WORD Pos() { return LOWORD(wp); }
209  WORD ScrollCode() { return HIWORD(wp); }
210  HWND ScrollBar() { return (HWND)lp; }
211 
213  void UpdateScrollPos() { return ::UpdateScrollPos(hwnd, message, wp, lp); }
214 };
215 
216 struct msgSize : public msgBase
217 {
218  WPARAM Type() { return wp; }
219  WORD Width() { return LOWORD(lp); }
220  WORD Height() { return HIWORD(lp); }
221  Size GetSize() { return Size(Width(), Height()); }
222 };
223 
224 struct msgSizing : public msgBase
225 {
226  WPARAM Side() { return wp; }
227  RECT* Rect() { return (RECT*)lp; }
228 };
229 
230 struct msgStyleChanged : public msgBase
231 {
232  bool StyleChanged() { return (wp & GWL_STYLE) > 0; }
233  bool ExStyleChanged() { return (wp & GWL_EXSTYLE) > 0; }
234  STYLESTRUCT* StyleStruct() { return (STYLESTRUCT*)lp; }
235 };
236 
237 struct msgTimer : public msgBase
238 {
239  WPARAM ID() { return wp; }
240 };
241 
243 {
244  WINDOWPOS* WindowPos() { return (WINDOWPOS*)lp; }
245 };
246 
247 #endif