?? timepoweroffdlg.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 + -