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
}
注意:这个不需要管理员权限