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

📄 contextmenu.cpp

📁 看图软件
💻 CPP
字号:
//idcomcn2008
//19:34 2008-4-9
//AddContextMenuForFile
//AddContextMenuForFolder

#include "StdAfx.h"
#include ".\contextmenu.h"

/*
*example ".jpg" "G:\\Program Files\\MySoftWare\\ScanViewer.exe"
*lpszDisplayNameForFile			为在文件菜单中显示的名称
*lpszDisplayNameForFolder		为在文件夹菜单中显示的名称
*lpszExtName					为文件扩展名“.jpg”
*lpszAppFullPath				为程序的路径
*/
CContextMenu::CContextMenu(LPCTSTR lpszDisplayNameForFile ,LPCTSTR lpszDisplayNameForFolder ,LPCTSTR lpszExtName,LPCTSTR lpszAppFullPath)
: m_strDisplayNameForFile(_T(""))
, m_strDisplayNameForFolder(_T(""))
, m_strExtName(_T(""))
, m_strAppFullPath(_T(""))
{
	if (strlen(lpszDisplayNameForFile)>0)    m_strDisplayNameForFile=lpszDisplayNameForFile;
	if (strlen(lpszDisplayNameForFolder)>0)  m_strDisplayNameForFolder=lpszDisplayNameForFolder;
	if (strlen(lpszExtName)>=2)
	{
		if (CString(lpszExtName).GetAt(0)=='.')
			m_strExtName=lpszExtName;
	}
	if (strlen(lpszAppFullPath)>0) 
	{
		m_strAppFullPath=lpszAppFullPath;
		m_strAppFullPath.TrimLeft().TrimRight();           //将字符串中前面和后面的空格整理出字符串		
		m_strAppFullPath="\""+m_strAppFullPath+"\" \"%1\"";
	}
	else
	{
		char *path=new char[MAX_PATH];
		::GetModuleFileName(NULL,path,MAX_PATH);
		m_strAppFullPath.Format("%s",path);
		m_strAppFullPath="\""+m_strAppFullPath+"\" \"%1\"";		
		delete path;
	}

	m_hRootKey=HKEY_CLASSES_ROOT;
}

CContextMenu::~CContextMenu(void)
{
}




/*
*example ".bmp" ->  "Paint.Picture"
*example ".jpg" ->  "jpegfile"
*example ".gif" ->  "giffile"
*example ".tif" ->  "TIFImage.Document"
*example ".png" ->  "pngfile"
*/
CString CContextMenu::GetImgType(LPCTSTR lpszExtName)
{
	CString strImgType="";
	// retrieves the type and data for a specified value name associated with an open registry key.
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	HKEY hKEYQuery;
	//打开与路径 lpszExtName 相关的 hKEY,第一个参数为根键名称,第二个参数表。 
	//表示要访问的键的位置,第三个参数必须为0,KEY_READ表示以查询的方式。 
	//访问注册表,hKEY则保存此函数所打开的键的句柄。 
	long lQuery=(::RegOpenKeyEx(m_hRootKey,lpszExtName, 0, KEY_READ, &hKEYQuery)); 
	if(lQuery!=ERROR_SUCCESS) //如果无法打开hKEY,则终止程序的执行 
	{
		AfxMessageBox("错误: 无法打开有关的hKEY!"); 
		return strImgType;
	} 		
	//DWORD type_1=REG_SZ ;
	DWORD  dwQuery=MAX_PATH;  	
	LPBYTE Vals=new BYTE[MAX_PATH];
	ZeroMemory(Vals, MAX_PATH);              // 用0填充新申请的空间	
	//表示要查 询的键值名,type_1表示查询数据的类型,Vals保存所。 
	//查询的数据,dwQuery表示预设置的数据长度。 		
	if ( ::RegQueryValueEx( hKEYQuery, "", 0, NULL,Vals, &dwQuery) == ERROR_SUCCESS )
	{
		strImgType=(CString)Vals;
		strImgType.TrimLeft().TrimRight();           //将字符串中前面和后面的空格整理出字符串		
		//AfxMessageBox(strImgType);
	}	
	delete Vals;
	::RegCloseKey( hKEYQuery );                      // 关闭注册表
	if (strImgType.IsEmpty()) return strImgType;	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	return strImgType;
}



void CContextMenu::AddContextMenuForFile(void)
{
	AddContextMenuForFile(m_strExtName);
}



/*
*往注册表里添加
*例如输入
* hKey = HKEY_CLASSES_ROOT ;  lpSubKey= "jpegfile\\shell\\Open in ScanViewer" ; strDisplayName = "Open in ScanViewer" ;
*结果例如为

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\jpegfile\shell\Open in ScanViewer\command]
@="\"e:\\Visual Studio 2003 Projects\\ScanViewer\\Debug\\ScanViewer.exe\" \"%1\""

*/
void CContextMenu::AddRegKey(HKEY hKey , LPCSTR lpSubKey,CString strDisplayName)
{
	CString strSubKey=lpSubKey;
	//AfxMessageBox(strSubKey);
	//Add Display Name
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	HKEY hKEYDN; 		
	DWORD dwDisposition;                                //creates the specified registry key. If the key already exists, the function opens it.
	if (::RegCreateKeyEx(hKey,(LPCTSTR)strSubKey,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKEYDN,&dwDisposition))
	{  
		AfxMessageBox("Unable to create registry key");
		return;
	}
	DWORD type_DN=REG_SZ; 
	DWORD cbData_DN=strDisplayName.GetLength()+1;	
	//表示要访问的键值名,strDisplayName表示新的键值
	//type_DN的数据类型和cbData_DN数据长度 
	long lDN=::RegSetValueEx(hKEYDN, "", NULL,type_DN,(BYTE*)(LPCSTR)CString("&"+strDisplayName),cbData_DN); 
	if(lDN!=ERROR_SUCCESS) 
	{
		AfxMessageBox("错误: 无法修改有关(设置)注册表信息!"); 
		return ; 
	} 
	//程序结束前要关闭已经打开的hKEY
	::RegCloseKey(hKEYDN);
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	strSubKey+="\\command";	
	//AfxMessageBox( m_strAppFullPath );		

	//Add AppFullPath
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	HKEY hKEYAN; 		
	DWORD dwDispAN;                                //creates the specified registry key. If the key already exists, the function opens it.
	if (::RegCreateKeyEx(hKey,(LPCTSTR)strSubKey,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKEYAN,&dwDispAN))
	{  
		AfxMessageBox("Unable to create registry key");
		return;
	}
	DWORD type_AN=REG_SZ; 
	DWORD cbData_AN=m_strAppFullPath.GetLength()+1;	
	//表示要访问的键值名,m_strAppFullPath表示新的键值
	//type_AN的数据类型和cbData_AN数据长度 
	long lAN=::RegSetValueEx(hKEYAN, "", NULL,type_AN,(BYTE*)(LPCSTR)m_strAppFullPath,cbData_AN); 
	if(lAN!=ERROR_SUCCESS) 
	{
		AfxMessageBox("错误: 无法修改有关(设置)注册表信息!"); 
		return ; 
	} 
	//程序结束前要关闭已经打开的hKEY
	::RegCloseKey(hKEYAN);
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}


// 为文件添加菜单项
void CContextMenu::AddContextMenuForFile(LPCTSTR lpszExtName)
{
	if (m_strDisplayNameForFile.IsEmpty()||m_strExtName.IsEmpty()||m_strAppFullPath.IsEmpty()) return;
	
	CString strImgType=GetImgType(lpszExtName);
	strImgType+="\\shell\\"+m_strDisplayNameForFile;		
	//AfxMessageBox(strImgType);

	//添加注表信息
	AddRegKey(m_hRootKey ,strImgType,m_strDisplayNameForFile);  
}


void CContextMenu::DelContextMenuForFile(void)
{	
	DelContextMenuForFile(m_strExtName);
}


// 删除文件菜单项
void CContextMenu::DelContextMenuForFile(LPCTSTR lpszExtName)
{
	CString strSubKey1=GetImgType(lpszExtName)+"\\shell\\"+m_strDisplayNameForFile;
	CString strSubKey2=GetImgType(lpszExtName)+"\\shell\\"+m_strDisplayNameForFile+"\\command";
	//AfxMessageBox(strSubKey);

	RegDeleteKey(m_hRootKey,strSubKey2);
	RegDeleteKey(m_hRootKey,strSubKey1);
}

// 文件菜单项是否为空
BOOL CContextMenu::IsNullForFile(void)
{	
	CString strSubKey=GetImgType(m_strExtName)+"\\shell\\"+m_strDisplayNameForFile;
	HKEY hKEYQuery;
	//打开与路径 strSubKey 相关的 hKEY,第一个参数为根键名称,第二个参数表。 
	//表示要访问的键的位置,第三个参数必须为0,KEY_READ表示以查询的方式。 
	//访问注册表,hKEY则保存此函数所打开的键的句柄。 
	long lQuery=(::RegOpenKeyEx(m_hRootKey,strSubKey, 0, KEY_READ, &hKEYQuery)); 
	if(lQuery!=ERROR_SUCCESS) //如果无法打开hKEY,则终止程序的执行 
	{
		//AfxMessageBox("错误: 无法打开有关的hKEY!"); 
		return TRUE;
	} 	
	return FALSE;
	::RegCloseKey( hKEYQuery );                      // 关闭注册表
}





// 为文件夹添加菜单项
void CContextMenu::AddContextMenuForFolder(void)
{
	if (m_strDisplayNameForFolder.IsEmpty()||m_strAppFullPath.IsEmpty()) return;

	CString strSubKey;
	strSubKey.Format("Folder\\shell\\%s",m_strDisplayNameForFolder);
	//AfxMessageBox(strSubKey);

	//添加注表信息
	AddRegKey(m_hRootKey ,strSubKey,m_strDisplayNameForFolder); 
}

// 删除文件夹菜单项
void CContextMenu::DelContextMenuForFolder(void)
{
	CString strSubKey1="Folder\\shell\\"+m_strDisplayNameForFolder;
	CString strSubKey2="Folder\\shell\\"+m_strDisplayNameForFolder+"\\command";
	//AfxMessageBox(strSubKey);

	RegDeleteKey(m_hRootKey,strSubKey2);
	RegDeleteKey(m_hRootKey,strSubKey1);
}


// 文件夹菜单项是否为空
BOOL CContextMenu::IsNullForFolder(void)
{
	CString strSubKey="Folder\\shell\\"+m_strDisplayNameForFolder;
	HKEY hKEYQuery;
	//打开与路径 strSubKey 相关的 hKEY,第一个参数为根键名称,第二个参数表。 
	//表示要访问的键的位置,第三个参数必须为0,KEY_READ表示以查询的方式。 
	//访问注册表,hKEY则保存此函数所打开的键的句柄。 
	long lQuery=(::RegOpenKeyEx(m_hRootKey,strSubKey, 0, KEY_READ, &hKEYQuery)); 
	if(lQuery!=ERROR_SUCCESS) //如果无法打开hKEY,则终止程序的执行 
	{
		//AfxMessageBox("错误: 无法打开有关的hKEY!"); 
		return TRUE;
	} 	
	return FALSE;
	::RegCloseKey( hKEYQuery );                      // 关闭注册表
}


⌨️ 快捷键说明

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