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

📄 settimedlg.cpp

📁 本程序提供了一种编写定时提醒功能的例子。
💻 CPP
字号:
// SetTimeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AlarmInfo.h"
#include "SetTimeDlg.h"

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

static int InitTime=45;

//设置开机程序自启动键值位置和其下新建子键值(可设为程序名)
const TCHAR gcszAutoRunKey[] = _T("Software\\Microsoft\\windows\\CurrentVersion\\Run");
const TCHAR gcszWindowClass[] = _T("MyWakeInfo");

/////////////////////////////////////////////////////////////////////////////
// CSetTimeDlg dialog


CSetTimeDlg::CSetTimeDlg(CWnd* pParent /*=NULL*/) : CDialog(CSetTimeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetTimeDlg)
	m_Time = InitTime;
	m_Pause = 1;
	m_Msg = _T("到休息时间了!");
	m_AutoRun = TRUE;
	//}}AFX_DATA_INIT

}

void CSetTimeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetTimeDlg)
	DDX_Text(pDX, IDC_TIMEEDIT, m_Time);
	DDX_Radio(pDX, IDC_PAUSERADIO, m_Pause);
	DDX_Text(pDX, IDC_TITLEEDIT, m_Msg);
	DDX_Check(pDX, IDC_AUTORUNCHECK, m_AutoRun);
	//}}AFX_DATA_MAP

}


BEGIN_MESSAGE_MAP(CSetTimeDlg, CDialog)
	//{{AFX_MSG_MAP(CSetTimeDlg)
	ON_BN_CLICKED(IDC_PAUSERADIO, OnPauseRadio)
	ON_BN_CLICKED(IDC_CONTINUERADIO, OnContinueRadio)
	ON_BN_CLICKED(IDC_AUTORUNCHECK, OnAutoRunCheck)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetTimeDlg message handlers
//开机时程序自动运行
bool CSetTimeDlg::AutoRun(BOOL bIsAdd)
{
	HKEY hKey;
	LONG lRet, lRet2;
	DWORD dwLength, dwDataType;
	TCHAR szItemValue[MAX_PATH], szPrevValue[MAX_PATH];
	TCHAR szBuffer[MAX_PATH];

	// 得到程序全路径名
	GetModuleFileName(NULL, szItemValue, MAX_PATH);


	// 打开注册表键
	lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, gcszAutoRunKey, 0,
			KEY_READ | KEY_WRITE, &hKey);
	if (lRet != ERROR_SUCCESS)
	{
		return FALSE;
	}

	// 查询自动运行项目是否存在
	dwLength = sizeof(szBuffer);
	lRet = RegQueryValueEx(hKey, gcszWindowClass, NULL, &dwDataType,
			(LPBYTE) szBuffer, &dwLength);

	// 添加
	if (bIsAdd == TRUE)
	{
		// 自动运行项目不存在
		if (lRet != ERROR_SUCCESS)
		{
			lRet2 = RegSetValueEx(hKey, gcszWindowClass, 0, REG_SZ,
						(LPBYTE) szItemValue, strlen(szItemValue));
		}
		else
		{
			// 存在, 比较二者是否相同
			dwLength = sizeof(szPrevValue);
			lRet2 = RegQueryValueEx(hKey, gcszWindowClass, 0, &dwDataType,
						(LPBYTE) szPrevValue, &dwLength);
			// 不相同则替换
			if (lstrcmp(szItemValue, szPrevValue))
			{
				lRet2 = RegDeleteValue(hKey, gcszWindowClass);
				lRet2 = RegSetValueEx(hKey, gcszWindowClass, 0, REG_SZ,
							(LPBYTE) szItemValue, strlen(szItemValue));
			}
		}
	}
	// 删除
	else 
	// 自动运行项目已存在则删除
	if (lRet == ERROR_SUCCESS)
	{
		lRet2 = RegDeleteValue(hKey, gcszWindowClass);
	}

	// 关闭注册表键
	RegCloseKey(hKey);

	if (lRet2 != ERROR_SUCCESS)
	{
		return FALSE;
	}


	return TRUE;
}

void CSetTimeDlg::OnOK() 
{
	// TODO: Add extra validation here
	//判断是否选中开机自启动项,并做相应处理
	int Status;
	CButton* check = (CButton*) GetDlgItem(IDC_AUTORUNCHECK);
	Status = check->GetCheck();

	if (Status == 1 || m_AutoRun==1) //添加子键
	{
		AutoRun(true);
	}
	else if(Status == 0 || m_AutoRun==0)  //删除子键
	{
		AutoRun(false);
	}	

	CDialog::OnOK();
}

void CSetTimeDlg::OnPauseRadio() 
{
	// TODO: Add your control notification handler code here
	m_Pause=0;
}

void CSetTimeDlg::OnContinueRadio() 
{
	// TODO: Add your control notification handler code here
	m_Pause=1;
}

void CSetTimeDlg::OnAutoRunCheck() 
{
	// TODO: Add your control notification handler code here
	m_AutoRun=!m_AutoRun;
}

⌨️ 快捷键说明

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