⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 regdlg.cpp

📁 c++系统开发实例精粹内附的80例源代码 环境:windows2000,c++6.0
💻 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 + -