C/C++得到系统启动项

来源:互联网 发布:ipad淘宝hd微淘在哪 编辑:程序博客网 时间:2024/05/23 21:38

1.在VC6.0里面新建一个MFC APPWizard(exe)工程:

这里写图片描述

选择基本对话:
这里写图片描述

放入CListCtrl控件:
这里写图片描述

在CListCtrl控件上单击右键,选建立类向导:
这里写图片描述

选择Member Variables那一栏:
这里写图片描述
单击Add variable…设置变量名为m_AutoRunList

void    initList(CListCtrl  &m_AutoRunList){    LONG lStyle;    lStyle = GetWindowLong(m_AutoRunList.m_hWnd, GWL_STYLE);// 获取当前窗口style    lStyle &= ~LVS_TYPEMASK; // 清除显示方式位    lStyle |= LVS_REPORT; // 设置style    SetWindowLong(m_AutoRunList.m_hWnd, GWL_STYLE, lStyle);// 设置style    DWORD dwStyle = m_AutoRunList.GetExtendedStyle();    dwStyle |= LVS_EX_FULLROWSELECT;// 选中某行使整行高亮(只适用与report 风格的listctrl )    dwStyle |= LVS_EX_GRIDLINES;// 网格线(只适用与report 风格的listctrl )    dwStyle |= LVS_EX_CHECKBOXES;//item 前生成checkbox 控件    m_AutoRunList.SetExtendedStyle(dwStyle); // 设置扩展风格     m_AutoRunList.InsertColumn(0, "No.",LVCFMT_LEFT, 40);// 插入列     m_AutoRunList.InsertColumn(1, "键名",LVCFMT_LEFT, 80);// 插入列     m_AutoRunList.InsertColumn(2, "键值",LVCFMT_LEFT, 500);// 插入列 }void showRunList(CListCtrl  &list){    list.DeleteAllItems();    DWORD   dwType = 0;    DWORD   dwBufferSize = MAXBYTE;    DWORD   dwKeySize = MAXBYTE;    char    szValueName[255]={0};    char    szValueKey[255]={0};    HKEY    hKey=NULL;    LONG    lRet = RegOpenKey(HKEY_CURRENT_USER,REG_RUN,&hKey);    if(lRet != ERROR_SUCCESS){        AfxMessageBox("打开注册表失败");        return;    }    int i=0;    CString strTemp;    while(true)    {        dwBufferSize = MAXBYTE;        dwKeySize = MAXBYTE;        lRet = RegEnumValue(hKey,i++,szValueName,&dwBufferSize,0,0,(unsigned char *)szValueKey,&dwKeySize);        if(lRet==ERROR_NO_MORE_ITEMS){            break;        }        strTemp.Format("%d",i);        int nRow = list.InsertItem(i,strTemp);// 插入行        list.SetItemText(nRow, 1, szValueName);// 设置其它列数据        list.SetItemText(nRow, 2, szValueKey);// 设置其它列数据        ZeroMemory(szValueName,MAXBYTE);        ZeroMemory(szValueKey,MAXBYTE);//清空缓冲区    }}BOOL CListDlg::OnInitDialog()//窗口初始化时调用的函数,在这里我们初始化了CListCtrl控件,用initList()和showRunList()向里面加入了内容.{    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);        }    }    SetIcon(m_hIcon, TRUE);         // Set big icon    SetIcon(m_hIcon, FALSE);        // Set small icon    // TODO: Add extra initialization here    //上面是编译器自动生成的初始化代码    initList(m_AutoRunList);    showRunList(m_AutoRunList);    //自己添加的函数    return TRUE;  // return TRUE  unless you set the focus to a control}

这里写图片描述
注意:这个不需要管理员权限

原创粉丝点击