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

📄 alertdlg.cpp

📁 定时提醒(可设:提醒;提醒并关机;直接关机;定时执行一程序等。是家长们控制孩子玩电脑的好帮手。
💻 CPP
字号:
// AlertDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Alert.h"
#include "AlertDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "SetDlg.h"
#include "EndDlg.h"
#include "BarterDlg.h"
/////////////////////////////////////////////////////////////////////////////
// CAlertDlg dialog

CAlertDlg::CAlertDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAlertDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAlertDlg)
	m_Alert = _T("");
	m_Date = 0;
	m_Minute = _T("");
	m_Systime = _T("");
	m_Time = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAlertDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAlertDlg)
	DDX_Text(pDX, IDC_ALERT, m_Alert);
	DDX_DateTimeCtrl(pDX, IDC_DATE, m_Date);
	DDX_Text(pDX, IDC_MINUTE, m_Minute);
	DDX_Text(pDX, IDC_SYSTIME, m_Systime);
	DDX_DateTimeCtrl(pDX, IDC_TIME, m_Time);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAlertDlg, CDialog)
	//{{AFX_MSG_MAP(CAlertDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATE, OnDatetimechangeDate)
	ON_BN_CLICKED(IDC_HIDE, OnHide)
	ON_EN_CHANGE(IDC_MINUTE, OnChangeMinute)
	ON_BN_CLICKED(IDC_SET, OnSet)
	ON_BN_CLICKED(IDC_SHUT, OnShut)
	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_TIME, OnDatetimechangeTime)
	ON_WM_RBUTTONDBLCLK()
	ON_WM_TIMER()
	ON_EN_CHANGE(IDC_ALERT, OnChangeAlert)
	//}}AFX_MSG_MAP
	ON_MESSAGE(MYICON,OnMyicon)
	ON_BN_CLICKED(IDCANCEL,OnExit)
	ON_WM_ENDSESSION()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAlertDlg message handlers

BOOL CAlertDlg::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
	CAlertApp * pa=(CAlertApp *)AfxGetApp();
	m_Alert=pa->GetProfileString("Settings","AlertStr","");
	time(&now);
	m_Minute.Format("%ld",((m_Time.GetTime()-now)+59)/60);
	SetTimer(IDD_ALERT_DIALOG,1000,NULL);
	UpdateData(0);
	tnid.cbSize = sizeof(NOTIFYICONDATA);
	tnid.hWnd = AfxGetApp()->m_pMainWnd->m_hWnd;
	tnid.uID = IDR_MAINFRAME;
	tnid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
	tnid.uCallbackMessage = MYICON;
	tnid.hIcon = m_hIcon;
	CString iconstr;
	Shell_NotifyIcon(NIM_ADD,&tnid);
	ModifyShellIcon();
	// TODO: Add extra initialization here
	
	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 CAlertDlg::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 CAlertDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAlertDlg::OnDatetimechangeDate(NMHDR* pNMHDR, LRESULT* pResult) 
{
	UpdateData();
	if(m_Date.GetTime()<now)
		m_Date=m_Time;
	else
	{
		m_Time=m_Date;
		m_Minute.Format("%ld",((m_Time.GetTime()-now)+59)/60);
	}
	UpdateData(0);
	WriteShutTime();
	ModifyShellIcon();
	*pResult = 0;
}

void CAlertDlg::OnShow()
{
	CAlertApp * pa=(CAlertApp *)AfxGetApp();
	if(pa->GetProfileInt("Settings","FixFlag",0))
	{
		GetDlgItem(IDC_MINUTE)->EnableWindow();
		GetDlgItem(IDC_DATE)->EnableWindow();
		GetDlgItem(IDC_TIME)->EnableWindow();
	}
	else
	{
		GetDlgItem(IDC_MINUTE)->EnableWindow(0);
		GetDlgItem(IDC_DATE)->EnableWindow(0);
		GetDlgItem(IDC_TIME)->EnableWindow(0);
	}
	pa->WriteProfileInt("Settings","ShowFlag",0);
	ShowWindow(SW_SHOW);
}

