?? timerdlg.cpp
字號:
// TimerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Timer.h"
#include "TimerDlg.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
const UINT WM_TASKBARCREATED =
::RegisterWindowMessage(_T("TaskbarCreated"));
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTimerDlg dialog
CTimerDlg::CTimerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTimerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTimerDlg)
m_curTime = _T("");
m_iHour = 0;
m_iMinute = 0;
m_bCalc = FALSE;
m_sInfo = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
iAlertTime=0;
}
void CTimerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTimerDlg)
DDX_Text(pDX, IDC_CURTIME, m_curTime);
DDX_Text(pDX, IDC_EDIT_HOUR, m_iHour);
DDV_MinMaxUInt(pDX, m_iHour, 0, 23);
DDX_Text(pDX, IDC_EDIT_MINUTE, m_iMinute);
DDV_MinMaxUInt(pDX, m_iMinute, 0, 59);
DDX_Text(pDX, IDC_EDIT_INFO, m_sInfo);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTimerDlg, CDialog)
//{{AFX_MSG_MAP(CTimerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_APPLY, OnApply)
ON_COMMAND(ID_ABOUT, OnAbout)
ON_COMMAND(ID_CLOSE, OnClose)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_NOTIFYICON, OnNotifyIcon)
ON_WM_QUERYENDSESSION( )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTimerDlg message handlers
BOOL CTimerDlg::OnInitDialog()
{
SYSTEMTIME st;
CString strTemp;
// 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
CDialog::OnInitDialog();
//m_Interval = 30;
CDialog::OnInitDialog();
//SetTimer(1, 60000, NULL);
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
m_nd.cbSize = sizeof (NOTIFYICONDATA);
m_nd.hWnd = m_hWnd;
m_nd.uID = IDI_TIMER;
m_nd.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
m_nd.uCallbackMessage = WM_NOTIFYICON;
m_nd.hIcon = m_hIcon;
strcpy(m_nd.szTip, "我的鬧鐘");
GetLocalTime(&st);
// 刷新時間
strTemp.Format("現在時間 %02i:%02i ",st.wHour,st.wMinute);
GetDlgItem(IDC_CURTIME)->SetWindowText(strTemp);
Shell_NotifyIcon(NIM_ADD, &m_nd);
// TODO: Add extra initialization here
// 設置定時器,每分鐘一次
SetTimer(1, 60000, NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTimerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
// 屏蔽最大化(MFC Bug),將最小化重定向至隱藏窗口
if (nID == SC_MAXIMIZE)
return;
if (nID == SC_MINIMIZE)
ShowWindow(SW_HIDE);
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CTimerDlg::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 CTimerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
afx_msg void CTimerDlg::OnNotifyIcon(WPARAM wParam, LPARAM lParam)
{
// 響應在托盤圖標上的單擊
if ((wParam == IDI_TIMER)&&(lParam == WM_LBUTTONDOWN))
ShowWindow(SW_SHOWNORMAL);
if((wParam == IDI_TIMER) &&(lParam == WM_RBUTTONDOWN))
{
CMenu menu;
if (!menu.LoadMenu(IDR_MENU_TEST))
return ;
CMenu* pSubMenu = menu.GetSubMenu(0);
if (!pSubMenu)
return ;
// Make first menu item the default (bold font)
::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);
// Display the menu at the current mouse location. There's a "bug"
// (Microsoft calls it a feature) in Windows 95 that requires calling
// SetForegroundWindow. To find out more, search for Q135788 in MSDN.
//
CPoint mouse;
GetCursorPos(&mouse);
::SetForegroundWindow(m_nd.hWnd);
::TrackPopupMenu(pSubMenu->m_hMenu, TPM_RIGHTALIGN, mouse.x, mouse.y, 0,
m_nd.hWnd, NULL);
}
}
void CTimerDlg::OnCancel()
{
// TODO: Add extra cleanup here
KillTimer(1);
Shell_NotifyIcon(NIM_DELETE, &m_nd);
CDialog::OnCancel();
}
void CTimerDlg::Alert()
{
// TODO: Add your control notification handler code here
KillTimer(1);
MessageBox(m_sInfo);
}
BOOL CTimerDlg::OnQueryEndSession()
{
// 在用戶退出Windows時自動退出應用
CTimerDlg::OnCancel();
return TRUE;
}
void CTimerDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
static int Count = 0;
COleDateTime dtTime;
SYSTEMTIME st;
GetLocalTime(&st);
// 刷新時間
if (m_bCalc)
{
m_curTime.Format("現在時間 %02i:%02i\t提醒時間 %02d : %02d", st.wHour,st.wMinute,m_iHour,m_iMinute);
if(st.wHour == m_iHour && st.wMinute >=m_iMinute && iAlertTime <5)
{
//KillTimer(1);
Alert();
iAlertTime=0;
}
} else {
m_curTime.Format("現在時間 %02i:%02i ",st.wHour,st.wMinute);
}
GetDlgItem(IDC_CURTIME)->SetWindowText(m_curTime);
m_curTime.ReleaseBuffer();
CDialog::OnTimer(nIDEvent);
}
void CTimerDlg::OnApply()
{
iAlertTime = 0;
SYSTEMTIME st;
GetLocalTime(&st);
UpdateData();
if(m_sInfo.GetLength() <1) {
MessageBox("請輸入提醒內容");
} else {
UpdateData();
//buffer=m_curTime.GetBuffer(m_curTime.GetLength())
m_bCalc = TRUE;
m_curTime.Format("現在時間 %02i:%02i\t提醒時間 %02d : %02d", st.wHour,st.wMinute,m_iHour,m_iMinute);
GetDlgItem(IDC_CURTIME)->SetWindowText(m_curTime);
}
m_curTime.ReleaseBuffer();
// TODO: Add your control notification handler code here
}
void CTimerDlg::OnAbout()
{
// TODO: Add your command handler code here
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void CTimerDlg::OnClose()
{
// TODO: Add your command handler code here
OnCancel();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -