1 CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_AUTO);
2 int state = pBtn->GetCheck();
3 HKEY hKey;
4 //找到系统的启动项
5 LPCTSTR lpRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
6 //打开启动项Key
7 long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey);
8 if(lRet == ERROR_SUCCESS)
9 {
10 char pFileName[MAX_PATH] = {0};
11 //得到程序自身的全路径
12 DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PATH);
13
14 if (state == 1)
15 {
16 //添加一个子Key,并设置值 // 下面的"test"是应用程序名字(不加后缀.exe)
17 lRet = RegSetValueEx(hKey, "client", 0, REG_SZ, (BYTE *)pFileName, dwRet); //开机启动
18 }else if ( state == 0)
19 {
20 //添加一个子Key,并设置值 // 下面的"test"是应用程序名字(不加后缀.exe)
21 lRet = RegDeleteValue(hKey, "client"); //开机不启动
22 }
23
24 //关闭注册表
25 RegCloseKey(hKey);
26 if(lRet != ERROR_SUCCESS)
27 {
28 AfxMessageBox("系统参数错误,不能完成开机启动设置");
29 }
30 }