📄 myshutdownprocessdlg.cpp
字号:
// MyShutDownProcessDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "MyShutDownProcess.h"
#include "MyShutDownProcessDlg.h"
#include ".\myshutdownprocessdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define ID_SHUTDOWN_TIMER 1
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CMyShutDownProcessDlg 对话框
CMyShutDownProcessDlg::CMyShutDownProcessDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyShutDownProcessDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pToolTip = NULL;
}
void CMyShutDownProcessDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDOK, MCtl_IDOK);
DDX_Control(pDX, IDC_YEAR, MCtl_YEAR);
DDX_Control(pDX, IDC_MONTH, MCtl_MONTH);
DDX_Control(pDX, IDC_DAY, MCtl_DAY);
DDX_Control(pDX, IDC_HOUR, MCtl_HOUR);
DDX_Control(pDX, IDC_MINUTE, MCtl_MINUTE);
DDX_Control(pDX, IDC_SECOND, MCtl_SECOND);
}
BEGIN_MESSAGE_MAP(CMyShutDownProcessDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_WM_TIMER()
ON_WM_CLOSE()
ON_CBN_SELCHANGE(IDC_YEAR, OnCbnSelchangeYear)
END_MESSAGE_MAP()
// CMyShutDownProcessDlg 消息处理程序
BOOL CMyShutDownProcessDlg::PreTranslateMessage(MSG *pMsg)
{
if(NULL!=m_pToolTip)
m_pToolTip->RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
BOOL CMyShutDownProcessDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将\“关于...\”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
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);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
this->SetDlgItemText(IDC_YEAR,"2006");
this->SetDlgItemText(IDC_MONTH,"06");
this->SetDlgItemText(IDC_DAY,"07");
//this->SetDlgItemText(IDC_HOUR,"");
//this->SetDlgItemText(IDC_MINUTE,"");
//this->SetDlgItemText(IDC_SECOND,"");
MCtl_IDOK.EnableWindow(TRUE);
CountTime=0;
opentimer=0;
this->SetWindowText("自动关机系统(请先选择关机时间)");
//Set up the tooltip
m_pToolTip = new CToolTipCtrl;
if(!m_pToolTip->Create(this))
{
TRACE("Unable To create ToolTip\n");
return TRUE;
}
if(m_pToolTip->AddTool(GetDlgItem(IDC_YEAR), "选择关机时间"))
{
TRACE("Unable to add Dialog to the tooltip\n");
}
if(m_pToolTip->AddTool(GetDlgItem(IDC_MONTH), "选择关机时间"))
{
TRACE("Unable to add Dialog to the tooltip\n");
}
if(m_pToolTip->AddTool(GetDlgItem(IDC_DAY), "选择关机时间"))
{
TRACE("Unable to add Dialog to the tooltip\n");
}
if(m_pToolTip->AddTool(GetDlgItem(IDC_HOUR), "选择关机时间"))
{
TRACE("Unable to add Dialog to the tooltip\n");
}
if(m_pToolTip->AddTool(GetDlgItem(IDC_MINUTE), "选择关机时间"))
{
TRACE("Unable to add Dialog to the tooltip\n");
}
if(m_pToolTip->AddTool(GetDlgItem(IDC_SECOND), "选择关机时间"))
{
TRACE("Unable to add Dialog to the tooltip\n");
}
if (m_pToolTip->AddTool(GetDlgItem(IDOK),"开启\\关闭"))
{
TRACE("Unable to add OK button to the tooltip\n");
}
m_pToolTip->Activate(TRUE);
return TRUE;
return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
}
void CMyShutDownProcessDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CMyShutDownProcessDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
//设置背景色
//CPaintDC dc1(this); //
//CRect rect1;
//GetClientRect(&rect1);
//dc1.FillSolidRect(rect1,RGB(0,255,255));
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CMyShutDownProcessDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
//void CMyShutDownProcessDlg::OnCbnSelchangeCombo1()
//{
// // TODO: Add your control notification handler code here
// this->SetWindowText("自动关机系统");
// MCtl_IDOK.EnableWindow(TRUE);
//}
void CMyShutDownProcessDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
//OnOK();
CString DateTime;
CString YearString;
CString MonString;
CString DayString;
CString HourString;
CString MinString;
CString SecString;
if(MCtl_YEAR.GetWindowTextLength()<=0&&MCtl_MONTH.GetWindowTextLength()<=0&&
MCtl_DAY.GetWindowTextLength()<=0&&MCtl_HOUR.GetWindowTextLength()<=0&&
MCtl_MINUTE.GetWindowTextLength()<=0&&MCtl_SECOND.GetWindowTextLength()<=0)
{
this->SetWindowText("自动关机系统(请先选择关机时间)");
//MCtl_IDOK.EnableWindow(FALSE);
return;
}
if(opentimer==0)
{
// CurrentIndex=MCtl_YEAR.GetCurSel();
MCtl_YEAR.EnableWindow(FALSE);
MCtl_MONTH.EnableWindow(FALSE);
MCtl_DAY.EnableWindow(FALSE);
MCtl_HOUR.EnableWindow(FALSE);
MCtl_MINUTE.EnableWindow(FALSE);
MCtl_SECOND.EnableWindow(FALSE);
MCtl_IDOK.SetWindowText("关闭");
this->SetWindowText("自动关机系统");
opentimer=1;
}
else
{
MCtl_YEAR.EnableWindow(TRUE);
MCtl_MONTH.EnableWindow(TRUE);
MCtl_DAY.EnableWindow(TRUE);
MCtl_HOUR.EnableWindow(TRUE);
MCtl_MINUTE.EnableWindow(TRUE);
MCtl_SECOND.EnableWindow(TRUE);
MCtl_IDOK.SetWindowText("开启");
this->KillTimer(ID_SHUTDOWN_TIMER);
this->SetWindowText("自动关机系统");
opentimer=0;
return;
}
//switch(CurrentIndex)
//{
//case 0:
// CountTime=3600000;//1小时
// break;
//case 1:
// CountTime=3600000*2;//2小时
// break;
//case 2:
// CountTime=3600000*3;//3小时
// break;
//case 3:
// CountTime=3600000*4;//4小时
// break;
//case 4:
// CountTime=3600000*5;//5小时
// break;
//case 5:
// CountTime=3600000*6;//6小时
// break;
//case 6:
// CountTime=3600000*7;//7小时
// break;
//
//
//}
MCtl_YEAR.GetWindowText(YearString);
MCtl_MONTH.GetWindowText(MonString);
MCtl_DAY.GetWindowText(DayString);
MCtl_HOUR.GetWindowText(HourString);
MCtl_MINUTE.GetWindowText(MinString);
MCtl_SECOND.GetWindowText(SecString);
if(YearString&&MonString&&
DayString&&HourString&&
MinString&&SecString)
{
this->Year=atoi((const char *)YearString);
this->Mon=atoi((const char *)MonString);
this->Day=atoi((const char *)DayString);
this->Hour=atoi((const char *)HourString);
this->Min=atoi((const char *)MinString);
this->Sec=atoi((const char *)SecString);
SetTimer (ID_SHUTDOWN_TIMER, 1000, NULL);
SYSTEMTIME st;
GetLocalTime(&st);// gets current time
CString DateTime;
DateTime.Format("自动关机系统(%d年%d月%d日 %d:%d)",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute);
this->SetWindowText(DateTime);
}
}
void CMyShutDownProcessDlg::OnTimer(UINT nIDEvent)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
SYSTEMTIME st;
GetLocalTime(&st);// gets current time
CString DateTime;
DateTime.Format("自动关机系统(%d年%d月%d日 %d:%d)",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute);
this->SetWindowText(DateTime);
if(this->Sec>=st.wSecond&&this->Min==st.wMinute&&
this->Hour==st.wHour&&this->Day==st.wDay&&
this->Mon==st.wMonth&&this->Year==st.wYear)
{
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return;
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return ;
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
SHTDN_REASON_MINOR_UPGRADE |
SHTDN_REASON_FLAG_PLANNED))
return ;
}
else
// TODO: Add your message handler code here and/or call default
CDialog::OnTimer(nIDEvent);
}
void CMyShutDownProcessDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
if(this->opentimer==1)
this->KillTimer(ID_SHUTDOWN_TIMER);
CDialog::OnClose();
}
CMyShutDownProcessDlg::~CMyShutDownProcessDlg()
{
delete m_pToolTip;
}
void CMyShutDownProcessDlg::OnCbnSelchangeYear()
{
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -