📄 timeshutdownpcdlg.cpp
字号:
// TimeShutDownPCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TimeShutDownPC.h"
#include "TimeShutDownPCDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTimeShutDownPCDlg dialog
CTimeShutDownPCDlg::CTimeShutDownPCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTimeShutDownPCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTimeShutDownPCDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTimeShutDownPCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTimeShutDownPCDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTimeShutDownPCDlg, CDialog)
//{{AFX_MSG_MAP(CTimeShutDownPCDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_SYSCOMMAND()
ON_COMMAND(ID_POP_MENU_SHOW_WINDOW, OnPopMenuShowWindow)
ON_COMMAND(ID_POP_MENU_HIDE_WINDOW, OnPopMenuHideWindow)
ON_COMMAND(ID_POP_MENU_QUIT, OnPopMenuQuit)
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDC_BUTTON_CONFIRM, OnButtonConfirm)
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_TRAYICON, OnTrayIcon) //自定义消息
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTimeShutDownPCDlg message handlers
void CTimeShutDownPCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
// 屏蔽最大化,将最小化重定向至隐藏窗口
if ((nID & 0xFFF0) == SC_MINIMIZE)
{
// 最小化时隐藏窗口
ShowWindow(0);
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
BOOL CTimeShutDownPCDlg::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
// TODO: Add extra initialization here
// 将图标放入系统托盘
TrayNotifyMsg(NIM_ADD, IDR_MAINFRAME, "定时关机");
// 初始化关机时间为23点.
SYSTEMTIME systime;
((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_TIME))->GetTime(&systime);
systime.wHour = 23;
systime.wMinute = 0;
systime.wSecond = 0;
((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_TIME))->SetTime(&systime);
// 设定定时器
SetTimer(1, 1000, NULL);
// 显示当前的系统时间
ShowCurrentTime();
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 CTimeShutDownPCDlg::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 CTimeShutDownPCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CTimeShutDownPCDlg::TrayNotifyMsg(DWORD Msg, UINT Icon, CString strTip)
{
// 向系统发送系统栏图标信息, 先填充一个 NOTIFYICONDATA 结构
// 向系统发送系统栏图标信息Msg: NIM_DELETE, NIM_ADD, NIM_MODIFY.
if (Icon == NULL)
{
Icon = IDR_MAINFRAME;
}
NOTIFYICONDATA data;
data.cbSize = sizeof(NOTIFYICONDATA);
data.hIcon = (HICON) LoadImage(AfxGetApp()->m_hInstance ,
MAKEINTRESOURCE(Icon),
IMAGE_ICON,
16,
16,
0);
data.hWnd = this->GetSafeHwnd();
data.uCallbackMessage = WM_TRAYICON;
data.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
data.uID = IDR_MAINFRAME;
switch(Msg)
{
case NIM_DELETE:
{
data.hIcon = NULL;
data.szTip[0] = '\0';
break;
}
case NIM_ADD:
{
strcpy(data.szTip, strTip);
break;
}
case NIM_MODIFY:
{
strcpy(data.szTip, strTip);
break;
}
}
return Shell_NotifyIcon(Msg, &data);
}
void CTimeShutDownPCDlg::OnTrayIcon(WPARAM wParam, LPARAM lParam)
{
switch(lParam)
{
case WM_LBUTTONDBLCLK:
SetForegroundWindow();
ShowWindow(1);
break;
case WM_LBUTTONUP:
SetForegroundWindow();
ShowWindow(1);
break;
case WM_RBUTTONDOWN:
CPoint point(0, 0);
GetCursorPos(&point);
SetForegroundWindow();
CMenu menu;
// 装入菜单
menu.LoadMenu(IDR_POP_MENU);
// 显示菜单
menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|
TPM_LEFTBUTTON|
TPM_RIGHTBUTTON,
point.x,
point.y,
this);
break;
//default:
//break;
}
}
void CTimeShutDownPCDlg::OnPopMenuShowWindow()
{
// TODO: Add your command handler code here
SetForegroundWindow();
ShowWindow(1);
}
void CTimeShutDownPCDlg::OnPopMenuHideWindow()
{
// TODO: Add your command handler code here
ShowWindow(0);
}
void CTimeShutDownPCDlg::OnPopMenuQuit()
{
// TODO: Add your command handler code here
TrayNotifyMsg(NIM_DELETE);
// 关闭程序
DestroyWindow();
}
void CTimeShutDownPCDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
PostMessage( WM_NCLBUTTONDOWN,
HTCAPTION,
MAKELPARAM( point.x, point.y));
CDialog::OnLButtonDown(nFlags, point);
}
void CTimeShutDownPCDlg::OnButtonConfirm()
{
// TODO: Add your control notification handler code here
// 获得日期和时间
CString csDate = _T("");
CString csTime = _T("");
GetDlgItem(IDC_DATETIMEPICKER_DATE)->GetWindowText(csDate);
GetDlgItem(IDC_DATETIMEPICKER_TIME)->GetWindowText(csTime);
// 隐藏窗口
OnPopMenuHideWindow();
}
void CTimeShutDownPCDlg::ShowCurrentTime()
{
CString csCurrentTime = CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S");
GetDlgItem(IDC_STATIC_CURRENT_SYSTEM_TIME)->SetWindowText(csCurrentTime);
}
void CTimeShutDownPCDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if ( nIDEvent == 1)
{
// 显示当前的系统时间
ShowCurrentTime();
if (CmpDateTime())
{
m_nTimes = 60;
SetTimer(2, 1000, NULL);
}
}
if ( nIDEvent == 2)
{
CString csTip = _T("");
if ((--m_nTimes) == 0)
{
KillTimer(2);
ShowDown();
}
else
{
SetForegroundWindow();
csTip.Format("离关机还有 %d 秒", m_nTimes);
TrayNotifyMsg(NIM_MODIFY, IDR_MAINFRAME, csTip);
SetWindowText(csTip);
}
}
CDialog::OnTimer(nIDEvent);
}
void CTimeShutDownPCDlg::ShowDown()
{
// 关机函数, 适用9X和NT
if (IsWin2000_Xp())
{
WinExec("ShutDown -s -f -t 0 -c \" WINDOWS即将关机,请做好保存工作!\"", SW_SHOW);
}
else
{
ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE | EWX_POWEROFF,NULL);
}
}
BOOL CTimeShutDownPCDlg::IsWin2000_Xp()
{
OSVERSIONINFO winInfo;
winInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&winInfo);
if (winInfo.dwMajorVersion == 5) //win2000 or winXP
{
return TRUE;
}
else
{
return FALSE;
}
}
// 时间比较函数, 判断时间是否已到
BOOL CTimeShutDownPCDlg::CmpDateTime()
{
CTime CurrentTime = CTime::GetCurrentTime();
CString csTime = CurrentTime.Format("%H:%M:%S");
CString csDate = _T("");
csDate.Format("%d-%d-%d", CurrentTime.GetYear(), CurrentTime.GetMonth(),
CurrentTime.GetDay());
CString csTempDate = _T("");
CString csTempTime = _T("");
GetDlgItem(IDC_DATETIMEPICKER_DATE)->GetWindowText(csTempDate);
if (csTempDate.CompareNoCase(csDate) == 0)
{
GetDlgItem(IDC_DATETIMEPICKER_TIME)->GetWindowText(csTempTime);
if (csTempTime.CompareNoCase(csTime) == 0)
{
return TRUE;
}
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -