?? shuntdowndlg.cpp
字號(hào):
// 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();
}
// 熱鍵響映函數(shù)
LRESULT CShuntDownDlg::OnHotHey(WPARAM wParam,LPARAM lParam)
{
m_bShow ? ShowWindow(SW_HIDE) : ShowWindow(SW_SHOW);
m_bShow = !m_bShow;
return 0;
}
// 將系統(tǒng)時(shí)間顯示在窗口標(biāo)題欄上
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);
// 當(dāng)前系統(tǒng)時(shí)間和設(shè)置的關(guān)機(jī)時(shí)間相符
if(str == (m_strYear + m_strMonth + m_strDay + m_strHour + m_strSecond))
{
m_bArrive = true;
CloseWindow();
ShuntDown();
}
}
// 如果已經(jīng)到達(dá)了關(guān)機(jī)時(shí)間, 將一直調(diào)用以下兩個(gè)函數(shù)進(jìn)行關(guān)機(jī)操作
// 以防止一些特殊情況下不能關(guān)機(jī)
if(m_bArrive)
{
CloseWindow();
ShuntDown();
}
CDialog::OnTimer(nIDEvent);
}
// 設(shè)置關(guān)機(jī)時(shí)間
void CShuntDownDlg::OnBtnOk()
{
// 取按鈕上面的文字
CString strButton;
GetDlgItemText(IDC_BTN_OK, strButton);
if(strButton == "設(shè)置")
goto SETTING;
else if(strButton == "重置")
goto RESET;
RESET:
KillTimer(CLOSETIME);
SetDlgItemText(IDC_BTN_OK, "設(shè)置");
::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, "關(guān)機(jī)時(shí)間 - 等待用戶設(shè)置!");
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("請(qǐng)?jiān)O(shè)置年份");
m_Year.ShowDropDown();
return ;
}
// 月
memset(buf, 0, 50);
::GetDlgItemText(m_hWnd, IDC_COMBO_MONTH, buf, 3);
m_strMonth = buf;
if(m_strMonth.IsEmpty())
{
AfxMessageBox("請(qǐng)?jiān)O(shè)置月份");
m_Month.ShowDropDown();
return ;
}
// 日
memset(buf, 0, 50);
::GetDlgItemText(m_hWnd, IDC_COMBO_DAY, buf, 3);
m_strDay = buf;
if(m_strDay.IsEmpty())
{
AfxMessageBox("請(qǐng)?jiān)O(shè)置日期");
m_Day.ShowDropDown();
return ;
}
// 時(shí)
memset(buf, 0, 50);
::GetDlgItemText(m_hWnd, IDC_COMBO_HOUR, buf, 3);
m_strHour = buf;
if(m_strHour.IsEmpty())
{
AfxMessageBox("請(qǐng)?jiān)O(shè)置\"時(shí)\"");
m_Hour.ShowDropDown();
return ;
}
// 分
memset(buf, 0, 50);
::GetDlgItemText(m_hWnd, IDC_COMBO_SECOND, buf, 3);
m_strSecond = buf;
if(m_strSecond.IsEmpty())
{
AfxMessageBox("請(qǐng)?jiān)O(shè)置\"分\"");
m_Second.ShowDropDown();
return ;
}
m_bSetting = true;
SetTimer(CLOSETIME, 1000, NULL);
SetDlgItemText(IDC_STATIC_TIME, "關(guān)機(jī)時(shí)間 - " + m_strYear + "年" + m_strMonth + "月" + m_strDay + "日" + m_strHour + "時(shí)" + m_strSecond + "分");
SetDlgItemText(IDC_BTN_OK, "重置");
if(IDYES == ::MessageBox(m_hWnd, "自動(dòng)關(guān)機(jī)時(shí)間已經(jīng)設(shè)置成功, 您是否隱藏本程序?\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);
}
// 是否潤(rùn)年
bool CShuntDownDlg::IsRYear(int nYear)
{
return (nYear % 400 || (nYear % 4 == 0 && nYear % 100 != 0)) ? false : true;
}
// 初始化
void CShuntDownDlg::Init()
{
// 顯示時(shí)間
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);
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -