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

📄 fileregdlg.cpp

📁 热热舞热舞热舞热舞人玩儿玩儿玩儿侮辱我让我让我如往日
💻 CPP
字号:
// FileRegDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileReg.h"
#include "FileRegDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CFileRegDlg dialog

CFileRegDlg::CFileRegDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFileRegDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFileRegDlg)
		// 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 CFileRegDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileRegDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFileRegDlg, CDialog)
	//{{AFX_MSG_MAP(CFileRegDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SELECT, OnSelect)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileRegDlg message handlers

BOOL CFileRegDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFileRegDlg::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 CFileRegDlg::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 CFileRegDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

#define DATASIZE 1688
DWORD theDWRegType = REG_SZ | REG_EXPAND_SZ;
void CFileRegDlg::OnSelect() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE);
	if (dlg.DoModal() == IDCANCEL)
		return;
	
	// 注册表中的对应主键由文件扩展名给出
	m_strPath = dlg.GetPathName();
	SetDlgItemText(IDC_EDIT_PATH,m_strPath);
	CString strRegEntry = "." + dlg.GetFileExt();
	CString strInto;

	// 打开HKEY_CLASSES_ROOT主键下与文件扩展名相关的主键
	::RegOpenKeyEx(HKEY_CLASSES_ROOT,strRegEntry,NULL,KEY_EXECUTE,&m_hKey);

	char chData[DATASIZE];
	DWORD dwSize = DATASIZE;
	// 获得记录该类文件的信息入口
	::RegQueryValueEx(m_hKey,"",NULL,&theDWRegType,(LPBYTE)chData,&dwSize);
	strRegEntry = chData;
	dwSize = DATASIZE;
	::RegCloseKey(m_hKey);

	// 文件类型在默认的数据中定义
	HICON hIcon;
	::RegOpenKeyEx(HKEY_CLASSES_ROOT,strRegEntry,NULL,KEY_EXECUTE,&m_hKey);
	if (::RegQueryValueEx(m_hKey,"",NULL,&theDWRegType,(LPBYTE)chData,&dwSize)
		!= ERROR_SUCCESS)
	{
		// 若找不到合适的类型定义则用扩展名作为其类型定义
		strInto = dlg.GetFileExt();
		strInto += "类型文件";
		memcpy(chData,(LPCTSTR)strInto,strInto.GetLength() + 1);
	}
	RegCloseKey(m_hKey);
	dwSize = DATASIZE;
	SetDlgItemText(IDC_EDIT_FILETYPE,chData);

	// 获得默认的打开方式
	dwSize = DATASIZE;
	strInto = strRegEntry + "\\shell";
	if (::RegOpenKeyEx(HKEY_CLASSES_ROOT,strInto,NULL,KEY_EXECUTE,&m_hKey)
		!= ERROR_SUCCESS)
		SetDlgItemText(IDC_EDIT_OPENFILE,"");
	else 
	{
		if (::RegQueryValueEx(m_hKey,"",NULL,&theDWRegType,(LPBYTE)chData,&dwSize)
			!= ERROR_SUCCESS)
			strInto = _T("");
		else 
			strInto = chData;
		dwSize = DATASIZE;
		if (strInto.IsEmpty())
		{
			// 在第一个子键默认的打开方式
			if (::RegEnumKeyEx(m_hKey,0,(LPTSTR)chData,&dwSize,NULL,NULL,NULL,NULL)
				!= ERROR_SUCCESS)
				strInto = _T("");
			else
				strInto = chData;
			dwSize = DATASIZE;
		}
		// command子键中记录默认的打开方式
		strInto += "\\command";
		if (::RegOpenKeyEx(m_hKey,strInto,NULL,KEY_EXECUTE,&m_hKey)
			!= ERROR_SUCCESS)
			SetDlgItemText(IDC_EDIT_OPENFILE,"");
		else if (::RegQueryValueEx(m_hKey,"",NULL,&theDWRegType,(LPBYTE)chData,&dwSize)
			!= ERROR_SUCCESS)
			SetDlgItemText(IDC_EDIT_OPENFILE,"");
		else
		{
			strInto = chData;
			dwSize = DATASIZE;
			SetDlgItemText(IDC_EDIT_OPENFILE,strInto);
		}
		::RegCloseKey(m_hKey);
	}

	// 图标信息记录在DefaultIcon子键内
	// 得到图标并显示在图像控件中
	strInto = strRegEntry + "\\DefaultIcon";
	if (::RegOpenKeyEx(HKEY_CLASSES_ROOT,strInto,NULL,KEY_EXECUTE,&m_hKey)
		!= ERROR_SUCCESS)
		hIcon = AfxGetApp()->LoadIcon(IDI_UNKNOWN);
	else
	{
		if (::RegQueryValueEx(m_hKey,"",NULL,&theDWRegType,(LPBYTE)chData,&dwSize)
			!= ERROR_SUCCESS)
			hIcon = AfxGetApp()->LoadIcon(IDI_UNKNOWN);
		else
		{
			strInto = chData;
			int nFind = strInto.Find(',');
			if (nFind == -1)
			{
				if (strInto.Compare("%1") == 0)
					::ExtractIconEx(m_strPath,0,&hIcon,NULL,1);
				else
					hIcon = AfxGetApp()->LoadIcon(IDI_UNKNOWN);
			}
			else
			{
				CString strFile = strInto.Left(nFind);
				CString strIndex = strInto.Right(strInto.GetLength() - nFind - 1);
				nFind = atoi((LPCTSTR)strIndex);
				::ExtractIconEx(strFile,nFind,&hIcon,NULL,1);
			}
			RegCloseKey(m_hKey);
		}
	}

	((CStatic *)GetDlgItem(IDC_FILE_ICON))->SetIcon(hIcon);
	GetDlgItem(IDC_OPEN)->EnableWindow();
}

void CFileRegDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	CString strCmdLine;
	GetDlgItemText(IDC_EDIT_OPENFILE,strCmdLine);
	OpenFile(strCmdLine);
}

void CFileRegDlg::OpenFile(LPCTSTR lpszCmdLine)
{
	CString strOpen = lpszCmdLine;
	strOpen.MakeUpper();
	if (strOpen.IsEmpty())
	{
		strOpen.Format("Rundll32.exe shell32.dll OpenAs_RunDLL %s",m_strPath);
	}

	if (strOpen.Find("%L") != -1)
		strOpen.Replace("%L",m_strPath);
	else if (strOpen.Find("%1") != -1)
		strOpen.Replace("%1",m_strPath);

	// 根据注册信息打开选定的文件
	::WinExec(strOpen,SW_SHOW);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -