Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
9.0 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998
  4. *
  5. * TITLE: SIMCRACK.H
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 5/11/1998
  12. *
  13. * DESCRIPTION: Simple Message-crackers
  14. *
  15. *******************************************************************************/
  16. #ifndef ___SIMCRACK_H_INCLUDED
  17. #define ___SIMCRACK_H_INCLUDED
  18. // Define these if we're compiling on pre-sundown prepared compiler
  19. #if !defined(GWLP_USERDATA)
  20. #define GWLP_USERDATA GWL_USERDATA
  21. #define GWLP_WNDPROC GWL_WNDPROC
  22. #define DWLP_USER DWL_USER
  23. #define DWLP_MSGRESULT DWL_MSGRESULT
  24. #define SetWindowLongPtr SetWindowLong
  25. #define GetWindowLongPtr GetWindowLong
  26. #define INT_PTR LONG
  27. #endif
  28. #if (0) // Examples
  29. /****************************************
  30. EXAMPLE USAGE
  31. ****************************************/
  32. // Normal message handlers for normal windows
  33. LRESULT CALLBACK CMyWindow::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  34. {
  35. SC_BEGIN_MESSAGE_HANDLERS(CMyWindow)
  36. {
  37. SC_HANDLE_MESSAGE( WM_SIZE, OnSize );
  38. SC_HANDLE_MESSAGE( WM_SETFOCUS, OnSetFocus );
  39. SC_FORWARD_MESSAGE( WM_CLOSE, hWnd );
  40. }
  41. SC_HANDLE_REGISTERED_MESSAGE(MyMessage,MyMessageHandler);
  42. SC_END_MESSAGE_HANDLERS();
  43. }
  44. // For use in dialog boxes (handles DWLP_MSGRESULT, etc)
  45. BOOL CALLBACK CMyDialog::DialogProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  46. {
  47. SC_BEGIN_DIALOG_MESSAGE_HANDLERS(CMyDialog)
  48. {
  49. SC_HANDLE_DIALOG_MESSAGE( WM_INITDIALOG, OnInitDialog );
  50. SC_HANDLE_DIALOG_MESSAGE( WM_COMMAND, OnCommand );
  51. }
  52. SC_HANDLE_REGISTERED_DIALOG_MESSAGE( MyMessage, MyMessageHandler );
  53. SC_END_DIALOG_MESSAGE_HANDLERS();
  54. }
  55. // WM_NOTIFY message cracker usage:
  56. LRESULT CMyWindow::OnNotify( WPARAM wParam, LPARAM lParam )
  57. {
  58. SC_BEGIN_NOTIFY_MESSAGE_HANDLERS()
  59. {
  60. SC_HANDLE_NOTIFY_MESSAGE_CODE(PSN_SETACTIVE,OnSetActive);
  61. SC_HANDLE_NOTIFY_MESSAGE_CONTROL(LVN_DBLCLK,IDC_LIST,OnListDblClk);
  62. }
  63. SC_END_NOTIFY_MESSAGE_HANDLERS();
  64. }
  65. // WM_COMMAND message cracker usage:
  66. LRESULT CMyWindow::OnCommand( WPARAM wParam, LPARAM lParam )
  67. {
  68. SC_BEGIN_COMMAND_HANDLERS()
  69. {
  70. SC_HANDLE_COMMAND_NOTIFY(EN_CHANGE,IDC_EDIT,OnEditChange);
  71. SC_HANDLE_COMMAND(IDOK,OnOK);
  72. }
  73. SC_END_COMMAND_HANDLERS();
  74. }
  75. /*
  76. For an example of a complete class structured to use this method of window encapsulation,
  77. look at the bottom of the file.
  78. */
  79. #endif // Examples
  80. /****************************************
  81. IMPLEMENTATION
  82. ****************************************/
  83. /****************************************
  84. Normal message handlers for normal windows
  85. ****************************************/
  86. #define SC_BEGIN_MESSAGE_HANDLERS(className) \
  87. className *This = (className*)GetWindowLongPtr(hWnd,GWLP_USERDATA);\
  88. if (WM_CREATE == uMsg)\
  89. {\
  90. This = new className(hWnd);\
  91. SetWindowLongPtr(hWnd,GWLP_USERDATA,(INT_PTR)This);\
  92. }\
  93. else if (WM_NCDESTROY == uMsg)\
  94. {\
  95. delete This;\
  96. This = NULL;\
  97. SetWindowLongPtr(hWnd,GWLP_USERDATA,0);\
  98. }\
  99. switch (uMsg)
  100. #define SC_HANDLE_MESSAGE(msg,handler) \
  101. case (msg):\
  102. {\
  103. if (This)\
  104. return This->handler( wParam, lParam );\
  105. }\
  106. break
  107. #define SC_FORWARD_MESSAGE(msg,hwndForward)\
  108. case (msg):\
  109. {\
  110. return SendMessage( hwndForward, msg, wParam, lParam );\
  111. }\
  112. break
  113. #define SC_END_MESSAGE_HANDLERS() \
  114. return (DefWindowProc(hWnd,uMsg,wParam,lParam))
  115. #define SC_HANDLE_REGISTERED_MESSAGE(msg,handler)\
  116. if (This && uMsg == This->msg)\
  117. {\
  118. return This->handler( wParam, lParam );\
  119. }
  120. /****************************************
  121. Normal message handlers for reference counted windows
  122. ****************************************/
  123. #define SC_BEGIN_REFCOUNTED_MESSAGE_HANDLERS(className) \
  124. className *This = (className*)GetWindowLongPtr(hWnd,GWLP_USERDATA);\
  125. if (WM_CREATE == uMsg)\
  126. {\
  127. This = new className(hWnd);\
  128. SetWindowLongPtr(hWnd,GWLP_USERDATA,(INT_PTR)This);\
  129. }\
  130. else if (WM_NCDESTROY == uMsg)\
  131. {\
  132. if (This)\
  133. {\
  134. This->Release();\
  135. This = NULL;\
  136. }\
  137. SetWindowLongPtr(hWnd,GWLP_USERDATA,0);\
  138. }\
  139. switch (uMsg)
  140. #define SC_HANDLE_MESSAGE(msg,handler) \
  141. case (msg):\
  142. {\
  143. if (This)\
  144. return This->handler( wParam, lParam );\
  145. }\
  146. break
  147. #define SC_FORWARD_MESSAGE(msg,hwndForward)\
  148. case (msg):\
  149. {\
  150. return SendMessage( hwndForward, msg, wParam, lParam );\
  151. }\
  152. break
  153. #define SC_END_MESSAGE_HANDLERS() \
  154. return (DefWindowProc(hWnd,uMsg,wParam,lParam))
  155. #define SC_HANDLE_REGISTERED_MESSAGE(msg,handler)\
  156. if (This && uMsg == This->msg)\
  157. {\
  158. return This->handler( wParam, lParam );\
  159. }
  160. /****************************************
  161. Dialog box message crackers
  162. ****************************************/
  163. #define SC_BEGIN_DIALOG_MESSAGE_HANDLERS(className) \
  164. UINT_PTR bRes = FALSE;\
  165. className *This = (className *)GetWindowLongPtr(hWnd,DWLP_USER);\
  166. if (WM_INITDIALOG == uMsg)\
  167. {\
  168. This = new className( hWnd );\
  169. SetWindowLongPtr(hWnd,DWLP_USER,(INT_PTR)This);\
  170. }\
  171. else if (WM_NCDESTROY == uMsg)\
  172. {\
  173. if (This)\
  174. delete This;\
  175. This = NULL;\
  176. SetWindowLongPtr(hWnd,DWLP_USER,(INT_PTR)This);\
  177. }\
  178. switch (uMsg)
  179. #define SC_HANDLE_DIALOG_MESSAGE(msg,handler) \
  180. case (msg):\
  181. {\
  182. if (This)\
  183. {\
  184. LRESULT lRes = This->handler( wParam, lParam );\
  185. if (WM_INITDIALOG==msg)\
  186. {\
  187. bRes = (UINT_PTR)(!lRes);\
  188. }\
  189. else if (WM_CTLCOLORBTN==msg || WM_CTLCOLORDLG==msg || WM_CTLCOLOREDIT==msg || WM_CTLCOLORLISTBOX==msg || WM_CTLCOLORMSGBOX==msg || WM_CTLCOLORSCROLLBAR==msg || WM_CTLCOLORSTATIC==msg)\
  190. {\
  191. bRes = (UINT_PTR)(lRes);\
  192. }\
  193. else bRes = true;\
  194. SetWindowLongPtr( hWnd, DWLP_MSGRESULT, (INT_PTR)lRes );\
  195. }\
  196. }\
  197. break
  198. #define SC_HANDLE_REGISTERED_DIALOG_MESSAGE(msg,handler)\
  199. if (This && uMsg == This->msg)\
  200. {\
  201. LRESULT lRes = This->handler( wParam, lParam );\
  202. SetWindowLongPtr( hWnd, DWLP_MSGRESULT, (INT_PTR)lRes );\
  203. bRes = true;\
  204. }
  205. #define SC_END_DIALOG_MESSAGE_HANDLERS() \
  206. return (bRes)
  207. /****************************************
  208. WM_NOTIFY message crackers
  209. ****************************************/
  210. #define SC_BEGIN_NOTIFY_MESSAGE_HANDLERS()
  211. #define SC_HANDLE_NOTIFY_MESSAGE_CODE(_code,proc)\
  212. if ((_code) == ((LPNMHDR)lParam)->code)\
  213. return proc( wParam, lParam )
  214. #define SC_HANDLE_NOTIFY_MESSAGE_CONTROL(_code,id,proc)\
  215. if ((_code) == ((LPNMHDR)lParam)->code && (id) == (int)wParam)\
  216. return proc( wParam, lParam )
  217. #define SC_END_NOTIFY_MESSAGE_HANDLERS()\
  218. return 0;
  219. /****************************************
  220. WM_COMMAND message crackers
  221. ****************************************/
  222. #define SC_BEGIN_COMMAND_HANDLERS()
  223. #define SC_HANDLE_COMMAND_NOTIFY(nCode,nIdCtl,handler)\
  224. if (nCode==(int)HIWORD(wParam) && nIdCtl==(int)LOWORD(wParam))\
  225. {\
  226. handler( wParam, lParam );\
  227. return (0);\
  228. }
  229. #define SC_HANDLE_COMMAND(nIdCtl,handler)\
  230. if (nIdCtl==(int)LOWORD(wParam))\
  231. {\
  232. handler( wParam, lParam );\
  233. return (0);\
  234. }
  235. #define SC_END_COMMAND_HANDLERS()\
  236. return (0)
  237. #if (0) // More examples
  238. class CMyWindow
  239. {
  240. private:
  241. HWND m_hWnd;
  242. private:
  243. explicit CHideWindow( HWND hWnd )
  244. : m_hWnd(hWnd)
  245. {
  246. }
  247. public:
  248. ~CMyWindow(void)
  249. {
  250. }
  251. // Standard Windows Message Handlers
  252. LRESULT OnCreate( WPARAM wParam, LPARAM lParam )
  253. {
  254. return 0;
  255. }
  256. // WM_COMMAND Handlers
  257. void OnEditChange( WPARAM wParam, LPARAM lParam )
  258. {
  259. }
  260. void OnOK( WPARAM wParam, LPARAM lParam )
  261. {
  262. }
  263. // WM_NOTIFY Handlers
  264. LRESULT OnSetActive( WPARAM wParam, LPARAM lParam )
  265. {
  266. return 0;
  267. }
  268. LRESULT OnListDblClk( WPARAM wParam, LPARAM lParam )
  269. {
  270. return 0;
  271. }
  272. LRESULT OnNotify( WPARAM wParam, LPARAM lParam )
  273. {
  274. SC_BEGIN_NOTIFY_MESSAGE_HANDLERS()
  275. {
  276. SC_HANDLE_NOTIFY_MESSAGE_CODE(PSN_SETACTIVE,OnSetActive);
  277. SC_HANDLE_NOTIFY_MESSAGE_CONTROL(LVN_DBLCLK,IDC_LIST,OnListDblClk);
  278. }
  279. SC_END_NOTIFY_MESSAGE_HANDLERS();
  280. }
  281. LRESULT OnCommand( WPARAM wParam, LPARAM lParam )
  282. {
  283. SC_BEGIN_COMMAND_HANDLERS()
  284. {
  285. SC_HANDLE_COMMAND_NOTIFY(EN_CHANGE,IDC_EDIT,OnEditChange);
  286. SC_HANDLE_COMMAND(IDOK,OnOK);
  287. }
  288. SC_END_COMMAND_HANDLERS();
  289. }
  290. static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  291. {
  292. SC_BEGIN_MESSAGE_HANDLERS(CMyWindow)
  293. {
  294. SC_HANDLE_MESSAGE( WM_CREATE, OnCreate );
  295. }
  296. SC_END_MESSAGE_HANDLERS();
  297. }
  298. };
  299. #endif // More examples
  300. #endif // ___SIMCRACK_H_INCLUDED