本文共 2191 字,大约阅读时间需要 7 分钟。
譬如:注册文件类型.pln,为其指定文件类型图标,并使用当前程序作为默认打开程序。 调用如下: RegisterShellFileType(".pln","Plan.Documnet", "规划文件", 1); 其中参数的含义: ● filterExt:文件后缀名 ● fileTypeID:文件类型ID ● fileTypeDesc:文件描述 ● iconIndex:文件图标在当前程序中的索引
BOOL CPlanningApp::RegisterShellFileType(CStringfilterExt, CString fileTypeID, CString fileTypeDesc, int iconIndex)
{
CString strPathName, strTemp;
TCHAR szLongPathName[_MAX_PATH];
TCHAR szShortPathName[_MAX_PATH];
::GetModuleFileName(this->m_hInstance, szLongPathName, _MAX_PATH);
if (::GetShortPathName(szLongPathName, szShortPathName, _MAX_PATH) == 0)
{
// rare failure case (especially on not-so-modern file systems)
strPathName = szLongPathName;
}
else
{
strPathName = szShortPathName;
}
// first register the type ID of our server
if (!SetClassesKey(fileTypeID, fileTypeDesc))
return FALSE; // just skipit
// path\DefaultIcon = path,1
strTemp.Format("%s\\DefaultIcon", (LPCTSTR)fileTypeID);
CString strDefaultIcon;
strDefaultIcon.Format("\"%s\",%d", strPathName,iconIndex);
if (!SetClassesKey(strTemp, strDefaultIcon))
return FALSE; // just skip it
// path\shell\open\command = path filename
strTemp.Format("%s\\shell\\open\\%s", (LPCTSTR)fileTypeID,
"command");
CString strOpenCommand;
strOpenCommand.Format("\"%s\" \"%%1\"",strPathName);
if (!SetClassesKey(strTemp, strOpenCommand))
return FALSE; // just skipit
// no association for that suffix
if (!SetClassesKey(filterExt, fileTypeID))
return FALSE; // just skipit
return TRUE;
}
BOOL CPlanningApp::SetClassesKey(LPCTSTRlpszKey, LPCTSTR lpszValue, LPCTSTR lpszValueName)
{
if (lpszValueName == NULL)
{
if (::RegSetValue(HKEY_CLASSES_ROOT, lpszKey, REG_SZ,
lpszValue, lstrlen(lpszValue) *sizeof(TCHAR)) != ERROR_SUCCESS)
{
TRACE1("Warning:registration database update failed for key '%s'.\n",
lpszKey);
return FALSE;
}
return TRUE;
}
else
{
HKEY hKey;
if(::RegCreateKey(HKEY_CLASSES_ROOT, lpszKey, &hKey) ==ERROR_SUCCESS)
{
LONG lResult = ::RegSetValueEx(hKey,lpszValueName, 0, REG_SZ,
(CONSTBYTE*)lpszValue, (lstrlen(lpszValue) + 1) * sizeof(TCHAR));
if(::RegCloseKey(hKey)== ERROR_SUCCESS && lResult == ERROR_SUCCESS)
return TRUE;
}
TRACE1("Warning: registration database update failed for key'%s'.\n", lpszKey);
return FALSE;
}
}
转载地址:http://fqnws.baihongyu.com/