void CAlertDlg::OnHide() 
{
	CAlertApp * pa=(CAlertApp *)AfxGetApp();
	if(pa->GetProfileInt("Settings","FixFlag",0))
	{
		GetDlgItem(IDC_MINUTE)->EnableWindow();
		GetDlgItem(IDC_DATE)->EnableWindow();
		GetDlgItem(IDC_TIME)->EnableWindow();
	}
	else
	{
		GetDlgItem(IDC_MINUTE)->EnableWindow(0);
		GetDlgItem(IDC_DATE)->EnableWindow(0);
		GetDlgItem(IDC_TIME)->EnableWindow(0);
	}
	pa->WriteProfileInt("Settings","ShowFlag",1);
	ShowWindow(SW_HIDE);
}

void CAlertDlg::OnChangeAlert() 
{
	UpdateData();
	ModifyShellIcon();
}

void CAlertDlg::OnChangeMinute() 
{
	UpdateData();
	if(atoi(m_Minute)<1)
		m_Minute.Format("%d",((m_Time.GetTime()-now)+59)/60);
	else
		m_Time=m_Date=now+atoi(m_Minute)*60;
	UpdateData(0);
	WriteShutTime();
	ModifyShellIcon();
}

void CAlertDlg::OnSet() 
{
	CSetDlg dlg;
	dlg.DoModal();
	ModifyShellIcon();
}

void CAlertDlg::OnShut() 
{
	// TODO: Add your control notification handler code here
	AtTime();
}

void CAlertDlg::OnDatetimechangeTime(NMHDR* pNMHDR, LRESULT* pResult) 
{
	UpdateData();
	if(m_Time.GetTime()<now)
		m_Time=m_Date;
	else
	{
		m_Date=m_Time;
		m_Minute.Format("%ld",((m_Time.GetTime()-now)+59)/60);
	}
	UpdateData(0);
	WriteShutTime();
	ModifyShellIcon();
	*pResult = 0;
}

void CAlertDlg::OnRButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(GetDlgItem(IDC_DATE)->IsWindowVisible())
	{
		GetDlgItem(IDC_DATE)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_TIME)->ShowWindow(SW_SHOW);
	}
	else
	{
		GetDlgItem(IDC_TIME)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_DATE)->ShowWindow(SW_SHOW);
	}
	CDialog::OnRButtonDblClk(nFlags, point);
}

void CAlertDlg::OnTimer(UINT nIDEvent) 
{
	time(&now);
	m_Systime=CTime(now).Format("%c");
	if((m_Time.GetTime()-now)%60==0)
	{
		m_Minute.Format("%d",(m_Time.GetTime()-now-1)/60+1);
		if(m_Time.GetTime()<=now)
			AtTime();
		else
		{//显示任务栏
			ModifyShellIcon();
		}
	}
	UpdateData(0);
	CDialog::OnTimer(nIDEvent);
}

void CAlertDlg::AtTime()//到设定时间后执行的函数
{
	CAlertApp * pa = (CAlertApp *)AfxGetApp();
	CEndDlg end;CString command;
	switch(pa->GetProfileInt("Settings","AlertMode",0))
	{
	case 0://仅提醒
		KillTimer(IDD_ALERT_DIALOG);
		end.m_bShut=FALSE;
		end.DoModal();
		m_Time=m_Date=now+pa->GetProfileInt("Settings","Few",1)*60-1;
		m_Minute.Format("%d",((m_Time.GetTime()-now)+59)/60);
		UpdateData(0);
		SetTimer(IDD_ALERT_DIALOG,1000,NULL);
		break;
	case 1://提醒后关机
		end.m_bShut=TRUE;
		if(end.DoModal()==IDOK)
		{
			if(pa->GetProfileInt("Settings","AutoRun",0))
				pa->SetAutoRun(1);
			ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF, 0);
		}
		else
		{
			m_Time=m_Date=now+pa->GetProfileInt("Settings","Few",1)*60-1;
			m_Minute.Format("%d",((m_Time.GetTime()-now)+59)/60);
			UpdateData(0);
		}
		break;
	case 2://直接关机
			if(pa->GetProfileInt("Settings","AutoRun",0))
				pa->SetAutoRun(1);
			ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF, 0);
		break;
	case 3://执行程序
		command=pa->GetProfileString("Settings","ExePath","");
		WinExec(command,1);
		m_Time=m_Date=now+pa->GetProfileInt("Settings","Few",1)*60-1;
		m_Minute.Format("%d",((m_Time.GetTime()-now)+59)/60);
		UpdateData(0);
		break;
	default://意外
		OnCancel();
	}
}
void CAlertDlg::OnMyicon(WPARAM wParam, LPARAM lParam)
{
	CAlertApp *pa=(CAlertApp*)AfxGetApp();
	UINT uMouseMsg;
	uMouseMsg = (UINT) lParam;
	if(uMouseMsg == WM_LBUTTONDOWN)
		{
			if(IsWindowVisible())
			{
				OnHide();
			}
			else
			{
				OnShow();
			}
		}
}

