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.

201 lines
5.6 KiB

  1. // PromptUserDlg.cpp: implementation of the CPromptUserDlg class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "pch.h"
  5. #include "PromptUserDlg.h"
  6. #include "Resource.h"
  7. #include "windowsx.h"
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. HINSTANCE CPromptUserDlg::m_hInstance = NULL;
  12. #define TIMER_INTERVAL 5000 //5 sec interval
  13. #define TOTAL_TIME_ELAPSE 300000 //Lesser number for testing purposes
  14. // For 5 mins 300000
  15. // Waiting time : 5 mins interval
  16. const TCHAR g_szDialogPtr[] = TEXT("PromptUser_DialogPtr");
  17. void CPromptUserDlg::SetInstanceHandle(HINSTANCE hInstance)
  18. {
  19. m_hInstance = hInstance;
  20. }
  21. CPromptUserDlg::CPromptUserDlg(WORD wDlgResourceId, BOOL fEnableYes, BOOL fEnableNo )
  22. {
  23. m_nIDTimer = 0;
  24. m_wDlgResourceId = wDlgResourceId;
  25. m_ElapsedTime = 0;
  26. m_fEnableYes = fEnableYes;
  27. m_fEnableNo = fEnableNo;
  28. }
  29. CPromptUserDlg::~CPromptUserDlg()
  30. {
  31. }
  32. INT CPromptUserDlg::DoModal(HWND hWndParent = NULL)
  33. {
  34. return (INT)DialogBoxParam(m_hInstance,
  35. MAKEINTRESOURCE(m_wDlgResourceId),
  36. hWndParent,
  37. (DLGPROC)CPromptUserDlg::_DlgProc,
  38. (LPARAM)this);
  39. }
  40. INT_PTR CALLBACK
  41. CPromptUserDlg::_DlgProc( // [static]
  42. HWND hwnd,
  43. UINT uMsg,
  44. WPARAM wParam,
  45. LPARAM lParam
  46. )
  47. {
  48. CPromptUserDlg *pThis = NULL;
  49. if (WM_INITDIALOG == uMsg)
  50. {
  51. pThis = (CPromptUserDlg *)lParam;
  52. if (!SetProp(hwnd, g_szDialogPtr, (HANDLE)pThis))
  53. {
  54. pThis = NULL;
  55. }
  56. }
  57. else
  58. {
  59. pThis = (CPromptUserDlg *)GetProp(hwnd, g_szDialogPtr);
  60. }
  61. if (NULL != pThis)
  62. {
  63. switch(uMsg)
  64. {
  65. HANDLE_MSG(hwnd, WM_INITDIALOG, pThis->_OnInitDialog);
  66. HANDLE_MSG(hwnd, WM_COMMAND, pThis->_OnCommand);
  67. HANDLE_MSG(hwnd, WM_DESTROY, pThis->_OnDestroy);
  68. HANDLE_MSG(hwnd, WM_TIMER, pThis->_OnTimer);
  69. HANDLE_MSG(hwnd, WM_ENDSESSION, pThis->_OnEndSession);
  70. default:
  71. break;
  72. }
  73. }
  74. return (FALSE);
  75. }
  76. extern HWND ghCurrentMainDlg;
  77. extern HWND ghCurrentDialog;
  78. BOOL CPromptUserDlg::_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  79. {
  80. // set icons
  81. HICON hIcon = (HICON)::LoadImage(m_hInstance, MAKEINTRESOURCE(IDI_AUICON),
  82. IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
  83. ::SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
  84. HICON hIconSmall = (HICON)::LoadImage(m_hInstance, MAKEINTRESOURCE(IDI_AUICON),
  85. IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
  86. ::SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSmall);
  87. EnableWindow(GetDlgItem(hwnd, IDYES), m_fEnableYes);
  88. EnableWindow(GetDlgItem(hwnd, IDNO), m_fEnableNo);
  89. ghCurrentMainDlg = hwnd;
  90. ghCurrentDialog = hwnd;
  91. m_nIDTimer = SetTimer(hwnd,m_nIDTimer,TIMER_INTERVAL,(TIMERPROC)NULL);
  92. m_ProgressBar = GetDlgItem(hwnd,IDC_PROG_TIME);
  93. SendMessage(m_ProgressBar,PBM_SETRANGE, 0, MAKELPARAM(0,TOTAL_TIME_ELAPSE / TIMER_INTERVAL));
  94. SendMessage(m_ProgressBar,PBM_SETSTEP,(WPARAM)1,(LPARAM)0);
  95. EnableMenuItem(GetSystemMenu(hwnd, FALSE), SC_CLOSE, MF_GRAYED);
  96. UpdateStatus(hwnd);
  97. // SetActiveWindow(ghCurrentMainDlg);
  98. SetForegroundWindow(ghCurrentMainDlg);
  99. return TRUE;
  100. }
  101. void CPromptUserDlg::_OnDestroy (HWND hwnd) {
  102. ghCurrentMainDlg = NULL;
  103. }
  104. BOOL CPromptUserDlg::_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  105. {
  106. switch(id)
  107. {
  108. case IDNO:
  109. case IDYES:
  110. if(BN_CLICKED == codeNotify)
  111. {
  112. KillTimer(hwnd,m_nIDTimer);
  113. EndDialog(hwnd,id);
  114. }
  115. break;
  116. }
  117. return TRUE;
  118. }
  119. void CPromptUserDlg::_OnTimer(HWND hwnd, UINT id)
  120. {
  121. m_ElapsedTime += TIMER_INTERVAL;
  122. if (m_ElapsedTime <= TOTAL_TIME_ELAPSE)
  123. {
  124. //Update Progress bar
  125. SendMessage(m_ProgressBar,PBM_STEPIT,0,0);
  126. UpdateStatus(hwnd);
  127. }
  128. if (m_ElapsedTime == TOTAL_TIME_ELAPSE)
  129. {
  130. KillTimer(hwnd,m_nIDTimer);
  131. //On end of TOTAL_TIME_ELAPSE send message idyes to the dialog.
  132. EndDialog(hwnd,AU_IDTIMEOUT);
  133. }
  134. }
  135. void CPromptUserDlg::UpdateStatus(HWND hwnd)
  136. {
  137. TCHAR tszCountdownFormat[81];//see
  138. TCHAR tszCountdown[160];
  139. TCHAR strFormat[200];
  140. TCHAR strUpdate[200];
  141. DWORD dwResId = 0;
  142. SYSTEMTIME st;
  143. ZeroMemory(&st, sizeof(st));
  144. st.wMinute = ((TOTAL_TIME_ELAPSE - m_ElapsedTime) / 1000) / 60;
  145. st.wSecond = ((TOTAL_TIME_ELAPSE - m_ElapsedTime) / 1000) % 60;
  146. switch(m_wDlgResourceId) {
  147. case IDD_START_INSTALL:
  148. dwResId = IDS_START_INSTALL;
  149. break;
  150. case IDD_PROMPT_RESTART:
  151. dwResId = IDS_PROMPT_RESTART;
  152. break;
  153. }
  154. if (LoadString(m_hInstance, IDS_COUNTDOWN_FORMAT, tszCountdownFormat, ARRAYSIZE(tszCountdownFormat)) &&
  155. LoadString(m_hInstance, dwResId, strFormat, ARRAYSIZE(strFormat)) &&
  156. GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, &st, tszCountdownFormat, tszCountdown, ARRAYSIZE(tszCountdown)))
  157. {
  158. (void)StringCchPrintfEx(strUpdate, ARRAYSIZE(strUpdate), NULL, NULL, MISTSAFE_STRING_FLAGS, strFormat, tszCountdown);
  159. //fixcode: check return value of GetDlgItem()
  160. SetWindowText(GetDlgItem(hwnd,IDC_STAT_COUNTER),strUpdate);
  161. }
  162. }
  163. void CPromptUserDlg::_OnEndSession(HWND hwnd, BOOL fEnding)
  164. {
  165. DEBUGMSG("OnEndSession: ending = %s", fEnding ? "TRUE" : "FALSE");
  166. KillTimer(hwnd,m_nIDTimer);
  167. //On end of TOTAL_TIME_ELAPSE send message idyes to the dialog.
  168. EnableMenuItem(GetSystemMenu(hwnd, FALSE), SC_CLOSE, MF_ENABLED);
  169. EndDialog(hwnd,AU_IDTIMEOUT);
  170. }