PPTtimerDlg.cpp
上传用户:chpname
上传日期:2018-11-08
资源大小:69k
文件大小:7k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // PPTtimerDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "PPTtimer.h"
  5. #include "PPTtimerDlg.h"
  6. #include "SetDlg.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CPPTtimerDlg dialog
  14. void ExportInt(char* buff, BYTE& hour, BYTE& minute, BYTE& second);
  15. CPPTtimerDlg::CPPTtimerDlg(CWnd* pParent /*=NULL*/)
  16. : CDialog(CPPTtimerDlg::IDD, pParent)
  17. {
  18. //{{AFX_DATA_INIT(CPPTtimerDlg)
  19. m_PrintTime = _T("请先设定时间");
  20. //}}AFX_DATA_INIT
  21. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  22. m_FlashFlag=PPT_NOTFLASH;
  23. m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON_MAIN);
  24. }
  25. void CPPTtimerDlg::DoDataExchange(CDataExchange* pDX)
  26. {
  27. CDialog::DoDataExchange(pDX);
  28. //{{AFX_DATA_MAP(CPPTtimerDlg)
  29. DDX_Text(pDX, IDC_STATIC_TIME, m_PrintTime);
  30. //}}AFX_DATA_MAP
  31. }
  32. BEGIN_MESSAGE_MAP(CPPTtimerDlg, CDialog)
  33. //{{AFX_MSG_MAP(CPPTtimerDlg)
  34. ON_WM_PAINT()
  35. ON_WM_QUERYDRAGICON()
  36. ON_WM_RBUTTONDOWN()
  37. ON_WM_LBUTTONDOWN()
  38. ON_WM_CREATE()
  39. ON_WM_TIMER()
  40. ON_WM_CTLCOLOR()
  41. //}}AFX_MSG_MAP
  42. ON_BN_CLICKED(ID_SYSMENU_SET,OnSysMenuSet)
  43. ON_BN_CLICKED(ID_SYSMENU_CLOSE,OnSysMenuClose)
  44. ON_BN_CLICKED(ID_SYSMENU_STOP,OnSysMenuStop)
  45. ON_BN_CLICKED(ID_SYSMENU_RESTART,OnSysMenuReStart)
  46. END_MESSAGE_MAP()
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CPPTtimerDlg message handlers
  49. BOOL CPPTtimerDlg::OnInitDialog()
  50. {
  51. CDialog::OnInitDialog();
  52. // Set the icon for this dialog.  The framework does this automatically
  53. //  when the application's main window is not a dialog
  54. SetIcon(m_hIcon, TRUE); // Set big icon
  55. SetIcon(m_hIcon, FALSE); // Set small icon
  56. //始终在最上面显示窗口
  57. CRect rect;
  58. GetClientRect(&rect);
  59. ::SetWindowPos(AfxGetMainWnd()->m_hWnd,HWND_TOPMOST,rect.left,rect.top,rect.right,rect.bottom,SWP_NOMOVE);
  60. //设置半透明窗口
  61. SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(),GWL_EXSTYLE)|0x80000);
  62. typedef BOOL (WINAPI *FSetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
  63. FSetLayeredWindowAttributes SetLayeredWindowAttributes;
  64. HINSTANCE hInst=LoadLibrary("User32.Dll");
  65. SetLayeredWindowAttributes=(FSetLayeredWindowAttributes)GetProcAddress(hInst,"SetLayeredWindowAttributes");
  66. if(SetLayeredWindowAttributes)
  67. SetLayeredWindowAttributes(GetSafeHwnd(),RGB(0,0,0),128,2);
  68. FreeLibrary(hInst);
  69. //隐藏标题栏
  70. /*LONG lStyle=::GetWindowLong(this->m_hWnd,GWL_STYLE);
  71. ::SetWindowLong(this->m_hWnd,GWL_STYLE,lStyle & ~WS_CAPTION);
  72. ::SetWindowPos(this->m_hWnd,NULL,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED);*/
  73. //隐藏窗体在任务栏中的显示
  74. ShowWindow(SW_HIDE);
  75. ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW,0);
  76. return TRUE;  // return TRUE  unless you set the focus to a control
  77. }
  78. // If you add a minimize button to your dialog, you will need the code below
  79. //  to draw the icon.  For MFC applications using the document/view model,
  80. //  this is automatically done for you by the framework.
  81. void CPPTtimerDlg::OnPaint() 
  82. {
  83. if (IsIconic())
  84. {
  85. CPaintDC dc(this); // device context for painting
  86. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  87. // Center icon in client rectangle
  88. int cxIcon = GetSystemMetrics(SM_CXICON);
  89. int cyIcon = GetSystemMetrics(SM_CYICON);
  90. CRect rect;
  91. GetClientRect(&rect);
  92. int x = (rect.Width() - cxIcon + 1) / 2;
  93. int y = (rect.Height() - cyIcon + 1) / 2;
  94. // Draw the icon
  95. dc.DrawIcon(x, y, m_hIcon);
  96. }
  97. else
  98. {
  99. CDialog::OnPaint();
  100. }
  101. }
  102. // The system calls this to obtain the cursor to display while the user drags
  103. //  the minimized window.
  104. HCURSOR CPPTtimerDlg::OnQueryDragIcon()
  105. {
  106. return (HCURSOR) m_hIcon;
  107. }
  108. void CPPTtimerDlg::OnRButtonDown(UINT nFlags, CPoint point) 
  109. {
  110. CPoint pt;
  111. CMenu SysMenu;
  112. GetCursorPos(&pt);
  113. if(SysMenu.LoadMenu(IDR_SYS_MENU))
  114. {
  115. CMenu * pSubMenu=SysMenu.GetSubMenu(0);
  116. if(pSubMenu!=NULL)
  117. {
  118. if(m_FlashFlag==PPT_TIME_STOP)
  119. {
  120. pSubMenu->EnableMenuItem(ID_SYSMENU_STOP,MF_GRAYED);
  121. pSubMenu->EnableMenuItem(ID_SYSMENU_RESTART,MF_ENABLED);
  122. }
  123. else if(m_FlashFlag==PPT_FIRST_RUN)
  124. {
  125. pSubMenu->EnableMenuItem(ID_SYSMENU_STOP,MF_GRAYED);
  126. pSubMenu->EnableMenuItem(ID_SYSMENU_RESTART,MF_GRAYED);
  127. }
  128. else if(m_FlashFlag==PPT_TIME_RESTART || m_FlashFlag==PPT_FLASH)
  129. {
  130. pSubMenu->EnableMenuItem(ID_SYSMENU_STOP,MF_ENABLED);
  131. pSubMenu->EnableMenuItem(ID_SYSMENU_RESTART,MF_GRAYED);
  132. }
  133. ::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);
  134. pSubMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,pt.x,pt.y,this);
  135. }
  136. }
  137. CDialog::OnRButtonDown(nFlags, point);
  138. }
  139. void CPPTtimerDlg::OnLButtonDown(UINT nFlags, CPoint point) 
  140. {
  141. PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
  142. CDialog::OnLButtonDown(nFlags, point);
  143. }
  144. BOOL CPPTtimerDlg::PreTranslateMessage(MSG* pMsg) 
  145. {
  146. //不使用ESC和Enter键关闭窗体
  147. if(pMsg->message==WM_KEYDOWN && (pMsg->wParam==27 || pMsg->wParam==13))
  148. return NULL;
  149. return CDialog::PreTranslateMessage(pMsg);
  150. }
  151. int CPPTtimerDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  152. {
  153. if (CDialog::OnCreate(lpCreateStruct) == -1)
  154. return -1;
  155. this->SetTimer(1,1000,0);//以秒为计时单位
  156. this->SetTimer(2,500,0);
  157. return 0;
  158. }
  159. void CPPTtimerDlg::OnTimer(UINT nIDEvent) 
  160. {
  161. //FlashWindow(TRUE);//闪烁标题栏
  162. if(nIDEvent==1 && m_FlashFlag==PPT_COUNTING)
  163. {
  164. BYTE hour, minute, second;
  165. ExportInt(m_CurrTime, hour, minute, second);
  166. if(second==0)
  167. {
  168. if(minute==0)
  169. {
  170. if(hour==0)
  171. {
  172. m_PrintTime="★=超时了=★";
  173. m_FlashFlag=PPT_FLASH;
  174. goto UpdateTime;
  175. }
  176. else
  177. {
  178. hour--;
  179. minute=59;
  180. second=59;
  181. }
  182. }
  183. else
  184. {
  185. minute--;
  186. second=59;
  187. }
  188. }
  189. else
  190. {
  191. second--;
  192. }
  193. m_PrintTime.Format("%2d:%2d:%2d",hour,minute,second);
  194. strcpy(m_CurrTime,m_PrintTime);
  195. UpdateTime: UpdateData(FALSE);
  196. }
  197. if(nIDEvent==2 && m_FlashFlag==PPT_FLASH)
  198. {
  199. if(!strcmp(m_PrintTime,"★=超时了=★")) m_PrintTime=" ";
  200. else m_PrintTime="★=超时了=★";
  201. UpdateData(FALSE);
  202. }
  203. CDialog::OnTimer(nIDEvent);
  204. }
  205. //设置控件颜色
  206. HBRUSH CPPTtimerDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  207. {
  208. HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  209. if(nCtlColor==CTLCOLOR_STATIC)
  210. {
  211. //pDC->SetBkColor(RGB(160,10,3));
  212. pDC->SetTextColor(RGB(255,0,0));//red color
  213. }
  214. // TODO: Return a different brush if the default is not desired
  215. return hbr;
  216. }
  217. void CPPTtimerDlg::OnSysMenuSet()
  218. {
  219. CSetDlg setdlg;
  220. if(setdlg.DoModal()==IDOK)
  221. {
  222. m_PrintTime.Format("%2d:%2d:%2d", setdlg.m_SetHour, setdlg.m_SetMinute, setdlg.m_SetSecond);
  223. strcpy(m_CurrTime,m_PrintTime);
  224. m_FlashFlag=PPT_COUNTING;
  225. UpdateData();
  226. }
  227. }
  228. void CPPTtimerDlg::OnSysMenuStop()
  229. {
  230. m_FlashFlag=PPT_TIME_STOP;
  231. }
  232. void CPPTtimerDlg::OnSysMenuReStart()
  233. {
  234. m_FlashFlag=PPT_TIME_RESTART;
  235. }
  236. void CPPTtimerDlg::OnSysMenuClose()
  237. {
  238. KillTimer(1);
  239. KillTimer(2);
  240. CDialog::OnOK();
  241. }
  242. void ExportInt(char* buff, BYTE& hour, BYTE& minute, BYTE& second)
  243. {
  244. char temBuff[3];
  245. hour=atoi(temBuff);
  246. temBuff[0]=buff[0];
  247. temBuff[1]=buff[1];
  248. temBuff[2]='n';
  249. hour=atoi(temBuff);
  250. temBuff[0]=buff[3];
  251. temBuff[1]=buff[4];
  252. temBuff[2]='n';
  253. minute=atoi(temBuff);
  254. temBuff[0]=buff[6];
  255. temBuff[1]=buff[7];
  256. temBuff[2]='n';
  257. second=atoi(temBuff);
  258. }