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

📄 shuntdowndlg.cpp

📁 一个用c++写的自动关机(VC源码) 很有意思的
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ShuntDownDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ShuntDown.h"
#include "ShuntDownDlg.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 };
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CShuntDownDlg dialog

CShuntDownDlg::CShuntDownDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CShuntDownDlg::IDD, pParent), 
	m_bShow(true), m_bSetting(false), m_bArrive(false)
{
	//{{AFX_DATA_INIT(CShuntDownDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CShuntDownDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CShuntDownDlg)
	DDX_Control(pDX, IDC_COMBO_SECOND, m_Second);
	DDX_Control(pDX, IDC_COMBO_HOUR, m_Hour);
	DDX_Control(pDX, IDC_COMBO_DAY, m_Day);
	DDX_Control(pDX, IDC_COMBO_MONTH, m_Month);
	DDX_Control(pDX, IDC_COMBO_YEAR, m_Year);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CShuntDownDlg, CDialog)
	//{{AFX_MSG_MAP(CShuntDownDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_OK, OnBtnOk)
	ON_BN_CLICKED(IDC_BTN_ABOUT, OnBtnAbout)
	ON_MESSAGE(WM_HOTKEY, OnHotHey)
	ON_CBN_SELCHANGE(IDC_COMBO_YEAR, OnSelchangeComboYear)
	ON_CBN_SELCHANGE(IDC_COMBO_MONTH, OnSelchangeComboMonth)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShuntDownDlg message handlers

BOOL CShuntDownDlg::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
	
	Init();

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

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

void CShuntDownDlg::OnBtnAbout() 
{
	CAboutDlg dlg;
	dlg.DoModal();
}

// 热键响映函数
LRESULT CShuntDownDlg::OnHotHey(WPARAM wParam,LPARAM lParam)
{
	m_bShow ? ShowWindow(SW_HIDE) : ShowWindow(SW_SHOW);
	m_bShow = !m_bShow;
	return 0;
}

// 将系统时间显示在窗口标题栏上
void CShuntDownDlg::OnTimer(UINT nIDEvent) 
{
	TIME time;
	GetTime(&time);
	CString str;
	str.Format("%.4d-%.2d-%.2d %.2d:%.2d:%.2d", time.nYear, time.nMonth, time.nDay, time.nHour, time.nMinute, time.nSecond);
	SetWindowText(str);

	if(nIDEvent == CLOSETIME)
	{
		str.Format("%.4d%.2d%.2d%.2d%.2d", time.nYear, time.nMonth, time.nDay, time.nHour, time.nMinute);

		// 当前系统时间和设置的关机时间相符
		if(str == (m_strYear + m_strMonth + m_strDay + m_strHour + m_strSecond)) 
		{
			m_bArrive = true;
			CloseWindow();
			ShuntDown();
		}
	}

	// 如果已经到达了关机时间, 将一直调用以下两个函数进行关机操作
	// 以防止一些特殊情况下不能关机
	if(m_bArrive)
	{
		CloseWindow();
		ShuntDown();
	}

	CDialog::OnTimer(nIDEvent);
}

