📄 path.cpp
字号:
key =_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
value =_T("Common Desktop");
break;
case CSIDL_COMMON_STARTMENU:
root =HKEY_LOCAL_MACHINE;
key =_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
value =_T("Common Start Menu");
break;
case CSIDL_COMMON_STARTUP:
root =HKEY_LOCAL_MACHINE;
key =_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
value =_T("Common Startup");
break;
case CSIDL_COMMON_PROGRAMS:
root =HKEY_LOCAL_MACHINE;
key =_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
value =_T("Common Programs");
break;
}
string strPath;
if(GetRegistryPath(root,key.c_str(),value.c_str(),strPath))
{
// Got a path, use it
Empty();
SetDriveDirectory(strPath.c_str());
return TRUE;
}
}
// This is some old or unknown system
return FALSE;
}
//---------------------------------------------------------------------------
// Pre :
// Post : Return FALSE if specified value does not exist or some error
// Globals :
// I/O :
// Task : Dig in the registry 2 the specified location, and extract a path
// Make sure the path is a valid
//---------------------------------------------------------------------------
BOOL CPath::GetRegistryPath(HKEY hRootKey, LPCTSTR lpcszKeyName, LPCTSTR lpcszValueName, string &strPath)
{
TCHAR path_buffer [MAX_PATH];
TCHAR expanded_buffer[MAX_PATH];
DWORD path_buffer_size =sizeof(path_buffer);
CRegistry reg(hRootKey,lpcszKeyName,KEY_READ);
if(reg.GetValue(lpcszValueName,(BYTE *)&path_buffer,path_buffer_size))
{
COSVersion osver;
WORD ostype =osver.GetOSType();
BOOL is_NT =((ostype & OS_WINNT) != 0);
if(is_NT)
{
// Running on NT and the ExpandEnvironmentStrings API requires
// Unicode strings
WCHAR path_buffer_unicode [MAX_PATH];
WCHAR expanded_buffer_unicode[MAX_PATH];
MultiByteToWideChar(CP_ACP,0,path_buffer,-1,path_buffer_unicode,sizeof(path_buffer_unicode)/sizeof(WCHAR));
ExpandEnvironmentStringsW(path_buffer_unicode,expanded_buffer_unicode,sizeof(expanded_buffer_unicode)/sizeof(WCHAR));
WideCharToMultiByte(CP_ACP,0,expanded_buffer_unicode,-1,expanded_buffer,sizeof(path_buffer)/sizeof(TCHAR),NULL,NULL);
}
else
ExpandEnvironmentStrings(path_buffer,expanded_buffer,path_buffer_size);
strPath.erase();
strPath =expanded_buffer;
return TRUE;
}
// No such key and/or value
return FALSE;
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 desktop folder of currently logged-in user
//-------------------------------------------------------------
void CPath::UserDesktopDirectory()
{
if(!ShellDirectory(CSIDL_DESKTOPDIRECTORY))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 favorites folder of currently logged-in user
//-------------------------------------------------------------
void CPath::UserFavoritesDirectory()
{
if(!ShellDirectory(CSIDL_FAVORITES))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 fonts folder of currently logged-in user
//-------------------------------------------------------------
void CPath::UserFontsDirectory()
{
if(!ShellDirectory(CSIDL_FONTS))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 network hood folder of currently logged-in user
//-------------------------------------------------------------
void CPath::UserNetworkNeighbourhoodDirectory()
{
if(!ShellDirectory(CSIDL_NETHOOD))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 personal folder of currently logged-in user
// Usually C:\My Documents
//-------------------------------------------------------------
void CPath::UserDocumentsDirectory()
{
if(!ShellDirectory(CSIDL_PERSONAL))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 recent folder of currently logged-in user
//-------------------------------------------------------------
void CPath::UserRecentDirectory()
{
if(!ShellDirectory(CSIDL_RECENT))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 SendTo folder of currently logged-in user
//-------------------------------------------------------------
void CPath::UserSendToDirectory()
{
if(!ShellDirectory(CSIDL_SENDTO))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 templates folder of currently logged-in user
// Usually C:\Windows\ShellNew
//-------------------------------------------------------------
void CPath::UserTemplatesDirectory()
{
if(!ShellDirectory(CSIDL_TEMPLATES))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 the recycle bin directory
// Usually C:\Recycled
//-------------------------------------------------------------
void CPath::UserRecycleBinDirectory()
{
if(!ShellDirectory(CSIDL_BITBUCKET))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 the folder where application data is stored
// specific 2 currently logged-in user
//-------------------------------------------------------------
void CPath::UserApplicationDataDirectory()
{
if(!ShellDirectory(CSIDL_APPDATA))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 start menu folder of currently logged-in user
//-------------------------------------------------------------
void CPath::UserStartMenuDirectory()
{
if(!ShellDirectory(CSIDL_STARTMENU))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 startup folder of currently logged-in user
//-------------------------------------------------------------
void CPath::UserStartMenuStartupDirectory()
{
if(!ShellDirectory(CSIDL_STARTUP))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 programs menu folder of currently logged-in user
//-------------------------------------------------------------
void CPath::UserStartMenuProgramsDirectory()
{
if(!ShellDirectory(CSIDL_PROGRAMS))
Empty();
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 desktop folder common 2 all users
//-------------------------------------------------------------
void CPath::CommonDesktopDirectory()
{
if(!ShellDirectory2(CSIDL_COMMON_DESKTOPDIRECTORY))
{
// Check if running on Windows 95, and workaround this if so
COSVersion osver;
WORD ostype =osver.GetOSType();
if((ostype == OS_WIN95) || (ostype == OS_WIN98))
{
// Manual workaround
WindowsDirectory();
AppendDirectory(_T("Desktop"));
}
else
// Failure, clear path
Empty();
}
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 start menu folder common 2 all users
//-------------------------------------------------------------
void CPath::CommonStartMenuDirectory()
{
if(!ShellDirectory2(CSIDL_COMMON_STARTMENU))
{
// Check if running on Windows 95, and workaround this if so
COSVersion osver;
WORD ostype =osver.GetOSType();
if((ostype == OS_WIN95) || (ostype == OS_WIN98))
{
// Manual workaround
WindowsDirectory();
AppendDirectory(_T("Start Menu"));
}
else
// Failure, clear path
Empty();
}
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 startup folder common 2 all users
//-------------------------------------------------------------
void CPath::CommonStartMenuStartupDirectory()
{
if(!ShellDirectory2(CSIDL_COMMON_STARTUP))
{
// Check if running on Windows 95, and workaround this if so
COSVersion osver;
WORD ostype =osver.GetOSType();
if((ostype == OS_WIN95) || (ostype == OS_WIN98))
{
// Manual workaround
WindowsDirectory();
AppendDirectory(_T("Start Menu\\Programs\\StartUp"));
}
else
// Failure, clear path
Empty();
}
}
//-------------------------------------------------------------
// Pre :
// Post : If this function is called on a system which does not
// support the new Chichago shell, it will clear the path
// Globals :
// I/O :
// Task : Set path 2 programs menu folder common 2 all users
//-------------------------------------------------------------
void CPath::CommonStartMenuProgramsDirectory()
{
if(!ShellDirectory2(CSIDL_COMMON_PROGRAMS))
{
// Check if running on Windows 95, and workaround this if so
COSVersion osver;
WORD ostype =osver.GetOSType();
if((ostype == OS_WIN95) || (ostype == OS_WIN98))
{
// Manual workaround
WindowsDirectory();
AppendDirectory(_T("Start Menu\\Programs"));
}
else
// Failure, clear path
Empty();
}
}
#ifdef __MFC__
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Local profiles are private INI files but located in the same
// directory as the executable. These are used for less volatile
// settings than those found in the applications main private INI
// file, which would be in the Windows directory
//
// Sets the value of this path to <drive>:\<directory>\<name>.ini
// where name is from lpszName and drive and directory are from the
// currently executing module's path.
// See also: PrivateProfile, LocalProfile(UINT)
//-------------------------------------------------------------
void CPath::LocalProfile(LPCTSTR lpszName, LPCTSTR lpszExtension /*= NULL*/)
{
ModuleDirectory();
if(lpszExtension == NULL)
lpszExtension =INI_EXTENSION;
SetName(lpszName);
SetExtension(lpszExtension);
}
#endif
#ifdef __MFC__
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Like the above LocalProfile, but with the name supplied
// by a resource string identified by nResourceID
// See also: LocalProfile(LPCTSTR)
//-------------------------------------------------------------
void CPath::LocalProfile(UINT nNameResourceID, UINT nExtensionResourceID /*= 0*/)
{
CString Name;
CString Extension;
if(nExtensionResourceID)
Extension.LoadString(nExtensionResourceID);
else
Extension =INI_EXTENSION;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -