PPTtimerDlg.cpp
资源名称:PPTtimer.rar [点击查看]
上传用户:chpname
上传日期:2018-11-08
资源大小:69k
文件大小:7k
源码类别:
对话框与窗口
开发平台:
Visual C++
- // PPTtimerDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "PPTtimer.h"
- #include "PPTtimerDlg.h"
- #include "SetDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CPPTtimerDlg dialog
- void ExportInt(char* buff, BYTE& hour, BYTE& minute, BYTE& second);
- CPPTtimerDlg::CPPTtimerDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CPPTtimerDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CPPTtimerDlg)
- m_PrintTime = _T("请先设定时间");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_FlashFlag=PPT_NOTFLASH;
- m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON_MAIN);
- }
- void CPPTtimerDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CPPTtimerDlg)
- DDX_Text(pDX, IDC_STATIC_TIME, m_PrintTime);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CPPTtimerDlg, CDialog)
- //{{AFX_MSG_MAP(CPPTtimerDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_RBUTTONDOWN()
- ON_WM_LBUTTONDOWN()
- ON_WM_CREATE()
- ON_WM_TIMER()
- ON_WM_CTLCOLOR()
- //}}AFX_MSG_MAP
- ON_BN_CLICKED(ID_SYSMENU_SET,OnSysMenuSet)
- ON_BN_CLICKED(ID_SYSMENU_CLOSE,OnSysMenuClose)
- ON_BN_CLICKED(ID_SYSMENU_STOP,OnSysMenuStop)
- ON_BN_CLICKED(ID_SYSMENU_RESTART,OnSysMenuReStart)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPPTtimerDlg message handlers
- BOOL CPPTtimerDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- //始终在最上面显示窗口
- CRect rect;
- GetClientRect(&rect);
- ::SetWindowPos(AfxGetMainWnd()->m_hWnd,HWND_TOPMOST,rect.left,rect.top,rect.right,rect.bottom,SWP_NOMOVE);
- //设置半透明窗口
- SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(),GWL_EXSTYLE)|0x80000);
- typedef BOOL (WINAPI *FSetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
- FSetLayeredWindowAttributes SetLayeredWindowAttributes;
- HINSTANCE hInst=LoadLibrary("User32.Dll");
- SetLayeredWindowAttributes=(FSetLayeredWindowAttributes)GetProcAddress(hInst,"SetLayeredWindowAttributes");
- if(SetLayeredWindowAttributes)
- SetLayeredWindowAttributes(GetSafeHwnd(),RGB(0,0,0),128,2);
- FreeLibrary(hInst);
- //隐藏标题栏
- /*LONG lStyle=::GetWindowLong(this->m_hWnd,GWL_STYLE);
- ::SetWindowLong(this->m_hWnd,GWL_STYLE,lStyle & ~WS_CAPTION);
- ::SetWindowPos(this->m_hWnd,NULL,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED);*/
- //隐藏窗体在任务栏中的显示
- ShowWindow(SW_HIDE);
- ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW,0);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CPPTtimerDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CPPTtimerDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CPPTtimerDlg::OnRButtonDown(UINT nFlags, CPoint point)
- {
- CPoint pt;
- CMenu SysMenu;
- GetCursorPos(&pt);
- if(SysMenu.LoadMenu(IDR_SYS_MENU))
- {
- CMenu * pSubMenu=SysMenu.GetSubMenu(0);
- if(pSubMenu!=NULL)
- {
- if(m_FlashFlag==PPT_TIME_STOP)
- {
- pSubMenu->EnableMenuItem(ID_SYSMENU_STOP,MF_GRAYED);
- pSubMenu->EnableMenuItem(ID_SYSMENU_RESTART,MF_ENABLED);
- }
- else if(m_FlashFlag==PPT_FIRST_RUN)
- {
- pSubMenu->EnableMenuItem(ID_SYSMENU_STOP,MF_GRAYED);
- pSubMenu->EnableMenuItem(ID_SYSMENU_RESTART,MF_GRAYED);
- }
- else if(m_FlashFlag==PPT_TIME_RESTART || m_FlashFlag==PPT_FLASH)
- {
- pSubMenu->EnableMenuItem(ID_SYSMENU_STOP,MF_ENABLED);
- pSubMenu->EnableMenuItem(ID_SYSMENU_RESTART,MF_GRAYED);
- }
- ::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);
- pSubMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,pt.x,pt.y,this);
- }
- }
- CDialog::OnRButtonDown(nFlags, point);
- }
- void CPPTtimerDlg::OnLButtonDown(UINT nFlags, CPoint point)
- {
- PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
- CDialog::OnLButtonDown(nFlags, point);
- }
- BOOL CPPTtimerDlg::PreTranslateMessage(MSG* pMsg)
- {
- //不使用ESC和Enter键关闭窗体
- if(pMsg->message==WM_KEYDOWN && (pMsg->wParam==27 || pMsg->wParam==13))
- return NULL;
- return CDialog::PreTranslateMessage(pMsg);
- }
- int CPPTtimerDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CDialog::OnCreate(lpCreateStruct) == -1)
- return -1;
- this->SetTimer(1,1000,0);//以秒为计时单位
- this->SetTimer(2,500,0);
- return 0;
- }
- void CPPTtimerDlg::OnTimer(UINT nIDEvent)
- {
- //FlashWindow(TRUE);//闪烁标题栏
- if(nIDEvent==1 && m_FlashFlag==PPT_COUNTING)
- {
- BYTE hour, minute, second;
- ExportInt(m_CurrTime, hour, minute, second);
- if(second==0)
- {
- if(minute==0)
- {
- if(hour==0)
- {
- m_PrintTime="★=超时了=★";
- m_FlashFlag=PPT_FLASH;
- goto UpdateTime;
- }
- else
- {
- hour--;
- minute=59;
- second=59;
- }
- }
- else
- {
- minute--;
- second=59;
- }
- }
- else
- {
- second--;
- }
- m_PrintTime.Format("%2d:%2d:%2d",hour,minute,second);
- strcpy(m_CurrTime,m_PrintTime);
- UpdateTime: UpdateData(FALSE);
- }
- if(nIDEvent==2 && m_FlashFlag==PPT_FLASH)
- {
- if(!strcmp(m_PrintTime,"★=超时了=★")) m_PrintTime=" ";
- else m_PrintTime="★=超时了=★";
- UpdateData(FALSE);
- }
- CDialog::OnTimer(nIDEvent);
- }
- //设置控件颜色
- HBRUSH CPPTtimerDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
- if(nCtlColor==CTLCOLOR_STATIC)
- {
- //pDC->SetBkColor(RGB(160,10,3));
- pDC->SetTextColor(RGB(255,0,0));//red color
- }
- // TODO: Return a different brush if the default is not desired
- return hbr;
- }
- void CPPTtimerDlg::OnSysMenuSet()
- {
- CSetDlg setdlg;
- if(setdlg.DoModal()==IDOK)
- {
- m_PrintTime.Format("%2d:%2d:%2d", setdlg.m_SetHour, setdlg.m_SetMinute, setdlg.m_SetSecond);
- strcpy(m_CurrTime,m_PrintTime);
- m_FlashFlag=PPT_COUNTING;
- UpdateData();
- }
- }
- void CPPTtimerDlg::OnSysMenuStop()
- {
- m_FlashFlag=PPT_TIME_STOP;
- }
- void CPPTtimerDlg::OnSysMenuReStart()
- {
- m_FlashFlag=PPT_TIME_RESTART;
- }
- void CPPTtimerDlg::OnSysMenuClose()
- {
- KillTimer(1);
- KillTimer(2);
- CDialog::OnOK();
- }
- void ExportInt(char* buff, BYTE& hour, BYTE& minute, BYTE& second)
- {
- char temBuff[3];
- hour=atoi(temBuff);
- temBuff[0]=buff[0];
- temBuff[1]=buff[1];
- temBuff[2]='n';
- hour=atoi(temBuff);
- temBuff[0]=buff[3];
- temBuff[1]=buff[4];
- temBuff[2]='n';
- minute=atoi(temBuff);
- temBuff[0]=buff[6];
- temBuff[1]=buff[7];
- temBuff[2]='n';
- second=atoi(temBuff);
- }