// 设置关机时间
void CShuntDownDlg::OnBtnOk() 
{
	// 取按钮上面的文字
	CString strButton;
	GetDlgItemText(IDC_BTN_OK, strButton);
	if(strButton == "设置")
		goto SETTING;
	else if(strButton == "重置")
		goto RESET;

RESET:

	KillTimer(CLOSETIME);
	SetDlgItemText(IDC_BTN_OK, "设置");
	::EnableWindow(GetDlgItem(IDC_COMBO_YEAR)->m_hWnd, true);
	::EnableWindow(GetDlgItem(IDC_COMBO_MONTH)->m_hWnd, true);
	::EnableWindow(GetDlgItem(IDC_COMBO_DAY)->m_hWnd, true);
	::EnableWindow(GetDlgItem(IDC_COMBO_HOUR)->m_hWnd, true);
	::EnableWindow(GetDlgItem(IDC_COMBO_SECOND)->m_hWnd, true);
	m_bSetting = false;
	SetDlgItemText(IDC_STATIC_TIME, "关机时间 - 等待用户设置!");
	m_Hour.SetCurSel(-1);
	m_Second.SetCurSel(-1);
	return ;

SETTING:

	char buf[50] = {0};
	
	// 年
	::GetDlgItemText(m_hWnd, IDC_COMBO_YEAR, buf, 5);
	m_strYear = buf;
	if(m_strYear.IsEmpty())
	{
		AfxMessageBox("请设置年份");
		m_Year.ShowDropDown();
		return ;
	}

	// 月
	memset(buf, 0, 50);
	::GetDlgItemText(m_hWnd, IDC_COMBO_MONTH, buf, 3);
	m_strMonth = buf;
	if(m_strMonth.IsEmpty())
	{
		AfxMessageBox("请设置月份");
		m_Month.ShowDropDown();
		return ;
	}

	// 日
	memset(buf, 0, 50);
	::GetDlgItemText(m_hWnd, IDC_COMBO_DAY, buf, 3);
	m_strDay = buf;
	if(m_strDay.IsEmpty())
	{
		AfxMessageBox("请设置日期");
		m_Day.ShowDropDown();
		return ;
	}

	// 时
	memset(buf, 0, 50);
	::GetDlgItemText(m_hWnd, IDC_COMBO_HOUR, buf, 3);
	m_strHour = buf;
	if(m_strHour.IsEmpty())
	{
		AfxMessageBox("请设置\"时\"");
		m_Hour.ShowDropDown();
		return ;
	}

	// 分
	memset(buf, 0, 50);
	::GetDlgItemText(m_hWnd, IDC_COMBO_SECOND, buf, 3);
	m_strSecond = buf;
	if(m_strSecond.IsEmpty())
	{
		AfxMessageBox("请设置\"分\"");
		m_Second.ShowDropDown();
		return ;
	}



	m_bSetting = true;
	SetTimer(CLOSETIME, 1000, NULL);	
	SetDlgItemText(IDC_STATIC_TIME, "关机时间 - " + m_strYear + "年" + m_strMonth + "月" + m_strDay + "日" + m_strHour + "时" + m_strSecond + "分");
	SetDlgItemText(IDC_BTN_OK, "重置");

	if(IDYES == ::MessageBox(m_hWnd, "自动关机时间已经设置成功, 您是否隐藏本程序?\n显示程序组合键(Ctrl + Shift + H)", "提示", MB_YESNO | MB_DEFBUTTON1))
	{
		ShowWindow(SW_HIDE);
		m_bShow = true;
	}
	::EnableWindow(GetDlgItem(IDC_COMBO_YEAR)->m_hWnd, false);
	::EnableWindow(GetDlgItem(IDC_COMBO_MONTH)->m_hWnd, false);
	::EnableWindow(GetDlgItem(IDC_COMBO_DAY)->m_hWnd, false);
	::EnableWindow(GetDlgItem(IDC_COMBO_HOUR)->m_hWnd, false);
	::EnableWindow(GetDlgItem(IDC_COMBO_SECOND)->m_hWnd, false);
	
}

// 是否润年
bool CShuntDownDlg::IsRYear(int nYear)
{
	return (nYear % 400 || (nYear % 4 == 0 && nYear % 100 != 0)) ? false : true;
}

// 初始化
void CShuntDownDlg::Init()
{
	// 显示时间
	TIME time;
	GetTime(&time);
	CString str;
	str.Format("%.4d-%.2d-%.2d %.2d:%.2d:%.2d", time.nYear, time.nMonth, time.nDay, time.nHour, time.nMinute, time.nSecond);
	SetWindowText(str);

⌨️ 快捷键说明

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