void CAlertDlg::ModifyShellIcon()
{
	CAlertApp *pa=(CAlertApp*)AfxGetApp();
	CString StrInfo;
	int few=((m_Time.GetTime()-now)+59)/60;
	switch(pa->GetProfileInt("Settings","AlertMode",0))
	{
	case 0:
		StrInfo.Format("%d分钟后(%d:%d:%d)仅提醒\"%s\"",few,m_Time.GetHour(),m_Time.GetMinute(),m_Time.GetSecond(),m_Alert);
		break;
	case 1:
		StrInfo.Format("%d分钟后(%d:%d:%d)提醒并关机\"%s\"",few,m_Time.GetHour(),m_Time.GetMinute(),m_Time.GetSecond(),m_Alert);
		break;
	case 2:
		StrInfo.Format("%d分钟后(%d:%d:%d)立即关机",few,m_Time.GetHour(),m_Time.GetMinute(),m_Time.GetSecond());
		break;
	case 3:
		StrInfo.Format("%d分钟后(%d:%d:%d)执行程序\"%s\"",few,m_Time.GetHour(),m_Time.GetMinute(),m_Time.GetSecond(),pa->GetProfileString("Settings","ExePath",""));
		break;
	default:
		;
	}
	lstrcpyn(tnid.szTip, StrInfo, sizeof(tnid.szTip));
	Shell_NotifyIcon(NIM_MODIFY,&tnid);
}

LRESULT CAlertDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	CAlertApp *pa=(CAlertApp*)AfxGetApp();
	if(message==133 ) 
	{
		if(pa->GetProfileInt("Settings","ShowFlag",0))
		{
			ShowWindow(SW_HIDE); 
		}
		else
		{
			ShowWindow(SW_SHOW); 
		}
	}	
	return CDialog::DefWindowProc(message, wParam, lParam);
}

void CAlertDlg::OnExit()
{
	CAlertApp *pa=(CAlertApp*)AfxGetApp();
	CBarterDlg dlg;
	if(GetDlgItem(IDC_MINUTE)->IsWindowEnabled()||dlg.DoModal()==IDOK)
	{
		pa->DelRegTree(HKEY_CURRENT_USER,"Software\\Microsoft\\Alert\\ShutTime");
		OnOK();
		Shell_NotifyIcon(NIM_DELETE,&tnid);
	}
}

void CAlertDlg::WriteShutTime()
{
	CAlertApp *pa=(CAlertApp*)AfxGetApp();
	pa->WriteProfileInt("ShutTime","Year",m_Time.GetYear());
	pa->WriteProfileInt("ShutTime","Month",m_Time.GetMonth());
	pa->WriteProfileInt("ShutTime","Day",m_Time.GetDay());
	pa->WriteProfileInt("ShutTime","Hour",m_Time.GetHour());
	pa->WriteProfileInt("ShutTime","Minute",m_Time.GetMinute());
	pa->WriteProfileInt("ShutTime","Second",m_Time.GetSecond());
}

void CAlertDlg::OnEndSession(BOOL flag)
{
	CAlertApp *pa=(CAlertApp*)AfxGetApp();
	if(pa->GetProfileInt("Settings","AutoRun",0))
		pa->SetAutoRun(1);
	else
		pa->SetAutoRun(0);
	CDialog::OnEndSession(flag);
}

⌨️ 快捷键说明

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