XTPTaskDialogFrame.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:13k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // XTPTaskDialogFrame.cpp: implementation of the CXTPTaskDialogFrame class.
  2. //
  3. // This file is a part of the XTREME CONTROLS MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. #include "Resource.h"
  22. #include "Common/XTPImageManager.h"
  23. #include "Common/XTPDrawHelpers.h"
  24. #include "Common/XTPVC80Helpers.h"
  25. #include "Controls/XTThemeManager.h"
  26. #include "Controls/XTButtonTheme.h"
  27. #include "Controls/XTButton.h"
  28. #include "XTPTaskDialogAPI.h"
  29. #include "XTPTaskDialogFrame.h"
  30. #ifdef _DEBUG
  31. #define new DEBUG_NEW
  32. #undef THIS_FILE
  33. static char THIS_FILE[] = __FILE__;
  34. #endif
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CXTPTaskDialogFrame
  37. CXTPTaskDialogFrame::CXTPTaskDialogFrame(TASKDIALOGCONFIG* pConfig)
  38. {
  39. m_pConfig = pConfig;
  40. ASSERT(m_pConfig != NULL);
  41. m_lpDlgTemplate = NULL;
  42. m_dwTickCount = 0;
  43. }
  44. CXTPTaskDialogFrame::~CXTPTaskDialogFrame()
  45. {
  46. DestroyWindow();
  47. free(m_lpDlgTemplate);
  48. }
  49. BOOL CXTPTaskDialogFrame::OnInitDialog()
  50. {
  51. BOOL bResult = CXTPTaskDialogClient::OnInitDialog();
  52. int nDefaultButton = m_pConfig->nDefaultButton;
  53. if (nDefaultButton == 0)
  54. {
  55. if (m_arrLinkButtons.GetSize() > 0)
  56. {
  57. nDefaultButton = m_arrLinkButtons[0]->GetDlgCtrlID();
  58. }
  59. else if (m_arrCommandButtons.GetSize() > 0)
  60. {
  61. nDefaultButton = m_arrCommandButtons[0]->GetDlgCtrlID();
  62. }
  63. }
  64. CWnd* pWnd = nDefaultButton > 0 ? GetDlgItem(nDefaultButton) : NULL;
  65. if (pWnd)
  66. {
  67. SetDefID(nDefaultButton);
  68. PostMessage(WM_NEXTDLGCTL, (WPARAM)pWnd->GetSafeHwnd(), TRUE);
  69. }
  70. m_dwTickCount = GetTickCount();
  71. if (m_pConfig->dwFlags & TDF_CALLBACK_TIMER)
  72. {
  73. SetTimer(1, 200, NULL);
  74. }
  75. return bResult;
  76. }
  77. BOOL CXTPTaskDialogFrame::Create(CWnd* pParentWnd)
  78. {
  79. if (m_lpDlgTemplate == NULL)
  80. {
  81. const int nDlgTemplateSize = sizeof(DLGTEMPLATE) + sizeof(DWORD) * 8;
  82. m_lpDlgTemplate =  (DLGTEMPLATE*)malloc(nDlgTemplateSize);
  83. ZeroMemory(m_lpDlgTemplate, nDlgTemplateSize);
  84. }
  85. if (!m_lpDlgTemplate)
  86. return FALSE;
  87. m_lpDlgTemplate->style = GetFrameStyle() | WS_VISIBLE;
  88. m_lpDlgTemplate->dwExtendedStyle = m_pConfig->dwFlags & TDF_RTL_LAYOUT ? WS_EX_LAYOUTRTL : 0;
  89. return CreateIndirect(m_lpDlgTemplate, pParentWnd ? pParentWnd : m_pParentWnd, NULL);
  90. }
  91. INT_PTR CXTPTaskDialogFrame::DoModal()
  92. {
  93. if (m_lpDlgTemplate == NULL)
  94. {
  95. const int nDlgTemplateSize = sizeof(DLGTEMPLATE) + sizeof(DWORD) * 8;
  96. m_lpDlgTemplate =  (DLGTEMPLATE*)malloc(nDlgTemplateSize);
  97. ZeroMemory(m_lpDlgTemplate, nDlgTemplateSize);
  98. }
  99. if (!m_lpDlgTemplate)
  100. return -1;
  101. m_lpDlgTemplate->style = GetFrameStyle();
  102. m_lpDlgTemplate->dwExtendedStyle = m_pConfig->dwFlags & TDF_RTL_LAYOUT ? WS_EX_LAYOUTRTL : 0;
  103. if (m_lpDialogTemplate == 0)
  104. {
  105. InitModalIndirect(m_lpDlgTemplate);
  106. }
  107. return CDialog::DoModal();
  108. }
  109. BEGIN_MESSAGE_MAP(CXTPTaskDialogFrame, CXTPTaskDialogClient)
  110. //{{AFX_MSG_MAP(CXTPTaskDialogFrame)
  111. ON_WM_CREATE()
  112. ON_WM_DESTROY()
  113. ON_WM_TIMER()
  114. //}}AFX_MSG_MAP
  115. ON_MESSAGE(TDM_SET_ELEMENT_TEXT, OnDialogSetElementText)
  116. ON_MESSAGE(TDM_CLICK_BUTTON, OnDialogClickButton)
  117. ON_MESSAGE(TDM_CLICK_RADIO_BUTTON, OnDialogClickRadioButton)
  118. ON_MESSAGE(TDM_ENABLE_BUTTON, OnDialogEnableButton)
  119. ON_MESSAGE(TDM_ENABLE_RADIO_BUTTON, OnDialogEnableRadioButton)
  120. ON_MESSAGE(TDM_CLICK_VERIFICATION, OnDialogClickVerificationButton)
  121. ON_MESSAGE(TDM_UPDATE_ICON, OnDialogUpdateIcon)
  122. ON_MESSAGE(TDM_SET_BUTTON_ELEVATION_REQUIRED_STATE, OnDialogSetButtonElevationState)
  123. ON_MESSAGE(TDM_SET_MARQUEE_PROGRESS_BAR, OnDialogSetMarqueeProgressBar)
  124. ON_MESSAGE(TDM_SET_PROGRESS_BAR_STATE, OnDialogSetProgressBarState)
  125. ON_MESSAGE(TDM_SET_PROGRESS_BAR_RANGE, OnDialogSetProgressBarRange)
  126. ON_MESSAGE(TDM_SET_PROGRESS_BAR_POS, OnDialogSetProgressBarPos)
  127. ON_MESSAGE(TDM_SET_PROGRESS_BAR_MARQUEE, OnDialogStartProgressBarMarquee)
  128. ON_MESSAGE(TDM_NAVIGATE_PAGE, OnDialogNavigatePage)
  129. END_MESSAGE_MAP()
  130. /////////////////////////////////////////////////////////////////////////////
  131. // CXTPTaskDialogFrame message handlers
  132. CString CXTPTaskDialogFrame::GetFrameTitle() const
  133. {
  134. CString strWindowTitle;
  135. if (m_pConfig->pszWindowTitle != NULL)
  136. {
  137. if (IS_INTRESOURCE(m_pConfig->pszWindowTitle))
  138. {
  139. XTPLoadStringInst(m_pConfig->hInstance,
  140. (UINT)(UINT_PTR)m_pConfig->pszWindowTitle, &strWindowTitle);
  141. }
  142. else
  143. {
  144. strWindowTitle = m_pConfig->pszWindowTitle;
  145. }
  146. }
  147. if (strWindowTitle.IsEmpty())
  148. {
  149. TCHAR szModuleName[_MAX_PATH];
  150. ::GetModuleFileName(AfxGetInstanceHandle(), szModuleName, _MAX_PATH);
  151. TCHAR szFileName[_MAX_FNAME], szExt[_MAX_EXT];
  152. SPLITPATH_S(szModuleName, NULL, NULL, szFileName, szExt);
  153. strWindowTitle.Format(_T("%s%s"), szFileName, szExt);
  154. }
  155. return strWindowTitle;
  156. }
  157. DWORD CXTPTaskDialogFrame::GetFrameStyle() const
  158. {
  159. DWORD dwStyle = WS_POPUP | WS_BORDER | WS_CAPTION | DS_ABSALIGN | DS_NOIDLEMSG | DS_3DLOOK | DS_MODALFRAME;
  160. dwStyle |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  161. if (m_pConfig->dwFlags & TDF_CAN_BE_MINIMIZED)
  162. {
  163. dwStyle |= WS_MINIMIZEBOX;
  164. }
  165. if ((m_pConfig->dwFlags & TDF_ALLOW_DIALOG_CANCELLATION) || (m_pConfig->dwCommonButtons & TDCBF_CANCEL_BUTTON))
  166. {
  167. dwStyle |= WS_SYSMENU;
  168. }
  169. return dwStyle;
  170. }
  171. int CXTPTaskDialogFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  172. {
  173. if (CXTPTaskDialogClient::OnCreate(lpCreateStruct) == -1)
  174. return -1;
  175. SetWindowText(GetFrameTitle());
  176. if (m_pConfig->pszMainIcon == TD_WARNING_ICON)
  177. MessageBeep(MB_ICONEXCLAMATION);
  178. if (m_pConfig->pszMainIcon == TD_ERROR_ICON)
  179. MessageBeep(MB_ICONERROR);
  180. if (m_pConfig->pszMainIcon == TD_INFORMATION_ICON)
  181. MessageBeep(MB_ICONINFORMATION);
  182. if (!CreateClient(TRUE))
  183. {
  184. TRACE0("Failed to create Task Dialog UI window.n");
  185. return FALSE;
  186. }
  187. // inform callback Task Dialog was constructed.
  188. SendNotify(TDN_DIALOG_CONSTRUCTED);
  189. // inform callback Task Dialog was created.
  190. SendNotify(TDN_CREATED);
  191. return 0;
  192. }
  193. void CXTPTaskDialogFrame::OnDestroy()
  194. {
  195. CXTPTaskDialogClient::OnDestroy();
  196. // inform callback Task Dialog was destroyed.
  197. SendNotify(TDN_DESTROYED);
  198. }
  199. BOOL CXTPTaskDialogFrame::PreTranslateMessage(MSG* pMsg)
  200. {
  201. if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_KEYUP)
  202. {
  203. switch (pMsg->wParam)
  204. {
  205. case VK_F1:
  206. {
  207. // inform callback F1 help key was pressed.
  208. SendNotify(TDN_HELP);
  209. return TRUE;
  210. }
  211. }
  212. }
  213. return CXTPTaskDialogClient::PreTranslateMessage(pMsg);
  214. }
  215. void CXTPTaskDialogFrame::OnTimer(UINT_PTR nIDEvent)
  216. {
  217. if (nIDEvent == 1)
  218. {
  219. DWORD dwTickCount = GetTickCount() - m_dwTickCount;
  220. if ((int)SendNotify(TDN_TIMER, dwTickCount) == 1)
  221. {
  222. m_dwTickCount = GetTickCount();
  223. }
  224. }
  225. CXTPTaskDialogClient::OnTimer(nIDEvent);
  226. }
  227. LRESULT CXTPTaskDialogFrame::OnDialogSetElementText(WPARAM wParam, LPARAM lParam)
  228. {
  229. if (wParam == TDE_MAIN_INSTRUCTION)
  230. {
  231. m_strMainInstruction.SetCaption(LoadItemString((LPCWSTR)lParam));
  232. RecalcLayout();
  233. return TRUE;
  234. }
  235. if (wParam == TDE_CONTENT)
  236. {
  237. m_strContent.SetCaption(LoadItemString((LPCWSTR)lParam));
  238. m_wndContent.DestroyWindow();
  239. if (IsHyperlinksEnabled())
  240. {
  241. m_wndContent.Create(CRect(0, 0, 0, 0), m_strContent.m_strCaption, &m_font, this);
  242. }
  243. RecalcLayout();
  244. return TRUE;
  245. }
  246. if (wParam == TDE_EXPANDED_INFORMATION)
  247. {
  248. m_strExpandedInformation.SetCaption(LoadItemString((LPCWSTR)lParam));
  249. m_wndExpandedInformation.DestroyWindow();
  250. RecalcLayout();
  251. return TRUE;
  252. }
  253. if (wParam == TDE_FOOTER)
  254. {
  255. m_strFooter.SetCaption(LoadItemString((LPCWSTR)lParam));
  256. m_wndFooter.DestroyWindow();
  257. if (IsHyperlinksEnabled())
  258. {
  259. m_wndFooter.Create(CRect(0, 0, 0, 0), m_strFooter.m_strCaption, &m_font, this);
  260. }
  261. RecalcLayout();
  262. return TRUE;
  263. }
  264. return FALSE;
  265. }
  266. LRESULT CXTPTaskDialogFrame::OnDialogClickButton(WPARAM wParam, LPARAM /*lParam*/)
  267. {
  268. CWnd* pWnd = GetDlgItem((UINT)wParam);
  269. if (!pWnd)
  270. return FALSE;
  271. WindowProc(WM_COMMAND, MAKEWPARAM((UINT)wParam, BN_CLICKED), (LPARAM)pWnd->GetSafeHwnd());
  272. return TRUE;
  273. }
  274. LRESULT CXTPTaskDialogFrame::OnDialogClickRadioButton(WPARAM wParam, LPARAM /*lParam*/)
  275. {
  276. CButton* pWnd = DYNAMIC_DOWNCAST(CButton, GetDlgItem((UINT)wParam));
  277. if (!pWnd)
  278. return FALSE;
  279. if ((pWnd->SendMessage(WM_GETDLGCODE) & DLGC_RADIOBUTTON) == 0)
  280. return FALSE;
  281. HWND hWndCtrl = ::GetWindow(m_hWnd, GW_CHILD);
  282. while (hWndCtrl)
  283. {
  284. if (::SendMessage(hWndCtrl, WM_GETDLGCODE, 0, 0L) & DLGC_RADIOBUTTON)
  285. {
  286. ::SendMessage(hWndCtrl, BM_SETCHECK, hWndCtrl == pWnd->GetSafeHwnd(), 0L);
  287. }
  288. hWndCtrl = ::GetWindow(hWndCtrl, GW_HWNDNEXT);
  289. }
  290. m_nSelRadioButtonID = (UINT)wParam;
  291. SendNotify(TDN_RADIO_BUTTON_CLICKED, (WPARAM)m_nSelRadioButtonID);
  292. return TRUE;
  293. }
  294. LRESULT CXTPTaskDialogFrame::OnDialogEnableButton(WPARAM wParam, LPARAM lParam)
  295. {
  296. CWnd* pWnd = DYNAMIC_DOWNCAST(CButton, GetDlgItem((UINT)wParam));
  297. if (!pWnd)
  298. return FALSE;
  299. if (!lParam && (::GetFocus() == pWnd->GetSafeHwnd()))
  300. {
  301. CWnd* pWndNext = GetNextDlgTabItem(pWnd);
  302. PostMessage(WM_NEXTDLGCTL, (WPARAM)pWndNext->GetSafeHwnd(), TRUE);
  303. }
  304. pWnd->EnableWindow(lParam != 0);
  305. return TRUE;
  306. }
  307. LRESULT CXTPTaskDialogFrame::OnDialogEnableRadioButton(WPARAM wParam, LPARAM lParam)
  308. {
  309. CButton* pWnd = DYNAMIC_DOWNCAST(CButton, GetDlgItem((UINT)wParam));
  310. if (!pWnd)
  311. return FALSE;
  312. if ((pWnd->SendMessage(WM_GETDLGCODE) & DLGC_RADIOBUTTON) == 0)
  313. return FALSE;
  314. pWnd->EnableWindow(lParam != 0);
  315. return TRUE;
  316. }
  317. LRESULT CXTPTaskDialogFrame::OnDialogClickVerificationButton(WPARAM wParam, LPARAM lParam)
  318. {
  319. if (!m_pBtnVerify)
  320. return FALSE;
  321. m_bVerification = (wParam != 0);
  322. m_pBtnVerify->SetCheck(m_bVerification);
  323. SendNotify(TDN_VERIFICATION_CLICKED, m_bVerification);
  324. if (lParam)
  325. {
  326. m_pBtnVerify->SetFocus();
  327. }
  328. return TRUE;
  329. }
  330. LRESULT CXTPTaskDialogFrame::OnDialogUpdateIcon(WPARAM wParam, LPARAM lParam)
  331. {
  332. if (wParam == TDIE_ICON_MAIN)
  333. {
  334. SAFE_DELETE_HICON(m_hIconMain);
  335. m_hIconMain = CreateIcon((HICON)lParam, (PCWSTR)lParam, m_sizeIconMain, IsMainIconHandleUsed());
  336. RecalcLayout();
  337. return TRUE;
  338. }
  339. if (wParam == TDIE_ICON_FOOTER)
  340. {
  341. SAFE_DELETE_HICON(m_hIconFooter);
  342. m_hIconFooter = CreateIcon((HICON)lParam, (PCWSTR)lParam, m_sizeIconFooter, IsFooterIconHandleUsed());
  343. RecalcLayout();
  344. return TRUE;
  345. }
  346. return FALSE;
  347. }
  348. #ifndef BCM_FIRST
  349. #define BCM_FIRST               0x1600      // Button control messages
  350. #endif
  351. #ifndef BCM_SETSHIELD
  352. #define BCM_SETSHIELD            (BCM_FIRST + 0x000C)
  353. #endif
  354. LRESULT CXTPTaskDialogFrame::OnDialogSetButtonElevationState(WPARAM wParam, LPARAM lParam)
  355. {
  356. CButton* pWnd = DYNAMIC_DOWNCAST(CButton, GetDlgItem((UINT)wParam));
  357. if (!pWnd)
  358. return FALSE;
  359. pWnd->SendMessage(BCM_SETSHIELD, 0, lParam);
  360. return TRUE;
  361. }
  362. #ifndef PBM_SETMARQUEE
  363. #define PBM_SETMARQUEE          (WM_USER+10)
  364. #endif
  365. #ifndef PBS_MARQUEE
  366. #define PBS_MARQUEE             0x08
  367. #endif
  368. #ifndef PBM_SETSTATE
  369. #define PBM_SETSTATE            (WM_USER+16)
  370. #endif
  371. LRESULT CXTPTaskDialogFrame::OnDialogSetMarqueeProgressBar(WPARAM wParam, LPARAM /*lParam*/)
  372. {
  373. if (!m_wndProgressBar)
  374. return FALSE;
  375. m_wndProgressBar.ModifyStyle(PBS_MARQUEE, wParam ? PBS_MARQUEE : 0);
  376. return TRUE;
  377. }
  378. LRESULT CXTPTaskDialogFrame::OnDialogSetProgressBarState(WPARAM wParam, LPARAM /*lParam*/)
  379. {
  380. if (!m_wndProgressBar)
  381. return FALSE;
  382. m_wndProgressBar.SendMessage(PBM_SETSTATE, wParam);
  383. return TRUE;
  384. }
  385. LRESULT CXTPTaskDialogFrame::OnDialogSetProgressBarRange(WPARAM /*wParam*/, LPARAM lParam)
  386. {
  387. if (!m_wndProgressBar)
  388. return FALSE;
  389. m_wndProgressBar.SetRange(LOWORD(lParam), HIWORD(lParam));
  390. return TRUE;
  391. }
  392. LRESULT CXTPTaskDialogFrame::OnDialogSetProgressBarPos(WPARAM wParam, LPARAM /*lParam*/)
  393. {
  394. if (!m_wndProgressBar)
  395. return FALSE;
  396. m_wndProgressBar.SetPos((int)wParam);
  397. return TRUE;
  398. }
  399. LRESULT CXTPTaskDialogFrame::OnDialogStartProgressBarMarquee(WPARAM wParam, LPARAM lParam)
  400. {
  401. if (!m_wndProgressBar)
  402. return FALSE;
  403. m_wndProgressBar.SendMessage(PBM_SETMARQUEE, wParam, wParam ? lParam : 0);
  404. return TRUE;
  405. }
  406. LRESULT CXTPTaskDialogFrame::OnDialogNavigatePage(WPARAM /*wParam*/, LPARAM lParam)
  407. {
  408. TASKDIALOGCONFIG* pConfig = (TASKDIALOGCONFIG*)lParam;
  409. m_pConfig = pConfig;
  410. KillTimer(1);
  411. SetWindowText(GetFrameTitle());
  412. if (!CreateClient(FALSE))
  413. {
  414. TRACE0("Failed to create Task Dialog UI window.n");
  415. return FALSE;
  416. }
  417. OnInitDialog();
  418. // inform callback Task Dialog was constructed.
  419. SendNotify(TDN_DIALOG_CONSTRUCTED);
  420. SendNotify(TDN_NAVIGATED);
  421. return TRUE;
  422. }