?? regdlg.cpp
字號:
// RegDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Reg.h"
#include "RegDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CRegDlg dialog
CRegDlg::CRegDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRegDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRegDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRegDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRegDlg)
DDX_Control(pDX, IDC_STATIC_FILETYPE, m_staFileType);
DDX_Control(pDX, IDC_STATIC_FILEPATH, m_staFilePath);
DDX_Control(pDX, IDC_STATIC_FILEICON, m_staFileIcon);
DDX_Control(pDX, IDC_STATIC_ASSOCIATETYPE, m_staAssociateType);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRegDlg, CDialog)
//{{AFX_MSG_MAP(CRegDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_FILEASSOSIATE, OnBtnFileassosiate)
ON_BN_CLICKED(IDC_BUTTON_SELECTFILE, OnBtnSelectFile)
ON_BN_CLICKED(IDC_BUTTON_OPENFILE, OnBtnOpenFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRegDlg message handlers
BOOL CRegDlg::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
//判斷當前Windows版本
OSVERSIONINFO WinVersion;
WinVersion.dwOSVersionInfoSize =sizeof(OSVERSIONINFOEX);
if(GetVersionEx(&WinVersion))
//Windows 2000 對應dwMajorVersion=5且dwMinorVersion=0
if(WinVersion.dwMajorVersion!=5||WinVersion.dwMinorVersion!=0)
{
//若當前不是Windows 2000 系統,則程序退出
AfxMessageBox(_T("本程序適用于Windows 2000操作系統!"),MB_OK|MB_ICONINFORMATION);
::PostMessage(this->m_hWnd,WM_QUIT,0,0);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CRegDlg::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 CRegDlg::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 CRegDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CRegDlg::OnBtnFileassosiate()
{
long lResult=0;
CString strSubKey[4];
DWORD dwDisposition;
//得到Reg.exe文件路徑
CFile RegFile(_T("Reg.exe"), CFile::shareDenyNone);
CString RegFilePath=RegFile.GetFilePath();
RegFile.Close();
//要寫入的4個子鍵名稱
strSubKey[0]=".rt";
strSubKey[1]="RegTest.File";
strSubKey[2]="RegTest.File\\DefaultIcon" ;
strSubKey[3]="RegTest.File\\shell\\open\\Command";
//對應的4個子鍵鍵值數據
char lpData[4][80];
CString strData[4];
strData[0]="RegTest.File";
strData[1]="RegTest File";
strData[2]=RegFilePath+",1";
strData[3]=RegFilePath+" \"%1\"";
int i=0,j=0;
for(i=0;i<=3;i++)
for(j=0;j<strData[i].GetLength();j++)
lpData[i][j]=strData[i].GetAt(j);
for(i=0;i<=3;i++)
{
//創建新鍵
lResult=RegCreateKeyEx(HKEY_CLASSES_ROOT,strSubKey[i],
0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&m_hKey,&dwDisposition);
//如果創建新鍵成功,設置新鍵鍵值數據
if (lResult==ERROR_SUCCESS)
RegSetValueEx(m_hKey,NULL,0,REG_SZ,(LPBYTE) lpData[i],strData[i].GetLength());
//關閉鍵句柄
RegCloseKey(m_hKey);
}
}
#define BUFSIZE 128 //緩沖區大小
//“選擇文件”按鈕響應函數
void CRegDlg::OnBtnSelectFile()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE);
if (dlg.DoModal() == IDCANCEL)
return;
// 得到選擇的文件的路徑及擴展名
m_strFilePath = dlg.GetPathName();
m_staFilePath.SetWindowText(m_strFilePath);
CString strRegEntry = "." + dlg.GetFileExt();
CString strRegInfo;
char cData[BUFSIZE];
DWORD dwBufSize = BUFSIZE;
DWORD dwRegType=REG_SZ | REG_EXPAND_SZ;
// 打開HKEY_CLASSES_ROOT主鍵下與文件擴展名相關的主鍵
RegOpenKeyEx(HKEY_CLASSES_ROOT,strRegEntry,NULL,KEY_EXECUTE,&m_hKey);
// 獲得記錄該類文件的信息入口
RegQueryValueEx(m_hKey,"",NULL,&dwRegType,(LPBYTE)cData,&dwBufSize);
strRegEntry = cData;
RegCloseKey(m_hKey);
// 文件類型在默認的數據中定義
HICON hIcon;
dwBufSize = BUFSIZE;
//打開失敗則返回
if(RegOpenKeyEx(HKEY_CLASSES_ROOT,strRegEntry,NULL,KEY_EXECUTE,&m_hKey)!=ERROR_SUCCESS)
{
AfxMessageBox(_T("打開注冊表項失敗!"),MB_OK|MB_ICONINFORMATION);
return;
}
// 若找不到合適的類型定義則用擴展名作為其類型定義
if(RegQueryValueEx(m_hKey,"",NULL,&dwRegType,(LPBYTE)cData,&dwBufSize)!= ERROR_SUCCESS)
{
strRegInfo = dlg.GetFileExt();
strRegInfo += "類型文件";
memcpy(cData,(LPCTSTR)strRegInfo,strRegInfo.GetLength() + 1);
}
RegCloseKey(m_hKey);
m_staFileType.SetWindowText(cData);
// 獲得默認的打開方式
dwBufSize = BUFSIZE;
strRegInfo = strRegEntry + "\\shell";
if (RegOpenKeyEx(HKEY_CLASSES_ROOT,strRegInfo,NULL,KEY_EXECUTE,&m_hKey)!= ERROR_SUCCESS)
m_staAssociateType.SetWindowText("");
else
{
if (RegQueryValueEx(m_hKey,"",NULL,&dwRegType,(LPBYTE)cData,&dwBufSize)
!= ERROR_SUCCESS)
strRegInfo = _T("");
else
strRegInfo = cData;
if (strRegInfo.IsEmpty())
{
// 在第一個子鍵默認的打開方式
dwBufSize = BUFSIZE;
if (RegEnumKeyEx(m_hKey,0,(LPTSTR)cData,&dwBufSize,NULL,NULL,NULL,NULL)
!= ERROR_SUCCESS)
strRegInfo = _T("");
else
strRegInfo = cData;
}
// command子鍵中記錄默認的打開方式
strRegInfo += "\\command";
dwBufSize = BUFSIZE;
if (RegOpenKeyEx(m_hKey,strRegInfo,NULL,KEY_EXECUTE,&m_hKey)!= ERROR_SUCCESS)
m_staAssociateType.SetWindowText("");
else if (RegQueryValueEx(m_hKey,"",NULL,&dwRegType,(LPBYTE)cData,&dwBufSize)
!= ERROR_SUCCESS)
m_staAssociateType.SetWindowText("");
else
{
strRegInfo = cData;
m_staAssociateType.SetWindowText(strRegInfo);
}
RegCloseKey(m_hKey);
}
// 關聯文件的默認圖標在DefaultIcon子鍵內
// 得到圖標并顯示在IDC_STATIC_FILEICON控件中
strRegInfo = strRegEntry + "\\DefaultIcon";
dwBufSize = BUFSIZE;
if (RegOpenKeyEx(HKEY_CLASSES_ROOT,strRegInfo,NULL,KEY_EXECUTE,&m_hKey)
!= ERROR_SUCCESS)
hIcon = AfxGetApp()->LoadIcon(IDI_FILEICON);
else
{
if (RegQueryValueEx(m_hKey,"",NULL,&dwRegType,(LPBYTE)cData,&dwBufSize)
!= ERROR_SUCCESS)
hIcon = AfxGetApp()->LoadIcon(IDI_FILEICON);
else
{
strRegInfo = cData;
int nFind = strRegInfo.Find(',');
if (nFind == -1)
{
if (strRegInfo.Compare("%1") == 0)
ExtractIconEx(m_strFilePath,0,&hIcon,NULL,1);
else
hIcon = AfxGetApp()->LoadIcon(IDI_FILEICON);
}
else
{
CString strFile = strRegInfo.Left(nFind);
CString strIndex = strRegInfo.Right(strRegInfo.GetLength() - nFind - 1);
nFind = atoi((LPCTSTR)strIndex);
ExtractIconEx(strFile,nFind,&hIcon,NULL,1);
}
RegCloseKey(m_hKey);
}
}
m_staFileIcon.SetIcon(hIcon);
GetDlgItem(IDC_BUTTON_OPENFILE)->EnableWindow(TRUE);
}
//“打開文件”按鈕響應函數
void CRegDlg::OnBtnOpenFile()
{
CString strOpen;
char cCmd[80];
m_staAssociateType.GetWindowText(cCmd,80);
strOpen.Format("%s",cCmd);
strOpen.MakeUpper();
if (strOpen.IsEmpty())
{
strOpen.Format("Rundll32.exe shell32.dll OpenAs_RunDLL %s",m_strFilePath);
}
if (strOpen.Find("%L") != -1)
strOpen.Replace("%L",m_strFilePath);
else if (strOpen.Find("%1") != -1)
strOpen.Replace("%1",m_strFilePath);
// 根據注冊信息打開選定的文件
WinExec(strOpen,SW_SHOW);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -