gusucode.com > 遍历搜索注册表C++源码程序 > 遍历搜索注册表/seekreg20/SeekReg20/SeekRegDlg.cpp
// SeekRegDlg.cpp : implementation file // #include "stdafx.h" #include "SeekReg.h" #include "SeekRegDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSeekRegDlg dialog CSeekRegDlg::CSeekRegDlg(CWnd* pParent /*=NULL*/) : CDialog(CSeekRegDlg::IDD, pParent) { //{{AFX_DATA_INIT(CSeekRegDlg) m_content = _T(""); m_result = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSeekRegDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSeekRegDlg) DDX_Control(pDX, ID_FINDNEXT, m_findNext); DDX_Control(pDX, IDC_MACHINE, m_machine); DDX_Control(pDX, IDC_USER, m_user); DDX_Text(pDX, IDC_CONTENT, m_content); DDX_Text(pDX, IDC_RESULT, m_result); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSeekRegDlg, CDialog) //{{AFX_MSG_MAP(CSeekRegDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(ID_FINDNEXT, OnFindNext) ON_BN_CLICKED(IDC_USER, OnUser) ON_BN_CLICKED(IDC_MACHINE, OnMachine) ON_EN_CHANGE(IDC_CONTENT, OnChangeContent) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSeekRegDlg message handlers BOOL CSeekRegDlg::OnInitDialog() { 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); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_user.SetCheck(1); select=1; m_findNext.EnableWindow(FALSE); return TRUE; // return TRUE unless you set the focus to a control } void CSeekRegDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CSeekRegDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CSeekRegDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSeekRegDlg::RegSearch(HKEY hKey, TCHAR rootKey[], CString path) { DWORD numSubKey=0; //存放子键的项数 DWORD i; //计数 DWORD sizeSubKey; //子键大小 TCHAR subKey[MAX_KEY_LENGTH]; //子键名 /*打开hKey中的rootKey, hKey乃继承递归调用者之hKey, rootKey乃递归调用者之subKey*/ RegOpenKeyEx( hKey, //欲开之键柄 rootKey, //欲开之根键名 0, //保留值,强制0 KEY_READ, //权限 &hKey); //打开子键后,返回键柄 /*首先查当前键下的值们(表明是复数哈)是否有符合条件的*/ if (RegQueryValueEx( hKey, //欲开之键柄 m_content, //欲查询的值 NULL, NULL, NULL, NULL)==ERROR_SUCCESS) { findFlag=1; //在该键下,直接查找成功 m_result=path; return; //跳出递归 } /*没有查到,则进入子键查询*/ else { /*首先查得当前键下的子键项数*/ RegQueryInfoKey( hKey, NULL, NULL, NULL, &numSubKey, //查得子键的项数 NULL, NULL, NULL, NULL, NULL, NULL, NULL); /*如果子键数不为0,遍历子键*/ if (numSubKey) { for (i=0; i<numSubKey; i++) { subKey[0]='\0'; //该两行很重要,保证了每次循环的初始 sizeSubKey=MAX_KEY_LENGTH; //第一行清空子键,第二行赋缓冲区大小 RegEnumKeyEx( hKey, //键柄 i, //子键序号 subKey, //存入子键名 &sizeSubKey,//指明缓冲区大小 NULL, NULL, NULL, NULL); RegSearch (hKey,subKey,path+subKey+"\\"); //递归 } } } RegCloseKey(hKey); //收尾工作是一定要做的,否则机器会异常的慢 } void CSeekRegDlg::OnUser() //消息处理函数 { m_user.SetCheck(1); m_machine.SetCheck(0); select=1; } void CSeekRegDlg::OnMachine() //消息处理函数 { m_machine.SetCheck(1); m_user.SetCheck(0); select=2; } void CSeekRegDlg::OnFindNext() //消息处理函数 { UpdateData(TRUE); m_result.Empty(); findFlag=0; switch (select) { case 1: RegSearch (HKEY_USERS,"","");m_result="HKEY_USERS\\"+m_result;break; case 2: RegSearch (HKEY_LOCAL_MACHINE,"","");m_result="HKEY_LOCAL_MACHINE\\"+m_result;break; } if(!findFlag) m_result="This value is Not found in registry."; UpdateData(FALSE); } void CSeekRegDlg::OnChangeContent() { UpdateData(TRUE); if(m_content.IsEmpty()) m_findNext.EnableWindow(FALSE); else m_findNext.EnableWindow(TRUE); } //屏蔽Esc BOOL CSeekRegDlg::PreTranslateMessage(MSG* pMsg) { if(pMsg->message == WM_KEYDOWN) { switch(pMsg->wParam) { case VK_ESCAPE://屏蔽Esc return TRUE; } } return CDialog::PreTranslateMessage(pMsg); }