⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shutdowndlg.cpp

📁 一个自动关机程序
💻 CPP
字号:
// shutdownDlg.cpp : implementation file
//

#include "stdafx.h"
#include "shutdown.h"
#include "shutdownDlg.h"
#include "HyperLink.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CHyperLink m_likEMail;
	//}}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)
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//m_likEMail = _T("");
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Control(pDX,IDC_EMAIL, m_likEMail);
	//}}AFX_DATA_MAP
}
BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	// Set HyperLink for E-Mail
	m_likEMail.SetURL("mailto:aspist@hotmail.com");	//联系的email地址
	m_likEMail.SetUnderline(FALSE);	//没有下划线
	m_likEMail.SetLinkCursor(AfxGetApp()->LoadCursor(IDC_HAND));
	return true;
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShutdownDlg dialog

CShutdownDlg::CShutdownDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CShutdownDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CShutdownDlg)
	
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	CTime SetTime=CTime::GetCurrentTime();
	m_uYear = SetTime.GetYear();
	m_uMonth = SetTime.GetMonth();
	m_uDay = SetTime.GetDay();
	m_uHour = 23;
	m_uMinute =59;
	m_uSecond =59;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CShutdownDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CShutdownDlg)
	DDX_Text(pDX, IDC_EDIT1, m_uYear);
	DDX_Text(pDX, IDC_EDIT2, m_uMonth);
	DDV_MinMaxUInt(pDX, m_uMonth, 1, 12);
	DDX_Text(pDX, IDC_EDIT3, m_uDay);
	DDV_MinMaxUInt(pDX, m_uDay, 1, 31);
	DDX_Text(pDX, IDC_EDIT4, m_uHour);
	DDV_MinMaxUInt(pDX, m_uHour, 0, 23);
	DDX_Text(pDX, IDC_EDIT5, m_uMinute);
	DDV_MinMaxUInt(pDX, m_uMinute, 0, 59);
	DDX_Text(pDX, IDC_EDIT6, m_uSecond);
	DDV_MinMaxUInt(pDX, m_uSecond, 0, 59);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CShutdownDlg, CDialog)
	//{{AFX_MSG_MAP(CShutdownDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_MESSAGE(WM_MYMESSAGE,OnMYMESSAGE)
	ON_WM_DESTROY()
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_APP_RESTORE, OnAppRestore)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShutdownDlg message handlers

BOOL CShutdownDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	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);
		}
	}

	// 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
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CShutdownDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	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 CShutdownDlg::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 CShutdownDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CShutdownDlg::ShowTrayIcon(){
	NOTIFYICONDATA tnd;
	tnd.cbSize=sizeof(NOTIFYICONDATA);
	tnd.hWnd=this->m_hWnd;
	tnd.uID=IDR_MAINFRAME;
	tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
	tnd.uCallbackMessage=WM_MYMESSAGE;
	tnd.hIcon=this->m_hIcon;//AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	_tcscpy(tnd.szTip,"定时关机");
	Shell_NotifyIcon(NIM_ADD, &tnd);
}

void CShutdownDlg::OnMYMESSAGE(WPARAM wParam, LPARAM lParam) 
{ 
    UINT uID; 
    UINT uMouseMsg; 
 
    uID = (UINT) wParam; 
    uMouseMsg = (UINT) lParam; 
 
    if (uMouseMsg == WM_LBUTTONDOWN&&uID==IDR_MAINFRAME)
		ShowWindow(SW_SHOWNORMAL);
	if (uMouseMsg == WM_RBUTTONDOWN&&uID==IDR_MAINFRAME){
		CMenu Menu;
		Menu.LoadMenu(IDR_POPMENU);
		POINT point;
		::GetCursorPos(&point);
		Menu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTBUTTON,point.x,point.y,this);
	}
 } 



void CShutdownDlg::OnButton1() 
{
	UpdateData(true);
	CTime SetTime(m_uYear,m_uMonth,m_uDay,m_uHour,m_uMinute,m_uSecond);
	CTime CurrTime=CTime::GetCurrentTime();
	CTimeSpan TimeSpan=SetTime-CurrTime;
	SetTimer(100,TimeSpan.GetTotalSeconds()*1000,NULL);
	ShowTrayIcon();
	ShowWindow(SW_HIDE);
}

void CShutdownDlg::OnTimer(UINT nIDEvent) 
{
	OSVERSIONINFO oi;
	oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	GetVersionEx(&oi);
	if (oi.dwPlatformId == VER_PLATFORM_WIN32_NT)
	//if ((ver & 0x0000ffff) == ver  and  and  (ver & 0x0000ffff) == 5)
	{
		HANDLE handle;
		TOKEN_PRIVILEGES tkp;
		OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &handle);
		LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

		tkp.PrivilegeCount = 1;  // one privilege to set    
		tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
 
		AdjustTokenPrivileges(handle, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
	}
	::ExitWindowsEx(EWX_SHUTDOWN, 0);
	exit(0);
}

void CShutdownDlg::OnOK(){OnButton1();}

void CShutdownDlg::OnButton2() 
{
	this->EndDialog(0);
}


void CShutdownDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	NOTIFYICONDATA tnid;
	tnid.cbSize=sizeof(NOTIFYICONDATA);
	tnid.hWnd=this->m_hWnd;
	tnid.uID=IDR_MAINFRAME;//保证删除的是该程序的图标
	Shell_NotifyIcon(NIM_DELETE,&tnid);	
}

void CShutdownDlg::OnAppAbout() 
{
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

void CShutdownDlg::OnAppRestore() 
{
	OnMYMESSAGE(IDR_MAINFRAME, WM_LBUTTONDOWN);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -