?? autoclosecomputerdlg.cpp
字號:
// AutoCloseComputerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AutoCloseComputer.h"
#include "AutoCloseComputerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAutoCloseComputerDlg dialog
CAutoCloseComputerDlg::CAutoCloseComputerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAutoCloseComputerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAutoCloseComputerDlg)
m_systime = _T("");
m_date = _T("");
m_valstatic5timing = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}
void CAutoCloseComputerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAutoCloseComputerDlg)
DDX_Control(pDX, IDC_STATIC5_timing, m_constatic5timing);
DDX_Control(pDX, IDC_EDIT_Minute, m_coneditMinute);
DDX_Control(pDX, IDC_EDIT_Hour, m_coneditHour);
DDX_Control(pDX, IDC_EDIT_Day, m_coneditDay);
DDX_Control(pDX, IDOK, m_ok);
DDX_Control(pDX, IDC_SYSTIME2, m_constatic3);
DDX_Control(pDX, IDC_EDIT1, m_conedit2);
DDX_Text(pDX, IDC_SYSTIME, m_systime);
DDX_Text(pDX, IDC_SYSTIME3, m_date);
DDX_Text(pDX, IDC_STATIC5_timing, m_valstatic5timing);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAutoCloseComputerDlg, CDialog)
//{{AFX_MSG_MAP(CAutoCloseComputerDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAutoCloseComputerDlg message handlers
BOOL CAutoCloseComputerDlg::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
// TODO: Add extra initialization here
m_ok.ShowWindow(SW_HIDE);
m_constatic3.ShowWindow(SW_HIDE);
m_conedit2.ShowWindow(SW_SHOW);
m_constatic5timing.ShowWindow(SW_HIDE);
SetTimer(1,1,NULL);
SetTimer(2,1000,NULL);
SetTimer(3,1,NULL);
TimeSwitch=false;
AppSwitch=true;
timing=false;
TotalTime=0;
m_conedit2.SetWindowText("輸入時間");
char cBuffer[10];
SYSTEMTIME systime;
::GetLocalTime(&systime);
CString cDay;
cDay=_gcvt(systime.wDay,10,cBuffer);
m_coneditDay.SetWindowText(cDay);
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 CAutoCloseComputerDlg::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 CAutoCloseComputerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAutoCloseComputerDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(MessageBox("你確認要退出嗎?","Really",MB_OKCANCEL+MB_ICONINFORMATION)==1)
CDialog::OnCancel();
}
void CAutoCloseComputerDlg::OnButton1()
{
if(MessageBox("真的要注銷嗎?","確認",MB_OKCANCEL+MB_ICONWARNING)==1)
{
::ExitWindowsEx(EWX_LOGOFF,0);
CDialog::OnCancel();
}
}
void CAutoCloseComputerDlg::OnButton2()
{
if(MessageBox("真的要重新啟動嗎?","確認",MB_OKCANCEL+MB_ICONWARNING)==1)
{
::ExitWindowsEx(EWX_REBOOT,0);
CDialog::OnCancel();
}
}
void CAutoCloseComputerDlg::OnButton3()
{
if(MessageBox("真的要關閉計算機嗎?","確認",MB_OKCANCEL+MB_ICONWARNING)==1)
{
::ExitWindowsEx(EWX_SHUTDOWN,0);
CDialog::OnCancel();
}
}
void CAutoCloseComputerDlg::OnButton4()
{
double times;
CString bb;
GetDlgItemText(IDC_EDIT1,bb);
times=atoi(bb);
if((times<=0&&TotalTime==0))
{
MessageBox("時間不能為0或少于1分鐘","錯誤",MB_OK+MB_ICONERROR);
m_conedit2.SetWindowText("輸入時間");
TotalTime=0;
TimeSwitch=false;
m_constatic3.ShowWindow(SW_HIDE);
m_conedit2.ShowWindow(SW_SHOW);
AppSwitch=true;
}
if(AppSwitch==true&×!=0)
{
m_constatic3.SetWindowText(bb);
if(TimeSwitch==false)
{
TotalTime=1;
}
TimeSwitch=true;
m_conedit2.ShowWindow(SW_HIDE);
m_constatic3.ShowWindow(SW_SHOW);
UpdateData(TRUE);
}
}
void CAutoCloseComputerDlg::OnButton5()
{
if(m_conedit2.ShowWindow(SW_SHOW))
{
MessageBox("還沒有開始計時","錯誤",MB_OK+MB_ICONERROR);
TotalTime=0;
}
TimeSwitch=false;
AppSwitch=true;
m_conedit2.ShowWindow(SW_SHOW);
m_constatic3.ShowWindow(SW_HIDE);
m_conedit2.SetWindowText("輸入時間");
TotalTime=0;
}
void CAutoCloseComputerDlg::OnTimer(UINT nIDEvent)
{
if(nIDEvent==1)
SYSTIME();
if(nIDEvent==2)
if(TimeSwitch==true)
ClosePC();
if(nIDEvent==3)
if(timing==true)
Timing();
CDialog::OnTimer(nIDEvent);
}
void CAutoCloseComputerDlg::SYSTIME()
{
SYSTEMTIME sysTime;
::GetLocalTime(&sysTime);
CString week;
switch(sysTime.wDayOfWeek)
{
case 1:
week="一"; break;
case 2:
week="二"; break;
case 3:
week="三"; break;
case 4:
week="四"; break;
case 5:
week="五"; break;
case 6:
week="六"; break;
case 7:
week="日"; break;
}
m_systime.Format("%2d:%2d:%2d.%3d",sysTime.wHour,sysTime.wMinute,sysTime.wSecond,sysTime.wMilliseconds);
m_date.Format("%d年%d月%d日 星期%s",sysTime.wYear,sysTime.wMonth,sysTime.wDay,week);
UpdateData(FALSE);
}
void CAutoCloseComputerDlg::ClosePC()
{
AppSwitch=false;
CString static3_1,static3_2,Total;
double static3_1_1;
char buffer[10];
GetDlgItemText(IDC_SYSTIME2,static3_1);
static3_1_1=atof(static3_1);
TotalTime=TotalTime-1;
if(TotalTime==0)
{
static3_1_1=static3_1_1-1;
TotalTime=59;
}
if(static3_1_1==-1)
{
CDialog::OnCancel();
::ExitWindowsEx(EWX_SHUTDOWN,0);
}
Total=_gcvt(TotalTime,10,buffer);
static3_2=_gcvt(static3_1_1,10,buffer);
m_constatic3.SetWindowText(static3_2+"分 "+Total+"秒");
m_conedit2.SetWindowText(static3_2);
UpdateData(TRUE);
}
void CAutoCloseComputerDlg::OnButton6()
{
timing=true;
SYSTEMTIME systime;
::GetLocalTime(&systime);
CString editday,edithour,editminute;
int iDay,iHour,iMinute;
GetDlgItemText(IDC_EDIT_Day,editday);
iDay=atoi(editday);
GetDlgItemText(IDC_EDIT_Hour,edithour);
iHour=atoi(edithour);
GetDlgItemText(IDC_EDIT_Minute,editminute);
iMinute=atoi(editminute);
m_coneditDay.EnableWindow(FALSE);
m_coneditHour.EnableWindow(FALSE);
m_coneditMinute.EnableWindow(FALSE);
m_constatic5timing.ShowWindow(SW_SHOW);
if(iHour==0)
m_coneditHour.SetWindowText("0");
if(iMinute==0)
m_coneditMinute.SetWindowText("0");
switch(systime.wMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(iDay>31 || iDay<1)
{
MessageBox("本月號數從1號~31號","錯誤",MB_OK+MB_ICONWARNING);
m_coneditDay.SetWindowText("");
timing=false;
m_coneditDay.EnableWindow(TRUE);
m_constatic5timing.ShowWindow(SW_HIDE);
}
break;
case 2:
case 4:
case 6:
case 9:
case 11:
if(iDay>30)
{
MessageBox("本月號數從1號~30號","錯誤",MB_OK+MB_ICONWARNING);
m_coneditDay.SetWindowText("");
timing=false;
m_coneditDay.EnableWindow(TRUE);
m_constatic5timing.ShowWindow(SW_HIDE);
}
break;
}
if(iHour>23 || iMinute>59)
{
MessageBox("時間范圍從00:00~23:59","錯誤",MB_OK+MB_ICONWARNING);
timing=false;
m_coneditHour.EnableWindow(TRUE);
m_coneditMinute.EnableWindow(TRUE);
m_constatic5timing.ShowWindow(SW_HIDE);
}
if( (iDay==systime.wDay&&iHour<systime.wHour)|| (iDay<systime.wDay) ||
(iDay==systime.wDay&&iHour==systime.wHour&&systime.wMinute>iMinute) )
{
MessageBox("設定關機的時間已過","錯誤",MB_OK+MB_ICONWARNING);
timing=false;
m_coneditHour.EnableWindow(TRUE);
m_coneditMinute.EnableWindow(TRUE);
m_coneditDay.EnableWindow(TRUE);
m_constatic5timing.ShowWindow(SW_HIDE);
}
}
void CAutoCloseComputerDlg::Timing()
{
SYSTEMTIME systime;
::GetLocalTime(&systime);
CString editday,edithour,editminute;
int iDay,iHour,iMinute,cDay,cHour,cMinute;
GetDlgItemText(IDC_EDIT_Day,editday);
iDay=atoi(editday);
GetDlgItemText(IDC_EDIT_Hour,edithour);
iHour=atoi(edithour);
GetDlgItemText(IDC_EDIT_Minute,editminute);
iMinute=atoi(editminute);
if(iDay==systime.wDay&&iHour==systime.wHour&&iMinute==systime.wMinute)
{
CDialog::OnCancel();
::ExitWindowsEx(EWX_SHUTDOWN,0);
}
cDay=iDay-systime.wDay;
cHour=iHour-systime.wHour;
cMinute=iMinute-systime.wMinute;
if(iMinute<systime.wMinute)
{
cMinute+=60;
cHour-=1;
}
m_valstatic5timing.Format("還剩余:%2d天 %2d小時 %2d分鐘 后關閉計算機",cDay,cHour,cMinute);
UpdateData(FALSE);
}
void CAutoCloseComputerDlg::OnButton7()
{
timing=false;
m_coneditDay.EnableWindow(TRUE);
m_coneditHour.EnableWindow(TRUE);
m_coneditMinute.EnableWindow(TRUE);
m_constatic5timing.ShowWindow(SW_HIDE);
}
void CAutoCloseComputerDlg::OnOK()
{
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -