📄 clockdlg.cpp
字号:
// clockDlg.cpp : implementation file
//
#include<winuser.h>
#include <shellapi.h>
#include "stdafx.h"
#include "clock.h"
#include "clockDlg.h"
#include"BtnST.h"
#include"BCMenu.h"
#include<mmsystem.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();
CButtonST bt;
// 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)
virtual BOOL OnInitDialog();
//}}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)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClockDlg dialog
CClockDlg::CClockDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClockDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClockDlg)
m_currettime = _T("");
m_h = 0;
m_m = 0;
m_s = 0;
sl=-1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDI_MAINFRAME);
}
void CClockDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClockDlg)
DDX_Text(pDX, IDC_EDIT_CURRENTTIME, m_currettime);
DDX_CBIndex(pDX, IDC_COMBO, m_combo);
DDX_Text(pDX, IDC_EDIT_HOUR, m_h);
DDV_MinMaxUInt(pDX, m_h, 0, 23);
DDX_Text(pDX, IDC_EDIT_MINU, m_m);
DDV_MinMaxUInt(pDX, m_m, 0, 59);
DDX_Text(pDX, IDC_EDIT_SECON, m_s);
DDV_MinMaxUInt(pDX, m_s, 0, 59);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClockDlg, CDialog)
//{{AFX_MSG_MAP(CClockDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_CBN_SELCHANGE(IDC_COMBO, OnSelchangeCombo)
ON_COMMAND(IDM_DISPLAY, OnDisplay)
ON_MESSAGE(WM_NOTIFYICON,OnNotifyIcon)
ON_BN_CLICKED(IDC_AUTOSET, OnAutoset)
ON_BN_CLICKED(IDC_MANUALSET, OnManualset)
ON_BN_CLICKED(IDC_CANCELTIME, OnCanceltime)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClockDlg message handlers
BOOL CClockDlg::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
//CButtonST m_btn1;
m_btn1.SubclassDlgItem(IDOK,this);
m_btn1.SetIcon(IDI_OKICON);
m_btn1.SetColor(CButtonST::BTNST_COLOR_BK_OUT, RGB(140, 180, 220));
m_btn1.SetColor(CButtonST::BTNST_COLOR_BK_IN, RGB(140, 180, 220));
m_btn1.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS, RGB(140, 180, 220));
m_btn1.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
//m_btn1.SetFlat(FALSE);
//CButtonST m_btn2;
m_btn2.SubclassDlgItem(IDCANCEL,this);
m_btn2.SetIcon(IDI_CANCELICON);
m_btn2.SetColor(CButtonST::BTNST_COLOR_BK_OUT, RGB(140, 180, 220));
m_btn2.SetColor(CButtonST::BTNST_COLOR_BK_IN, RGB(140, 180, 220));
m_btn2.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS, RGB(140, 180, 220));
m_btn1.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
//m_btn2.SetFlat(FALSE);
CTime t=CTime::GetCurrentTime();
m_currettime=t.Format("%p %I:%M:%S");
UpdateData(FALSE);
SetTimer(1,900,NULL);
combo=(CComboBox*)GetDlgItem(IDC_COMBO);
combo->SetCurSel(0);
E1=(CEdit*)GetDlgItem(IDC_EDIT_HOUR);
E2=(CEdit*)GetDlgItem(IDC_EDIT_MINU);
E3=(CEdit*)GetDlgItem(IDC_EDIT_SECON);
nid.SetMsg(WM_NOTIFYICON);
HICON hicon=AfxGetApp()->LoadIcon(IDI_MAINFRAME);
nid.SetHwnd(this->m_hWnd,NIF_MESSAGE|NIF_ICON|NIF_TIP);
nid.SetIcon(IDI_MAINFRAME,hicon,"这是定时报时程序");
t=CTime::GetCurrentTime();
date=(CStatic*)GetDlgItem(IDC_STATIC_DATE);
date->SetWindowText(t.Format("%Y年%m月%d日星期%w"));
return TRUE; // return TRUE unless you set the focus to a control
}
void CClockDlg::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 CClockDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
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;
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 CClockDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CClockDlg::OnOK()
{
ShowWindow(SW_HIDE);
nid.Shell_NotifyIcon(NIM_ADD,nid.m_nid);
UpdateData(TRUE);
CTime te(t.GetYear(),t.GetMonth(),t.GetDay(),m_h,m_m,m_s);
m_t=te;
//MessageBox(m_t.Format("%H:%M:%S"));
}
void CClockDlg::OnTimer(UINT nIDEvent)
{
t=CTime::GetCurrentTime();
CTimeSpan st=t-m_t;
int chk1=st.GetTotalHours();
int chk2=st.GetTotalMinutes();
int chk3=st.GetSeconds();
m_currettime=t.Format("%p %I:%M:%S");
CEdit*e;
cs=combo->GetCurSel();
e=(CEdit*)GetDlgItem(IDC_EDIT_CURRENTTIME);
e->SetWindowText(m_currettime);
if(sl==1)
{
if(chk1==1&&chk2==0&&chk3==0)
{
cs=combo->GetCurSel();
switch(cs)
{
case 0:MbInfo();break;
case 1:SdInfo();break;
case 2:CloseInfo();break;
}
}
}
else if(sl==2)
{
if(chk1==0&&chk2==0&&chk3==0)
{
cs=combo->GetCurSel();
switch(cs)
{
case 0:MbInfo();break;
case 1:SdInfo();break;
case 2:CloseInfo();break;
}
}
}
else;
CDialog::OnTimer(nIDEvent);
}
LRESULT CClockDlg::OnNotifyIcon(WPARAM wParam,LPARAM lParam)
{
//MessageBox("到了");
return nid.OnNotifyIcon( wParam, lParam);
}
BOOL CAboutDlg::OnInitDialog()
{
bt.SubclassDlgItem(IDOK,this);
bt.SetIcon(IDI_OKICON);
bt.SetColor(CButtonST::BTNST_COLOR_BK_OUT, RGB(140, 180, 220));
bt.SetColor(CButtonST::BTNST_COLOR_BK_IN, RGB(140, 180, 220));
bt.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS, RGB(140, 180, 220));
bt.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
CDialog::OnInitDialog();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CClockDlg::OnSelchangeCombo()
{
//cs=combo->GetCurSel();
UpdateData(TRUE);
}
void CClockDlg::OnDisplay()
{
::ShowWindow(GetSafeHwnd(),SW_SHOW);
}
void CClockDlg::OnCancel()
{
nid.Shell_NotifyIcon(NIM_DELETE,nid.m_nid);
CDialog::OnCancel();
}
void CClockDlg::OnAutoset()
{
m_t=CTime::GetCurrentTime();
sl=1;
E1->SetReadOnly(TRUE);
E2->SetReadOnly(TRUE);
E3->SetReadOnly(TRUE);
m_h=m_t.GetHour();
m_s=m_t.GetSecond();
m_m=m_t.GetMinute();
UpdateData(FALSE);
}
void CClockDlg::OnManualset()
{
sl=2;
E1->SetReadOnly(FALSE);
E2->SetReadOnly(FALSE);
E3->SetReadOnly(FALSE);
m_h=0;
m_m=0;
m_s=0;
UpdateData(FALSE);
}
void CClockDlg::MbInfo()
{
MessageBox("小伙子,姑娘们时间到了");
}
void CClockDlg::SdInfo()
{
PlaySound("The Microsoft Sound",NULL,SND_FILENAME );
}
void CClockDlg::CloseInfo()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)){
// AfxMessageBox("OpenProcessToken");
}
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS) {
AfxMessageBox("AdjustTokenPrivileges");
}
if(!ExitWindowsEx(EWX_SHUTDOWN|EWX_POWEROFF,0))
MessageBox("函数没有执行");
}
void CClockDlg::OnCanceltime()
{
sl=0;
m_h=0;
m_m=0;
m_s=0;
E1->SetReadOnly(FALSE);
E2->SetReadOnly(FALSE);
E3->SetReadOnly(FALSE);
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -