1. 程式人生 > >VC檔案關聯,詳細介紹,易懂 ,還親測可用

VC檔案關聯,詳細介紹,易懂 ,還親測可用

剛搞了個程式要做檔案關聯,可麻煩了,上網看的有些錯誤的,有些看了也不懂,所以搞了2個小時。現搞好後,發一篇易懂的介紹檔案關聯的文章:

(1)首先舉個例子先。例子中將副檔名為.tp的檔案關聯到程式。將檔案關聯到程式的重點是在登錄檔上寫一些東西,如下圖

(寫的東西都是在HKEY_CLASSES_ROOT上寫)

是在HKEY_CLASSES_ROOT展開下的。添加了一個新項.tp,而這新項的值可不是隨便亂定,因為開啟副檔名為.tp的檔案,將讀取HKEY_CLASSES_ROOT下.tp項的值,再從這值所代表的項中讀取關聯的程式相關資訊(如圖示,程式路徑)



也是在HKEY_CLASSES_ROOT

展開下的,添加了一個新項TOUCHPANEL,新項TOUCHPANEL下的DefaultIcon項的值代表關聯檔案圖示路徑(不是單純寫路徑,有些格式),新項TOUCHPANEL下的shell\\open\\command項的值代表程式路徑(不是單純寫路徑,有些格式),

到這一步,副檔名為.tp的檔案就關聯到程式了。如果不行的話,1:可能相關路徑寫錯,因為有路徑格式要求;2:之前這個該.tp副檔名的檔案可能用其它程式開啟過,造成該檔案與其它程式關聯上了,可以滑鼠右鍵該檔案,選擇開啟方式-選擇預設程式-選擇你想關聯的程式就行了

(2)可以手動新增進行測試檔案關聯,下面是VC下的unicode編碼的程式碼:

void CTouchPanelApp::setFileRelation()//呼叫設定檔案關聯
{
	TCHAR appPath[_MAX_PATH];//程式路徑
	TCHAR appName[_MAX_PATH];//寫進登錄檔的進行一些格式化的程式路徑

	const TCHAR* NAME_APP = _T("TOUCHPANEL");//應用程式名稱,其實這個名字可以隨便定的
	const TCHAR* NAME_PROJECTFILE_EXT = _T(".tp");//要關聯的檔案的副檔名

	if(GetModuleFileName(NULL, appPath, _MAX_PATH) != 0)
	{
		swprintf_s(appName, _T("\"%s\" %%1"), appPath);

		//檢查登錄檔是否已經添加了,沒有則新增
		if (!checkFileRelation(NAME_PROJECTFILE_EXT, appName, NAME_APP))
		{
			TCHAR icoPath[_MAX_PATH];
			swprintf_s(icoPath, _T("%s, 1"), appPath);
			registerFileRelation(NAME_PROJECTFILE_EXT, appName, NAME_APP, icoPath);
		}
	}
}

BOOL CTouchPanelApp::checkFileRelation(const TCHAR *strExt,
	const TCHAR *strAppName,
	const TCHAR *strAppKey)
{
	int nRet = FALSE;


	HKEY hExtKey;


	TCHAR strTemp[_MAX_PATH]; 


	DWORD dwSize=sizeof(strTemp); 


	if (RegOpenKey(HKEY_CLASSES_ROOT, strExt, &hExtKey) == ERROR_SUCCESS)
	{
		RegQueryValueEx(hExtKey, NULL, NULL, NULL, (LPBYTE)strTemp, &dwSize);

		RegCloseKey(hExtKey);

		//檢查.tp的值是否與設定的一樣
		if(_tcscmp(strTemp, strAppKey) == 0)
		{
			swprintf_s(strTemp, _T("%s\\shell\\open\\command"), strAppKey);


			if (RegOpenKey(HKEY_CLASSES_ROOT, strTemp, &hExtKey) == ERROR_SUCCESS)
			{
				RegQueryValueEx(hExtKey, NULL, NULL, NULL, (LPBYTE)strTemp, &dwSize);

				//檢查TOUCHPANEL下的\\shell\\open\\command上的程式路徑是否一樣
				if(_tcscmp(strTemp, strAppName) == 0)
				{
					nRet = TRUE;
				}


				RegCloseKey(hExtKey);
			}
		}
	}


	return nRet;
}


void CTouchPanelApp::registerFileRelation(const TCHAR *strExt, 
	const TCHAR *strAppName, 
	const TCHAR *strAppKey, 
	const TCHAR *strDefaultIcon)
{
	TCHAR strTemp[_MAX_PATH];
	HKEY hKey;

	//新項.tp
	RegCreateKey(HKEY_CLASSES_ROOT, strExt, &hKey);
	RegSetValue(hKey, _T(""), REG_SZ, strAppKey, _tcslen(strAppKey) + 1);
	RegCloseKey(hKey);

	//新項TOUCHPANEL
	RegCreateKey(HKEY_CLASSES_ROOT, strAppKey, &hKey);
	RegCloseKey(hKey);

	//新項TOUCHPANEL下DefaultIcon
	swprintf_s(strTemp, _T("%s\\DefaultIcon"), strAppKey); 
	RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey);
	RegSetValue(hKey, _T(""), REG_SZ, strDefaultIcon, _tcslen(strDefaultIcon) + 1);
	RegCloseKey(hKey);

	//新項TOUCHPANEL下shell
	swprintf_s(strTemp, _T("%s\\shell"), strAppKey);
	RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey);
	RegSetValue(hKey, _T(""), REG_SZ, _T("open"), _tcslen(_T("open")) + 1);
	RegCloseKey(hKey);

	//新項TOUCHPANEL下shell\\open\\command
	swprintf_s(strTemp, _T("%s\\shell\\open\\command"), strAppKey);
	RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey);
	RegSetValue(hKey,_T(""),REG_SZ, strAppName,_tcslen(strAppName)+1);
	RegCloseKey(hKey);
}