本文主要是介绍MFC清理系统、IE、回收站垃圾代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// m_Start.cpp : 实现文件//
#include "stdafx.h"
#include "MFCApplication1.h"
#include "m_Start.h"
#include "afxdialogex.h"
// m_Start 对话框
IMPLEMENT_DYNAMIC(m_Start, CDialog)
m_Start::m_Start(CWnd* pParent /*=NULL*/)
: CDialog(m_Start::IDD, pParent)
{
}
m_Start::~m_Start()
{
}
void m_Start::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX,IDC_LIST1,MyList);
DDX_Control(pDX,IDC_EDIT1,m_KeyName);
DDX_Control(pDX,IDC_EDIT2,m_ItemName);
DDX_Control(pDX,IDC_EDIT3,m_ItemValue);
}
BEGIN_MESSAGE_MAP(m_Start, CDialog)
ON_BN_CLICKED(IDC_BUTTON1,&m_Start::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON_WRITE,&m_Start::OnBnClickedButtonWrite)
ON_BN_CLICKED(IDC_BUTTON_RED,&m_Start::OnBnClickedButtonRed)
ON_WM_TIMER( )
END_MESSAGE_MAP( )
// m_Start 消息处理程序
BOOL m_Start::OnInitDialog( )
{
CDialog::OnInitDialog( );
// TODO: 在此添加额外的初始化
// CRect rc;
// ::GetClientRect(AfxGetApp( )->m_pMainWnd->m_hWnd,rc);
// rc.DeflateRect(0,15,40,90);
// MoveWindow(rc);
InitRunList( );
ShowRunList(HKEY_CURRENT_USER);
ShowRunList(HKEY_LOCAL_MACHINE);
MyList.SetExtendedStyle(MyList.GetExtendedStyle( ) | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
CRect rect,rc;
GetWindowRect(rect);
GetDesktopWindow( )->GetWindowRect(rc);
MoveWindow((rc.Width( ) - rect.Width( )) / 2,(rc.Height( ) - rect.Height( )) / 2,0,0);
m_width = rect.Width( );
m_height = rect.Height( );
m_dx = 2;
m_dy = 2;
SetTimer(1,10,NULL);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void m_Start::InitRunList( )
{
MyList.SetExtendedStyle(MyList.GetExtendedStyle( ) | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
CRect rc;
MyList.GetClientRect(rc);
MyList.InsertColumn(0,_T("描述"),0,rc.Width( ) / 3);
MyList.InsertColumn(1,_T("键名"),0,rc.Width( ) / 3);
MyList.InsertColumn(2,_T("键值"),0,rc.Width( ) / 3);
}
void m_Start::ShowRunList(HKEY path)
{
DWORD dwType = 0;
DWORD dwBufferSize = MAXBYTE;
DWORD dwKeySize = MAXBYTE;
WCHAR szValueName[MAXBYTE] = {0};
WCHAR szValueKey[MAXBYTE] = {0};
int i = 0;//下标索引
int j = 0;
HKEY hKey = NULL;
if(RegOpenKey((HKEY)(ULONG_PTR)((LONG)(path)),L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",&hKey) != ERROR_SUCCESS)
{
return;
}
CString strTmp;
while(TRUE)
{
int rect = RegEnumValue(hKey,i,szValueName,&dwBufferSize,NULL,&dwType,(LPBYTE)szValueKey,&dwKeySize);
if(rect == ERROR_NO_MORE_ITEMS)
{
break;
}
strTmp.Format(L"%d",j);
MyList.InsertItem(j,strTmp);
MyList.SetItemText(j,1,szValueName);
MyList.SetItemText(j,2,szValueKey);
i++;
j++;
dwBufferSize = MAXBYTE;
dwKeySize = MAXBYTE;
ZeroMemory(szValueName,MAXBYTE);
ZeroMemory(szValueKey,MAXBYTE);
}
RegCloseKey(hKey);
}
//禁止按钮
void m_Start::OnBnClickedButton1( )
{
// TODO: 在此添加控件通知处理程序代码
CString str1;
int nId;
//首先得到点击的位置
POSITION pos = MyList.GetFirstSelectedItemPosition( );
//得到行号,通过POSITION转化
nId = (int)MyList.GetNextSelectedItem(pos);
//得到列中的内容(0表示第一列,同理1,2,3...表示第二,三,四...列)
str1 = MyList.GetItemText(nId,1);
HKEY hroot; //子键句柄
DWORD dwDisposition;
RegCreateKeyEx(HKEY_LOCAL_MACHINE,L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",0,
NULL,0,KEY_ALL_ACCESS,NULL,&hroot,&dwDisposition);
RegDeleteValue(hroot,str1); //删除子键下名为AutoRun的键值
}
//写入按钮
void m_Start::OnBnClickedButtonWrite( )
{
// TODO: 在此添加控件通知处理程序代码
//定义注册表句柄
HKEY hroot;
//定义一个整型变量
DWORD action;
//定义一个字符串变量
CString keyname;
//获取键名
m_KeyName.GetWindowText(keyname);
//设置键名
//在注册表中创建键名
keyname += "\\";
//定义字符串变量
RegCreateKeyEx(HKEY_CURRENT_USER,keyname,0,NULL,0,KEY_WRITE,NULL,&hroot,&action);
CString itemname;
//获取项名
m_ItemName.GetWindowText(itemname);
//定义字符串变量
CString itemvalue;
//获取项值
m_ItemValue.GetWindowText(itemvalue);
//获取字符串长度
DWORD size = itemvalue.GetLength( );
if(ERROR_SUCCESS == RegSetValueEx(hroot,itemname,0,
REG_SZ,(unsigned char*)itemvalue.GetBuffer(0),size*2)) //设置项值
MessageBox(_T("成功!"));
//关闭键句柄
RegCloseKey(hroot);
}
void m_Start::OnBnClickedButtonRed( )
{
// TODO: 在此添加控件通知处理程序代码
//定义键句柄
HKEY hroot;
//定义字符串变量
CString keyname;
//获取键名
m_KeyName.GetWindowText(keyname);
//打开注册表键值
RegOpenKeyEx(HKEY_CURRENT_USER,keyname,0,KEY_READ,&hroot);
//定义字符串变量
CString itemname;
//获取项名称
m_ItemName.GetWindowText(itemname);
//设置项的数据类型
DWORD type = REG_SZ;
//定义一个字符串变量
/*char data[MAX_PATH];*/
char data[MAX_PATH];
//设置字符串代码
DWORD size = MAX_PATH;
//从注册表中获取项信息
RegQueryValueEx(hroot,itemname,0,&type,(unsigned char *)&data,&size);
//关闭键句柄
RegCloseKey(hroot);
//以对话框形式显示数据
char* str1 = data;
LPCTSTR str2 = (LPCTSTR)(LPTSTR)str1;
MessageBox(str2,L"提示");
}
void m_Start::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CRect rect,rc;
GetWindowRect(rect);
GetDesktopWindow( )->GetWindowRect(rc);
MoveWindow((-m_dx + rc.Width( ) - rect.Width( )) / 2,
(-m_dy + rc.Height( ) - rect.Height( )) / 2,
+m_dx + rect.Width( ),+m_dy + rect.Height( ));
if(rect.Height( ) >= m_height)
{
m_dy = 0;
}
if((rect.Width( ) >= m_width) && (rect.Height( ) >= m_height))
{
KillTimer(1);
}
CDialog::OnTimer(nIDEvent);
}
这篇关于MFC清理系统、IE、回收站垃圾代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!