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

📄 timepoweroffdlg.cpp

📁 使用VC++语言实现定时关机的一个程序
💻 CPP
字号:
// TimePowerOffDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TimePowerOff.h"
#include "TimePowerOffDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define  MYWM_NOTIFYICON  WM_USER+1
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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()

/////////////////////////////////////////////////////////////////////////////
// CTimePowerOffDlg dialog

CTimePowerOffDlg::CTimePowerOffDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTimePowerOffDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTimePowerOffDlg)
	m_sInfo = _T("定时器未启动");

	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	timeOff=m_Time1 = m_Time2 =CTime::GetCurrentTime();
	bAppend=false;
}

void CTimePowerOffDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTimePowerOffDlg)
	DDX_Control(pDX, IDC_STATIC_INFO, m_cInfo);
	DDX_Text(pDX, IDC_STATIC_INFO, m_sInfo);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER1, m_Time1);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER2, m_Time2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTimePowerOffDlg, CDialog)
	//{{AFX_MSG_MAP(CTimePowerOffDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK2, OnOk2)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTimePowerOffDlg message handlers

BOOL CTimePowerOffDlg::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
	
	// TODO: Add extra initialization here
	
    m_tnid.cbSize=sizeof(NOTIFYICONDATA);        //定义结构的成员变量
    m_tnid.hWnd=this->m_hWnd;
    m_tnid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
    m_tnid.uCallbackMessage=MYWM_NOTIFYICON;     //用户定义的回调消息
    _tcscpy(m_tnid.szTip, _T("定时关机程序"));
	m_tnid.uID=IDR_MAINFRAME;
//	hIcon=AfxGetApp()->LoadIcon(m_hIcon);        //图标化的图标
	hIcon=m_hIcon;
	m_tnid.hIcon=hIcon;
	if(hIcon)::DestroyIcon(hIcon);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTimePowerOffDlg::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 CTimePowerOffDlg::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 CTimePowerOffDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTimePowerOffDlg::OnOK() 
{
	UpdateData(true);
	CString str1,str2,str3;
	str1=m_Time1.Format("%Y-%m-%d");
	str2=m_Time2.Format("%H:%M:%S");
	str3=str1+" "+str2;
	CString strPath=".\\powertime.ini";
	WritePrivateProfileString("time", "time",
		str3, strPath);
	   
	m_Time1=m_Time2=GetOffTime();
	UpdateData(false);
	
	//CDialog::OnOK();
}
CTime CTimePowerOffDlg::GetOffTime() 
{
  	CString strPath=".\\powertime.ini";
	char strSqlInfo[100];
	GetPrivateProfileString("time", "time", "time", strSqlInfo, 200, strPath);
	CString strTime=strSqlInfo;
	if(strTime==""||strTime=="time") return 0;
	return StrToTime(strTime);

}

CTime CTimePowerOffDlg::StrToTime(CString str)//函数功能:STRTOTIME
{// 时间串格式 "%Y-%m-%d %H:%M:%S",如"1999-01-01 11:11:11"
	int nYear,nMonth,nDay,nHour,nMinute,nSecond;
	sscanf(str.Left(4), "%d", &nYear); // 得到年
	sscanf(str.Mid(5,2), "%d", &nMonth); // 得到月
	sscanf(str.Mid(8,2), "%d", &nDay); // 得到日
	sscanf(str.Mid(11,2), "%d", &nHour); // 得到时
	sscanf(str.Mid(14,2), "%d", &nMinute); // 得到分
	sscanf(str.Mid(17,2), "%d", &nSecond); // 得到秒
	// 构造CTime变量
	CTime result(nYear,nMonth,nDay,nHour,nMinute,nSecond);
	return result;
}

void CTimePowerOffDlg::OnOk2() 
{
   
	timeOff=GetOffTime(); 
	//SetTimer(1,1000,NULL);
	bAppend=!bAppend;
	if(bAppend)
	{
	 SetDlgItemText(IDOK2,"停止计时器");
	 SetTimer(1,1000,NULL);
	}
	else
	{
	  SetDlgItemText(IDOK2,"启动计时器");
	  KillTimer(1);
	}
	OnTimer(1); 

}

void CTimePowerOffDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent)
	{
		CTime CurTime=CTime::GetCurrentTime();
		CTimeSpan tpUse=timeOff-CurTime;
		long iMins=tpUse.GetTotalSeconds();
		m_cInfo.SetWindowText("离关机时间还有:"+tpUse.Format("%H时%M分%S秒"));
		if(iMins<-30)
		{
			m_cInfo.SetWindowText("无效时间");
			return;
		}

		if(timeOff<=0) 
		{  
			m_cInfo.SetWindowText("无效时间");
			return;
		}
		if(CurTime>timeOff)
		{
			PowerOff();
		}

	}
	CDialog::OnTimer(nIDEvent);
}

void CTimePowerOffDlg::PowerOff()
{
   
    OnCancel(); 
    TOKEN_PRIVILEGES tp;
    HANDLE hToken; 
    LUID luid; 
    LPTSTR MachineName=NULL; 
    if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken ))
	{
       // PERR("OpenProcessToken",GetLastError());
        return ;
	}
    if(!LookupPrivilegeValue(MachineName, SE_SHUTDOWN_NAME, &luid))
	{
      //  PERR("LookupPrivilegeValue", GetLastError());
        return ; 
	}
    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,EWX_FORCE);	
}

void CTimePowerOffDlg::OnCancel() 
{
	// TODO: Add extra cleanup here

	CDialog::OnCancel();
}



LRESULT CTimePowerOffDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
   
    if((message==WM_SYSCOMMAND)&&(wParam==SC_MINIMIZE))         //如果是系统消息
	{
		this->ShowWindow(SW_HIDE);
		::Shell_NotifyIcon(NIM_ADD,&m_tnid);
		return true;
	}
	else if((message==MYWM_NOTIFYICON)&&(lParam==WM_LBUTTONDBLCLK))  //如果是用户定义的消息
	{
		this->ShowWindow(SW_SHOW);
		this->SetActiveWindow();   //双击并且出现在最上面
		::Shell_NotifyIcon(NIM_DELETE,&m_tnid);
		return true;
	}
	else
	{
		return CDialog::WindowProc(message, wParam, lParam);
	}
	return CDialog::WindowProc(message, wParam, lParam);
}

⌨️ 快捷键说明

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