📄 poweroffdlg.cpp
字号:
// PowerOffDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PowerOff.h"
#include "PowerOffDlg.h"
#include "Shellapi.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_NC WM_USER+1
#define CountDown 2880000 //48分钟
//#define CountDown 240000 //4分钟
/////////////////////////////////////////////////////////////////////////////
// CPowerOffDlg dialog
CPowerOffDlg::CPowerOffDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPowerOffDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPowerOffDlg)
m_Password = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPowerOffDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPowerOffDlg)
DDX_Text(pDX, IDC_EDIT1, m_Password);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPowerOffDlg, CDialog)
//{{AFX_MSG_MAP(CPowerOffDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
ON_WM_SYSCOMMAND()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON1, OnOk)
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_NC,OnNc)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPowerOffDlg message handlers
BOOL CPowerOffDlg::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
SetTimer(1,CountDown,0);
SetTimer(2,CountDown-180000,0); //提前3分钟提示
// RegisterServiceProcess(); //注册为系统进程,无法杀死 目前未实现
// TODO: Add extra initialization here
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 CPowerOffDlg::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 CPowerOffDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPowerOffDlg::PowerOff()
{
HANDLE hToken;
TOKEN_PRIVILEGES tp;
LUID luid;
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken);
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
tp.PrivilegeCount =1;
tp.Privileges [0].Luid =luid;
tp.Privileges [0].Attributes =SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),NULL, NULL);
ExitWindowsEx(EWX_POWEROFF,0);
}
int CPowerOffDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
PostMessage(WM_SHOWWINDOW,FALSE,SW_PARENTOPENING);
OnMini();
// TODO: Add your specialized creation code here
return 0;
}
void CPowerOffDlg::OnMini()
{
NOTIFYICONDATA nc;
nc.cbSize=sizeof(NOTIFYICONDATA);
nc.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
nc.hWnd=m_hWnd;
lstrcpy(nc.szTip,"PowerOff");
nc.uCallbackMessage=WM_NC;
nc.uFlags=NIF_ICON | NIF_MESSAGE | NIF_TIP;
nc.uID=IDR_MAINFRAME;
Shell_NotifyIcon(NIM_ADD,&nc);
// ShowWindow(false);
SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | WS_EX_TOPMOST);
}
void CPowerOffDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
// TODO: Add your message handler code here and/or call default
if(nID == SC_CLOSE) //鼠标点击关闭后隐藏
{
OnMini();
CDialog::OnSysCommand(nID, lParam);
}
}
LRESULT CPowerOffDlg::OnNc(WPARAM wParam,LPARAM IParam)
{
if(IParam==WM_LBUTTONDOWN)
{
ShowWindow(SW_SHOWNORMAL);
NOTIFYICONDATA nc;
nc.cbSize=sizeof(NOTIFYICONDATA);
nc.hWnd=m_hWnd;
nc.uCallbackMessage=WM_NC;
nc.uFlags=NIF_ICON | NIF_MESSAGE | NIF_TIP;
nc.uID=IDR_MAINFRAME;
Shell_NotifyIcon(NIM_DELETE,&nc);
::SetWindowPos(GetSafeHwnd(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
return 0;
}
void CPowerOffDlg::OnClose() //禁止程序自动关闭,关闭则隐藏
{
// TODO: Add your message handler code here and/or call default
PostMessage(WM_SHOWWINDOW,FALSE,SW_PARENTOPENING);
return;
// CDialog::OnClose();
}
void CPowerOffDlg::OnOk()
{
// TODO: Add your control notification handler code here
CString password;
GetDlgItem(IDC_EDIT1)->GetWindowText(password);
if(password=="123456") //密码设置
{
KillTimer(1);
KillTimer(2);
PostMessage(WM_QUIT);
}
else
{
GetDlgItem(IDC_EDIT1)->SetWindowText("");
}
UpdateData(FALSE);
}
void CPowerOffDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch(nIDEvent)
{
case 1:
PowerOff();
break;
case 2:
// MessageBox("3分钟后关机!");
if(KillTimer(2))
MessageBox("帅哥曹瑞提醒您:3分钟后关机!");
break;
}
CDialog::OnTimer(nIDEvent);
}
//DEL void CPowerOffDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
//DEL {
//DEL CDialog::OnActivate(nState, pWndOther, bMinimized);
//DEL
//DEL // TODO: Add your message handler code here
//DEL
//DEL }
void CPowerOffDlg::RegisterServiceProcess() //未实现
{
HANDLE hToken;
BOOL fOk=FALSE;
if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken))
{
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount=1;
if(!LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tp.Privileges[0].Luid))
printf("Can't lookup privilege value.\n");
tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
if(!AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(tp),NULL,NULL))
printf("Can't adjust privilege value.\n");
fOk=(GetLastError()==ERROR_SUCCESS);
CloseHandle(hToken);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -