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

📄 autoclosecomputerdlg.cpp

📁 很好的开机关机程序定时关机很好用的也是学习的好例子
💻 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&&times!=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 + -