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.

428 lines
10 KiB

  1. /*+
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. DLG.CPP
  5. Abstract:
  6. C_Dlg implementation
  7. Author:
  8. 990518 dane Created.
  9. 990721 dane Added _THIS_MODULE_ for logging macros.
  10. georgema 000310 updated
  11. Environment:
  12. Win98, Win2000
  13. Revision History:
  14. --*/
  15. #pragma comment(user, "Compiled on " __DATE__ " at " __TIME__)
  16. #pragma comment(compiler)
  17. //////////////////////////////////////////////////////////////////////////////
  18. //
  19. // Include files
  20. //
  21. #include "Dlg.h"
  22. //////////////////////////////////////////////////////////////////////////////
  23. //
  24. // Static initialization
  25. //
  26. static const char _THIS_FILE_[ ] = __FILE__;
  27. //////////////////////////////////////////////////////////////////////////////
  28. //
  29. // Static member initialization
  30. //
  31. LPCTSTR C_Dlg::SZ_HWND_PROP = _T("hwnd");
  32. //////////////////////////////////////////////////////////////////////////////
  33. //
  34. // C_Dlg
  35. //
  36. // Constructor.
  37. //
  38. // parameters:
  39. // hwndParent parent window for the dialog (may be NULL)
  40. // hInstance instance handle of the parent window (may be NULL)
  41. // lIDD dialog template id
  42. // pfnDlgProc pointer to the function that will process messages for
  43. // the dialog. if it is NULL, the default dialog proc
  44. // will be used.
  45. //
  46. // returns:
  47. // Nothing.
  48. //
  49. //////////////////////////////////////////////////////////////////////////////
  50. C_Dlg::C_Dlg(
  51. HWND hwndParent,
  52. HINSTANCE hInstance,
  53. LONG lIDD,
  54. DLGPROC pfnDlgProc // = NULL
  55. )
  56. : m_hwndParent(hwndParent),
  57. m_hInstance(hInstance),
  58. m_lIDD(lIDD),
  59. m_pfnDlgProc(pfnDlgProc),
  60. m_hwnd(NULL)
  61. {
  62. ASSERT(NULL != SZ_HWND_PROP);
  63. if (NULL == m_pfnDlgProc)
  64. {
  65. // Use the default dialog proc
  66. //
  67. m_pfnDlgProc = (DLGPROC) C_Dlg::DlgProc;
  68. }
  69. } // C_Dlg::C_Dlg
  70. //////////////////////////////////////////////////////////////////////////////
  71. //
  72. // ~C_Dlg
  73. //
  74. // Destructor.
  75. //
  76. // parameters:
  77. // None.
  78. //
  79. // returns:
  80. // Nothing.
  81. //
  82. //////////////////////////////////////////////////////////////////////////////
  83. C_Dlg::~C_Dlg( )
  84. {
  85. OnShutdown();
  86. } // C_Dlg::~C_Dlg
  87. void C_Dlg::CenterWindow() {
  88. RECT rectWorkArea;
  89. RECT rectWindow;
  90. DWORD FreeWidth, Width, FreeHeight, Height;
  91. if (! SystemParametersInfo(SPI_GETWORKAREA, 0, &rectWorkArea, 0))
  92. {
  93. return;
  94. }
  95. GetWindowRect(m_hwnd, &rectWindow);
  96. Height = (rectWorkArea.bottom - rectWorkArea.top);
  97. Width = (rectWorkArea.right - rectWorkArea.left);
  98. FreeHeight = Height -
  99. (rectWindow.bottom - rectWindow.top);
  100. FreeWidth = Width -
  101. (rectWindow.right - rectWindow.left);
  102. DWORD dxOffset = FreeWidth / 2;
  103. DWORD dyOffset = FreeHeight / 2;
  104. MoveWindow(m_hwnd,
  105. dxOffset,
  106. dyOffset,
  107. (rectWindow.right - rectWindow.left), (rectWindow.bottom - rectWindow.top),
  108. TRUE
  109. );
  110. return;
  111. }
  112. //////////////////////////////////////////////////////////////////////////////
  113. //
  114. // DoModal
  115. //
  116. // Display the dialog box modally and wait for it to be closed.
  117. //
  118. // parameters:
  119. // lparam user-defined data that will be passed to the dialog
  120. // proc as the lparam of the WM_INITDIALOG message.
  121. //
  122. // returns:
  123. // User-defined result code returned by EndDialog( ).
  124. //
  125. //////////////////////////////////////////////////////////////////////////////
  126. INT_PTR
  127. C_Dlg::DoModal(
  128. LPARAM lparam // = NULL
  129. )
  130. {
  131. INT_PTR nResult = DialogBoxParam(m_hInstance,
  132. MAKEINTRESOURCE(m_lIDD),
  133. m_hwndParent,
  134. m_pfnDlgProc,
  135. lparam
  136. );
  137. if (-1 == nResult)
  138. {
  139. DWORD dwErr = GetLastError( );
  140. }
  141. return nResult;
  142. } // C_Dlg::DoModal
  143. //////////////////////////////////////////////////////////////////////////////
  144. //
  145. // DlgProc
  146. //
  147. // Window procedure for wizard pages. All messages are routed here, then
  148. // dispatched to the appropriate C_Dlg object.
  149. //
  150. // parameters:
  151. // hwndDlg window handle of the page for which the message is
  152. // intended
  153. // uMessage the message
  154. // wparam message-specific data
  155. // lparam message-specific data
  156. //
  157. // returns:
  158. // TRUE if the message was processed
  159. // FALSE otherwise
  160. //
  161. //////////////////////////////////////////////////////////////////////////////
  162. BOOL CALLBACK
  163. C_Dlg::DlgProc(
  164. HWND hwndDlg,
  165. UINT uMessage,
  166. WPARAM wparam,
  167. LPARAM lparam
  168. )
  169. {
  170. CHECK_MESSAGE(hwndDlg, uMessage, wparam, lparam);
  171. // Get the pointer to the C_Dlg object corresponding to the hwndDlg
  172. //
  173. C_Dlg* pDlg = NULL;
  174. if (WM_INITDIALOG == uMessage)
  175. {
  176. // For WM_INITDIALOG, the pointer to the dialog object will be in
  177. // the lparam.
  178. //
  179. pDlg = (C_Dlg*) lparam;
  180. }
  181. else
  182. {
  183. // For all other messages, it will be attached to the HWND
  184. //
  185. HRESULT hr = C_Dlg::DlgFromHwnd(hwndDlg, &pDlg);
  186. if (FAILED(hr))
  187. {
  188. return FALSE;
  189. }
  190. ASSERT(NULL != pDlg);
  191. }
  192. // Let the page route application-specific messages
  193. //
  194. if (WM_APP <= uMessage)
  195. {
  196. return pDlg->OnAppMessage(uMessage, wparam, lparam);
  197. }
  198. // Route Windows messages to appropriate handler
  199. //
  200. switch (uMessage)
  201. {
  202. case WM_INITDIALOG:
  203. return pDlg->OnInitDialog(hwndDlg,
  204. (HWND)wparam
  205. );
  206. case WM_HELP:
  207. return pDlg->OnHelpInfo(lparam);
  208. case WM_CONTEXTMENU:
  209. return pDlg->OnContextMenu(wparam,lparam);
  210. case WM_COMMAND:
  211. return pDlg->OnCommand(HIWORD(wparam), LOWORD(wparam), (HWND)lparam);
  212. case WM_NOTIFY:
  213. return RouteNotificationMessage(pDlg, (NMHDR*)lparam);
  214. break;
  215. case WM_DESTROY:
  216. return pDlg->OnDestroyDialog();
  217. break;
  218. default:
  219. // Message was not processed
  220. //
  221. return FALSE;
  222. } // switch (uMessage)
  223. } // C_Dlg::DlgProc
  224. //////////////////////////////////////////////////////////////////////////////
  225. //
  226. // RouteNotificationMessage
  227. //
  228. // Routes notification messages from wizard buttons to the appropriate page
  229. // and handler.
  230. //
  231. // parameters:
  232. // hwndDlg window handle of page to which message is to be sent
  233. // pnmhdr pointer to the NMHDR structure containing info about
  234. // the particular notification
  235. //
  236. // returns:
  237. // TRUE if the message is processed
  238. // FALSE otherwise
  239. //
  240. //////////////////////////////////////////////////////////////////////////////
  241. BOOL
  242. C_Dlg::RouteNotificationMessage(
  243. C_Dlg* pDlg,
  244. NMHDR* pnmhdr
  245. )
  246. {
  247. if (NULL == pDlg)
  248. {
  249. //FIX220699
  250. return FALSE;
  251. //return E_INVALIDARG;
  252. }
  253. // If any specific notifications are to be handled, switch on pnmhdr->code.
  254. //
  255. return pDlg->OnNotify(pnmhdr);
  256. } // C_Dlg::RouteNotificationMessage
  257. //////////////////////////////////////////////////////////////////////////////
  258. //
  259. // LinkHwnd
  260. //
  261. // Store the pointer to this object in a window handle property. This
  262. // provides a way to get to the object when all that is known is the HWND.
  263. // Particularly useful in window procedures.
  264. //
  265. // parameters:
  266. // None.
  267. //
  268. // returns:
  269. // TRUE if the operation is successful
  270. // FALSE otherwise
  271. //
  272. //////////////////////////////////////////////////////////////////////////////
  273. BOOL
  274. C_Dlg::LinkHwnd( )
  275. {
  276. ASSERT(IsWindow(m_hwnd));
  277. if (! IsWindow(m_hwnd))
  278. {
  279. return FALSE;
  280. }
  281. return SetProp(m_hwnd, SZ_HWND_PROP, (HANDLE)this);
  282. } // C_Dlg::LinkHwnd
  283. //////////////////////////////////////////////////////////////////////////////
  284. //
  285. // UnlinkHwnd
  286. //
  287. // Remove the pointer to the associated object from a window handle. The
  288. // pointer must have been set with LinkHwnd( ).
  289. //
  290. // parameters:
  291. // None.
  292. //
  293. // returns:
  294. // TRUE if the window handle is removed and it is a pointer to
  295. // this object
  296. // FALSE otherwise
  297. //
  298. //////////////////////////////////////////////////////////////////////////////
  299. BOOL
  300. C_Dlg::UnlinkHwnd( )
  301. {
  302. ASSERT(IsWindow(m_hwnd));
  303. if (! IsWindow(m_hwnd))
  304. {
  305. return FALSE;
  306. }
  307. C_Dlg* pDlg = (C_Dlg*)RemoveProp(m_hwnd, SZ_HWND_PROP);
  308. ASSERT(this == pDlg);
  309. return (this == pDlg);
  310. } // C_Dlg::UnlinkHwnd
  311. //////////////////////////////////////////////////////////////////////////////
  312. //
  313. // DlgFromHwnd
  314. //
  315. // Retrieves the pointer to the associated object from a window handle. The
  316. // pointer was stored in a property by LinkHwnd( ).
  317. //
  318. // parameters:
  319. // hwnd the window handle containing the pointer
  320. // ppDlg pointer to a buffer that will receive the pointer to
  321. // the C_Dlg object
  322. //
  323. // returns:
  324. // S_OK if the operation is successful
  325. // E_INVALIDARG if hwnd is not a valid window or ppDlg is NULL
  326. // E_POINTER if the retrieved pointer is NULL
  327. //
  328. //////////////////////////////////////////////////////////////////////////////
  329. HRESULT
  330. C_Dlg::DlgFromHwnd(
  331. HWND hwnd,
  332. C_Dlg** ppDlg
  333. )
  334. {
  335. if (! ::IsWindow(hwnd))
  336. {
  337. return (E_INVALIDARG);
  338. }
  339. ASSERT(NULL != ppDlg);
  340. if (NULL == ppDlg)
  341. {
  342. return (E_INVALIDARG);
  343. }
  344. *ppDlg = (C_Dlg*) GetProp(hwnd, SZ_HWND_PROP);
  345. if (NULL == *ppDlg)
  346. {
  347. return (E_POINTER);
  348. }
  349. return (S_OK);
  350. } // C_Dlg::DlgFromHwnd
  351. //
  352. ///// End of file: Dlg.cpp ////////////////////////////////////////////////