📄 setdlg.cpp
字号:
// SetDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Clock.h"
#include "SetDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int falg = 1;
/////////////////////////////////////////////////////////////////////////////
// CSetDlg dialog
CSetDlg::CSetDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSetDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSetDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CSetDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSetDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSetDlg, CDialog)
//{{AFX_MSG_MAP(CSetDlg)
ON_BN_CLICKED(IDC_KEEP, OnKeep)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSetDlg message handlers
void CSetDlg::OnKeep()
{
// TODO: Add your control notification handler code here
// if(falg == 0)
// {
//添加以下相关代码:
HKEY hKey;
LPCTSTR data_Set = "SoftWare\\Microsoft\\Windows\\CurrentVersion\\Run";
long ret0 =::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set,0,KEY_WRITE,&hKey);//打开注册表
if(ret0!=ERROR_SUCCESS)
{
MessageBox("打开注册表时出现错误");
}
CString m_filename;
GetModuleFileName(NULL,(char*)(LPCTSTR)m_filename,MAX_PATH);
m_filename = (CString)m_filename;
int length = m_filename.GetLength();
LPBYTE lpb = new BYTE[length+1];
for(int i = 0;i<length;i++)
{
lpb[i]=m_filename[i];
}
lpb[length]=0;
long ret1 =::RegSetValueEx(hKey,"Clock",NULL,REG_SZ,lpb,length+1);
if(ret1!=ERROR_SUCCESS)
{
MessageBox("写注册表时出现错误");
}
if(falg == 2)
{
DeleteKey(hKey,data_Set,"Clock");
}
delete lpb;
::RegCloseKey(hKey);
// }
// else
// if(falg == 1)
// {
// //添加以下相关代码:
// HKEY hKey;
// LPCTSTR data_Set = "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
// long ret0 =::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set,0,KEY_WRITE,&hKey);//打开注册表
// if(ret0!=ERROR_SUCCESS)
// {
// MessageBox("打开注册表时出现错误");
//
// }
// CString m_filename;
//
// GetModuleFileName(NULL,(char*)(LPCTSTR)m_filename,MAX_PATH);
//
// m_filename = (CString)m_filename;
//
// int length = m_filename.GetLength();
//
// LPBYTE lpb = new BYTE[length+1];
// for(int i = 0;i<length;i++)
// {
// lpb[i]=m_filename[i];
//
// }
// lpb[length]=0;
// long ret1 =::RegSetValueEx(hKey,"Clock",NULL,REG_SZ,lpb,length+1);
// if(ret1!=ERROR_SUCCESS)
// {
// MessageBox("写注册表时出现错误");
//
// }
//
// delete lpb;
//
// ::RegCloseKey(hKey);
//
// }
CSetDlg::OnOK();
}
//删除一个键下的所有值
BOOL CSetDlg::DeleteKey(HKEY hKey,LPCTSTR pszSubKey,LPCTSTR pszValueName)
{
ASSERT( pszSubKey != 0 ) ;
ASSERT( hKey != 0 ) ;
HKEY hSubKey ;
LONG lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, pszSubKey, 0, KEY_WRITE | KEY_READ, &hSubKey );
if( lRet != ERROR_SUCCESS )
{
// ASSERT(0);
// TRACE0( "CRegistry::DeleteKey(): RegOpenKeyEx() failed\n" ) ;
return FALSE ;
}
if( pszValueName )
{
// 如果参数pszValueName不为NULL则仅删除该键
lRet = RegDeleteValue( hSubKey, pszValueName ) ;
RegCloseKey( hSubKey ) ;
RegCloseKey( hKey ) ;
if( lRet != ERROR_SUCCESS )
{
// ASSERT(0);
// TRACE0( "CRegistry::DeleteKey(): RegDeleteValue() failed\n" );
return FALSE ;
}
}
else
{
DWORD dwSubKeyCnt = 0 ;
do
{
// 得到子键的信息
DWORD dwMaxSubKey ;
LONG lRet = RegQueryInfoKey(hSubKey,0, 0, 0, &dwSubKeyCnt, &dwMaxSubKey,0,0, 0, 0,0, 0);
if( lRet != ERROR_SUCCESS )
{
// TRACE0( "CRegistry::DeleteKey(): RegQueryInfoKey() failed.\n" );
RegCloseKey( hSubKey ) ;
return FALSE ;
}
if( dwSubKeyCnt > 0 )
{
LPTSTR pszKeyName = new TCHAR [ dwMaxSubKey + 1 ] ;
DWORD dwKeyNameLen = dwMaxSubKey ;
lRet = RegEnumKey(
hSubKey,
0, // index
pszKeyName, // address of buffer for key name string
dwKeyNameLen+1 // max. length of key name string
) ;
if( lRet != ERROR_SUCCESS )
{
// TRACE0( "CRegistry::DeleteKey(): RegEnumKey() failed\n" ) ;
delete [] pszKeyName ;
RegCloseKey( hSubKey ) ;
return FALSE ;
}
if( ! DeleteKey( hSubKey,pszKeyName, pszValueName ) )
{
delete [] pszKeyName ;
RegCloseKey( hSubKey ) ;
return FALSE ;
}
delete [] pszKeyName ;
}
} while( dwSubKeyCnt > 0 ) ;
RegCloseKey( hSubKey ) ;
lRet = RegDeleteKey( hKey, pszSubKey ) ;
//RegCloseKey( hKey ) ;
if( lRet != ERROR_SUCCESS )
{
// TRACE0( "CRegistry::DeleteKey(): RegDeleteKey() failed\n" ) ;
return FALSE ;
}
}
return TRUE ;
}
void CSetDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CSetDlg::OnRadio1()
{
// TODO: Add your control notification handler code here
falg = 1;
}
void CSetDlg::OnRadio2()
{
// TODO: Add your control notification handler code here
falg = 0;
}
void CSetDlg::OnRadio3()
{
// TODO: Add your control notification handler code here
falg = 2;
}
BOOL CSetDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
((CButton *)GetDlgItem(IDC_RADIO2))->SetCheck(BST_CHECKED);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -