SHGetSpecialFolderLocation

Windows特殊文件夹位置

WINSHELLAPI HRESULT WINAPI SHGetSpecialFolderLocation(
HWND hwndOwner,
int nFolder,
LPITEMIDLIST *ppidl
);

CSIDL_ALTSTARTUP File system directory that corresponds to the user's nonlocalized Startup program group.
CSIDL_APPDATA File system directory that serves as a common repository for application-specific data.
CSIDL_BITBUCKET File system directory containing file objects in the user's Recycle Bin. The location of this directory is not in the registry; it is marked with the hidden and system attributes to prevent the user from moving or deleting it.
CSIDL_COMMON_ALTSTARTUP File system directory that corresponds to the nonlocalized Startup program group for all users.
CSIDL_COMMON_DESKTOPDIRECTORY File system directory that contains files and folders that appear on the desktop for all users.
CSIDL_COMMON_FAVORITES File system directory that serves as a common repository for all users' favorite items.
CSIDL_COMMON_PROGRAMS File system directory that contains the directories for the common program groups that appear on the Start menu for all users.
CSIDL_COMMON_STARTMENU File system directory that contains the programs and folders that appear on the Start menu for all users.
CSIDL_COMMON_STARTUP File system directory that contains the programs that appear in the Startup folder for all users.
CSIDL_CONTROLS Virtual folder containing icons for the Control Panel applications.
CSIDL_COOKIES File system directory that serves as a common repository for Internet cookies.
CSIDL_DESKTOP Windows Desktop—virtual folder at the root of the namespace.
CSIDL_DESKTOPDIRECTORY File system directory used to physically store file objects on the desktop (not to be confused with the desktop folder itself).
CSIDL_DRIVES My Computer—virtual folder containing everything on the local computer: storage devices, printers, and Control Panel. The folder may also contain mapped network drives.
CSIDL_FAVORITES File system directory that serves as a common repository for the user's favorite items.
CSIDL_FONTS Virtual folder containing fonts.
CSIDL_HISTORY File system directory that serves as a common repository for Internet history items.
CSIDL_INTERNET Virtual folder representing the Internet.
CSIDL_INTERNET_CACHE File system directory that serves as a common repository for temporary Internet files.
CSIDL_NETHOOD File system directory containing objects that appear in the network neighborhood.
CSIDL_NETWORK Network Neighborhood Folder—virtual folder representing the top level of the network hierarchy.
CSIDL_PERSONAL File system directory that serves as a common repository for documents.
CSIDL_PRINTERS Virtual folder containing installed printers.
CSIDL_PRINTHOOD File system directory that serves as a common repository for printer links.
CSIDL_PROGRAMS File system directory that contains the user's program groups (which are also file system directories).
CSIDL_RECENT File system directory that contains the user's most recently used documents.
CSIDL_SENDTO File system directory that contains Send To menu items.
CSIDL_STARTMENU File system directory containing Start menu items.
CSIDL_STARTUP File system directory that corresponds to the user's Startup program group. The system starts these programs whenever any user logs onto Windows NT or starts Windows 95.
CSIDL_TEMPLATES File system directory that serves as a common repository for document templates.

CSIDL_ADMINTOOLS (FOLDERID_AdminTools) Version 5.0 . The file system directory that is used to store administrative tools for an individual user. The Microsoft Management Console (MMC) will save customized consoles to this directory, and it will roam with the user.

<!----> CSIDL_ALTSTARTUP (FOLDERID_Startup) The file system directory that corresponds to the user's nonlocalized Startup program group. This value is recognized in WindowsVista for backward compatibility, but the folder itself no longer exists.

<!----> CSIDL_APPDATA (FOLDERID_RoamingAppData) Version 4.71. The file system directory that serves as a common repository for application-specific data. A typical path is C:/Documents and Settings/username/Application Data. This CSIDL is supported by the redistributable Shfolder.dll for systems that do not have the Microsoft Internet Explorer 4.0 integrated Shell installed.

<!----> CSIDL_BITBUCKET (FOLDERID_RecycleBinFolder) The virtual folder that contains the objects in the user's Recycle Bin.

<!----> CSIDL_CDBURN_AREA (FOLDERID_CDBurning) Version 6.0. The file system directory that acts as a staging area for files waiting to be written to a CD. A typical path is C:/Documents and Settings/username/Local Settings/Application Data/Microsoft/CD Burning.

<!----> CSIDL_COMMON_ADMINTOOLS (FOLDERID_CommonAdminTools) Version 5.0. The file system directory that contains administrative tools for all users of the computer.

<!----> CSIDL_COMMON_ALTSTARTUP (FOLDERID_CommonStartup) The file system directory that corresponds to the nonlocalized Startup program group for all users. Valid only for Microsoft Windows NT systems. This value is recognized in WindowsVista for backward compatibility, but the folder itself no longer exists.

<!----> CSIDL_COMMON_APPDATA (FOLDERID_ProgramData) Version 5.0. The file system directory that contains application data for all users. A typical path is C:/Documents and Settings/All Users/Application Data. This folder is used for application data that is not user specific. For example, an application can store a spell-check dictionary, a database of clip art, or a log file in the CSIDL_COMMON_APPDATA folder. This information will not roam and is available to anyone using the computer.

<!----> CSIDL_COMMON_DESKTOPDIRECTORY (FOLDERID_PublicDesktop) The file system directory that contains files and folders that appear on the desktop for all users. A typical path is C:/Documents and Settings/All Users/Desktop. Valid only for Windows NT systems.

<!----> CSIDL_COMMON_DOCUMENTS (FOLDERID_PublicDocuments) The file system directory that contains documents that are common to all users. A typical paths is C:/Documents and Settings/All Users/Documents. Valid for Windows NT systems and Microsoft Windows 95 and Windows 98 systems with Shfolder.dll installed.

<!----> CSIDL_COMMON_FAVORITES (FOLDERID_Favorites) The file system directory that serves as a common repository for favorite items common to all users. Valid only for Windows NT systems.

<!----> CSIDL_COMMON_MUSIC (FOLDERID_PublicMusic) Version 6.0. The file system directory that serves as a repository for music files common to all users. A typical path is C:/Documents and Settings/All Users/Documents/My Music.

<!----> CSIDL_COMMON_OEM_LINKS (FOLDERID_CommonOEMLinks) This value is recognized in WindowsVista for backward compatibility, but the folder itself is no longer used.

<!----> CSIDL_COMMON_PICTURES (FOLDERID_PublicPictures) Version 6.0. The file system directory that serves as a repository for image files common to all users. A typical path is C:/Documents and Settings/All Users/Documents/My Pictures.

<!----> CSIDL_COMMON_PROGRAMS (FOLDERID_CommonPrograms) The file system directory that contains the directories for the common program groups that appear on the Start menu for all users. A typical path is C:/Documents and Settings/All Users/Start Menu/Programs. Valid only for Windows NT systems.

<!----> CSIDL_COMMON_STARTMENU (FOLDERID_CommonStartMenu) The file system directory that contains the programs and folders that appear on the Start menu for all users. A typical path is C:/Documents and Settings/All Users/Start Menu. Valid only for Windows NT systems.

<!----> CSIDL_COMMON_STARTUP (FOLDERID_CommonStartup) The file system directory that contains the programs that appear in the Startup folder for all users. A typical path is C:/Documents and Settings/All Users/Start Menu/Programs/Startup. Valid only for Windows NT systems.

<!----> CSIDL_COMMON_TEMPLATES (FOLDERID_CommonTemplates) The file system directory that contains the templates that are available to all users. A typical path is C:/Documents and Settings/All Users/Templates. Valid only for Windows NT systems.

<!----> CSIDL_COMMON_VIDEO (FOLDERID_PublicVideos) Version 6.0. The file system directory that serves as a repository for video files common to all users. A typical path is C:/Documents and Settings/All Users/Documents/My Videos.

<!----> CSIDL_COMPUTERSNEARME (FOLDERID_NetworkFolder) The folder that represents other computers in your workgroup.

<!----> CSIDL_CONNECTIONS (FOLDERID_ConnectionsFolder) The virtual folder that represents Network Connections, that contains network and dial-up connections.

<!----> CSIDL_CONTROLS (FOLDERID_ControlPanelFolder) The virtual folder that contains icons for the Control Panel applications.

<!----> CSIDL_COOKIES (FOLDERID_Cookies) The file system directory that serves as a common repository for Internet cookies. A typical path is C:/Documents and Settings/username/Cookies.

<!----> CSIDL_DESKTOP (FOLDERID_Desktop) The virtual folder that represents the Windows desktop, the root of the namespace.

<!----> CSIDL_DESKTOPDIRECTORY (FOLDERID_Desktop) The file system directory used to physically store file objects on the desktop (not to be confused with the desktop folder itself). A typical path is C:/Documents and Settings/username/Desktop.

<!----> CSIDL_DRIVES (FOLDERID_ComputerFolder) The virtual folder that represents My Computer, containing everything on the local computer: storage devices, printers, and Control Panel. The folder can also contain mapped network drives.

<!----> CSIDL_FAVORITES (FOLDERID_Favorites) The file system directory that serves as a common repository for the user's favorite items. A typical path is C:/Documents and Settings/username/Favorites.

<!----> CSIDL_FONTS (FOLDERID_Fonts) A virtual folder that contains fonts. A typical path is C:/Windows/Fonts.

<!----> CSIDL_HISTORY (FOLDERID_History) The file system directory that serves as a common repository for Internet history items.

<!----> CSIDL_INTERNET (FOLDERID_InternetFolder) A virtual folder for Internet Explorer.

<!----> CSIDL_INTERNET_CACHE (FOLDERID_InternetCache) Version 4.72. The file system directory that serves as a common repository for temporary Internet files. A typical path is C:/Documents and Settings/username/Local Settings/Temporary Internet Files.

<!----> CSIDL_LOCAL_APPDATA (FOLDERID_LocalAppData) Version 5.0. The file system directory that serves as a data repository for local (nonroaming) applications. A typical path is C:/Documents and Settings/username/Local Settings/Application Data.

<!----> CSIDL_MYDOCUMENTS (FOLDERID_Documents) Version 6.0. The virtual folder that represents the My Documents desktop item. This value is equivalent to CSIDL_PERSONAL .

<!----> CSIDL_MYMUSIC (FOLDERID_Music) The file system directory that serves as a common repository for music files. A typical path is C:/Documents and Settings/User/My Documents/My Music.

<!----> CSIDL_MYPICTURES (FOLDERID_Pictures) Version 5.0. The file system directory that serves as a common repository for image files. A typical path is C:/Documents and Settings/username/My Documents/My Pictures.

<!----> CSIDL_MYVIDEO (FOLDERID_Videos) Version 6.0. The file system directory that serves as a common repository for video files. A typical path is C:/Documents and Settings/username/My Documents/My Videos.

<!----> CSIDL_NETHOOD (FOLDERID_NetHood) A file system directory that contains the link objects that may exist in the My Network Places virtual folder. It is not the same as CSIDL_NETWORK , which represents the network namespace root. A typical path is C:/Documents and Settings/username/NetHood.

<!----> CSIDL_NETWORK (FOLDERID_NetworkFolder) A virtual folder that represents Network Neighborhood, the root of the network namespace hierarchy.

<!----> CSIDL_PERSONAL (FOLDERID_Documents) Version 6.0. The virtual folder that represents the My Documents desktop item. This is equivalent to CSIDL_MYDOCUMENTS .

Previous to Version 6.0. The file system directory used to physically store a user's common repository of documents. A typical path is C:/Documents and Settings/username/My Documents. This should be distinguished from the virtual My Documents folder in the namespace. To access that virtual folder, use SHGetFolderLocation, which returns the ITEMIDLIST for the virtual location, or refer to the technique described in Managing the File System.

<!----> CSIDL_PRINTERS (FOLDERID_PrintersFolder) The virtual folder that contains installed printers.

<!----> CSIDL_PRINTHOOD (FOLDERID_PrintHood) The file system directory that contains the link objects that can exist in the Printers virtual folder. A typical path is C:/Documents and Settings/username/PrintHood.

<!----> CSIDL_PROFILE (FOLDERID_Profile) Version 5.0. The user's profile folder. A typical path is C:/Documents and Settings/username. Applications should not create files or folders at this level; they should put their data under the locations referred to by CSIDL_APPDATA or CSIDL_LOCAL_APPDATA .

<!----> CSIDL_PROGRAM_FILES (FOLDERID_ProgramFiles) Version 5.0. The Program Files folder. A typical path is C:/Program Files.

<!----> CSIDL_PROGRAM_FILESX86 (FOLDERID_ProgramFilesX86) <!---->

<!----> CSIDL_PROGRAM_FILES_COMMON (FOLDERID_ProgramFilesCommon) Version 5.0. A folder for components that are shared across applications. A typical path is C:/Program Files/Common. Valid only for Windows NT, Windows 2000, and Windows XP systems. Not valid for Windows Millennium Edition (Windows Me).

<!----> CSIDL_PROGRAM_FILES_COMMONX86 (FOLDERID_ProgramFilesCommonX86) <!---->

<!----> CSIDL_PROGRAMS (FOLDERID_Programs) The file system directory that contains the user's program groups (which are themselves file system directories). A typical path is C:/Documents and Settings/username/Start Menu/Programs.

<!----> CSIDL_RECENT (FOLDERID_Recent) The file system directory that contains shortcuts to the user's most recently used documents. A typical path is C:/Documents and Settings/username/My Recent Documents. To create a shortcut in this folder, use SHAddToRecentDocs . In addition to creating the shortcut, this function updates the Shell's list of recent documents and adds the shortcut to the My Recent Documents submenu of the Start menu.

<!----> CSIDL_RESOURCES (FOLDERID_ResourceDir) WindowsVista. The file system directory that contains resource data. A typical path is C:/Windows/Resources.

<!----> CSIDL_RESOURCES_LOCALIZED (FOLDERID_LocalizedResourcesDir) <!---->

<!----> CSIDL_SENDTO (FOLDERID_SendTo) The file system directory that contains Send To menu items. A typical path is C:/Documents and Settings/username/SendTo.

<!----> CSIDL_STARTMENU (FOLDERID_StartMenu) The file system directory that contains Start menu items. A typical path is C:/Documents and Settings/username/Start Menu.

<!----> CSIDL_STARTUP (FOLDERID_Startup) The file system directory that corresponds to the user's Startup program group. The system starts these programs whenever any user logs onto Windows NT or starts Windows 95. A typical path is C:/Documents and Settings/username/Start Menu/Programs/Startup.

<!----> CSIDL_SYSTEM (FOLDERID_System) Version 5.0. The Windows System folder. A typical path is C:/Windows/System32.

<!----> CSIDL_SYSTEMX86 (FOLDERID_SystemX86) <!---->

<!----> CSIDL_TEMPLATES (FOLDERID_Templates) The file system directory that serves as a common repository for document templates. A typical path is C:/Documents and Settings/username/Templates.

<!----> CSIDL_WINDOWS (FOLDERID_Windows) Version 5.0. The Windows directory or SYSROOT. This corresponds to the %windir% or %SYSTEMROOT% environment variables. A typical path is C:/Windows.

<!----> Flags <!---->

<!----> CSIDL_FLAG_CREATE (KF_FLAG_CREATE) Version 5.0. Combine with another CSIDL to force the creation of the associated folder if it does not exist.

<!----> CSIDL_FLAG_DONT_UNEXPAND (KF_FLAG_DONT_UNEXPAND) Combine with another CSIDL constant to ensure the expansion of environment variables.

<!----> CSIDL_FLAG_DONT_VERIFY (KF_FLAG_DONT_VERIFY) Combine with another CSIDL constant, except for CSIDL_FLAG_CREATE , to return an unverified folder path with no attempt to create or initialize the folder.

<!----> CSIDL_FLAG_NO_ALIAS (KF_FLAG_NO_ALIAS) Combine with another CSIDL constant to ensure the retrieval of the true system path for the folder, free of any aliased placeholders such as %USERPROFILE%, returned by SHGetFolderLocation. This flag has no effect on paths returned by SHGetFolderPath.

<!----> CSIDL_FLAG_PER_USER_INIT <!---->

<!----> CSIDL_FLAG_MASK A mask for any valid CSIDL flag value.
#include <windows.h> #include <shlobj.h> #include <tchar.h> #include <commctrl.h> #include <strsafe.h> #include <Shlwapi.h> #include <knownfolders.h> #include <ShObjIdl.h> #pragma comment(lib, "Shlwapi.lib") #pragma comment(lib, "comctl32.lib") #pragma comment(lib, "Shell32.lib") // 禁用某些警告 #define _CRT_NONSTDC_NO_DEPRECATE #pragma warning(disable:4996) // 确保使用正确版本的公共控件 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") // 避免宏重定义 #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0601 #endif #ifndef WINVER #define WINVER 0x0601 #endif // 全局状态管理结构 typedef struct { HTREEITEM hItem; BOOL isExpanded; LPITEMIDLIST pidl; BOOL isLibrary; BOOL isSystemFolder; } TreeNodeState; #define MAX_TREE_NODES 1024 TreeNodeState g_treeStates[MAX_TREE_NODES]; int g_nodeCount = 0; // 控件ID定义 #define ID_TREEVIEW 101 #define ID_EDIT_PATH 102 #define ID_BUTTON_COPY 103 // 函数声明 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HTREEITEM ExpandTreeViewItem(HWND hTreeView, HTREEITEM hItem); HRESULT PopulateChildFolders(HWND hTreeView, HTREEITEM hParent, LPITEMIDLIST pidlParent, BOOL isLibraryParent); HTREEITEM AddFolderToTreeView(HWND hTreeView, HTREEITEM hParent, LPWSTR pszName, LPITEMIDLIST pidl, BOOL isLibrary, BOOL isSystemFolder); void CleanupTreeStates(); void DebugPrint(LPCTSTR format, ...); void UpdatePathDisplay(HWND hEdit, LPITEMIDLIST pidl, BOOL isLibrary); void CopyPathToClipboard(HWND hWnd, LPCTSTR path); BOOL IsLibraryFolder(LPITEMIDLIST pidl); BOOL IsSystemFolder(LPITEMIDLIST pidl); BOOL ShouldSkipFolder(LPCTSTR pszName, BOOL isLibraryContext); HRESULT HandleLibraryFolder(HWND hTreeView, HTREEITEM hParent, LPITEMIDLIST pidlLibrary); HRESULT SafeGetDisplayName(IShellFolder* pFolder, LPCITEMIDLIST pidl, LPWSTR pszName, DWORD cchName); HRESULT EnumerateFolderContents(HWND hTreeView, HTREEITEM hParent, LPITEMIDLIST pidlFolder, BOOL isLibraryContext); HRESULT AddKnownLibraryFolders(HWND hTreeView, HTREEITEM hParent); HRESULT EnumerateLibraryContents(HWND hTreeView, HTREEITEM hParent, LPITEMIDLIST pidlLibrary); // 主窗口类名 #define CLASS_NAME _T("TreeViewDesktopBrowser") // 函数实现 void DebugPrint(LPCTSTR format, ...) { TCHAR buffer[1024]; va_list args; va_start(args, format); StringCchVPrintf(buffer, _countof(buffer), format, args); va_end(args); OutputDebugString(buffer); } void CleanupTreeStates() { DebugPrint(L"清理状态\n"); for (int i = 0; i < g_nodeCount; i++) { if (g_treeStates[i].pidl) { CoTaskMemFree(g_treeStates[i].pidl); g_treeStates[i].pidl = NULL; } } g_nodeCount = 0; } HTREEITEM AddFolderToTreeView(HWND hTreeView, HTREEITEM hParent, LPWSTR pszName, LPITEMIDLIST pidl, BOOL isLibrary, BOOL isSystemFolder) { if (!hTreeView || !pszName || !pidl) return NULL; TVINSERTSTRUCT tvInsert = { 0 }; tvInsert.hParent = hParent; tvInsert.hInsertAfter = TVI_LAST; tvInsert.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_CHILDREN; tvInsert.item.pszText = pszName; tvInsert.item.lParam = (LPARAM)pidl; tvInsert.item.cChildren = 1; HTREEITEM hItem = TreeView_InsertItem(hTreeView, &tvInsert); if (hItem) { if (g_nodeCount < MAX_TREE_NODES) { g_treeStates[g_nodeCount].hItem = hItem; g_treeStates[g_nodeCount].isExpanded = FALSE; g_treeStates[g_nodeCount].pidl = pidl; g_treeStates[g_nodeCount].isLibrary = isLibrary; g_treeStates[g_nodeCount].isSystemFolder = isSystemFolder; g_nodeCount++; } } else { DebugPrint(L"添加文件夹失败: %s\n", pszName); CoTaskMemFree(pidl); } return hItem; } HTREEITEM ExpandTreeViewItem(HWND hTreeView, HTREEITEM hItem) { if (!hTreeView || !hItem) return NULL; TVITEM tvItem = { 0 }; tvItem.mask = TVIF_HANDLE | TVIF_PARAM | TVIF_STATE; tvItem.hItem = hItem; tvItem.stateMask = TVIS_EXPANDED; if (!TreeView_GetItem(hTreeView, &tvItem)) { return NULL; } LPITEMIDLIST pidlParent = (LPITEMIDLIST)tvItem.lParam; if (!pidlParent) return NULL; BOOL isCurrentlyExpanded = (tvItem.state & TVIS_EXPANDED) != 0; if (!isCurrentlyExpanded) { BOOL isLibraryParent = FALSE; for (int i = 0; i < g_nodeCount; i++) { if (g_treeStates[i].hItem == hItem) { isLibraryParent = g_treeStates[i].isLibrary; break; } } HTREEITEM hFirstChild = TreeView_GetChild(hTreeView, hItem); if (!hFirstChild) { HRESULT hr = PopulateChildFolders(hTreeView, hItem, pidlParent, isLibraryParent); if (FAILED(hr)) { DebugPrint(L"展开失败: 0x%08X\n", hr); return NULL; } } TreeView_Expand(hTreeView, hItem, TVE_EXPAND); for (int i = 0; i < g_nodeCount; i++) { if (g_treeStates[i].hItem == hItem) { g_treeStates[i].isExpanded = TRUE; break; } } } else { TreeView_Expand(hTreeView, hItem, TVE_COLLAPSE); for (int i = 0; i < g_nodeCount; i++) { if (g_treeStates[i].hItem == hItem) { g_treeStates[i].isExpanded = FALSE; break; } } } return hItem; } HRESULT PopulateChildFolders(HWND hTreeView, HTREEITEM hParent, LPITEMIDLIST pidlParent, BOOL isLibraryParent) { DebugPrint(L"填充子文件夹,父节点是库: %d\n", isLibraryParent); if (IsLibraryFolder(pidlParent)) { return HandleLibraryFolder(hTreeView, hParent, pidlParent); } return EnumerateFolderContents(hTreeView, hParent, pidlParent, isLibraryParent); } HRESULT SafeGetDisplayName(IShellFolder* pFolder, LPCITEMIDLIST pidl, LPWSTR pszName, DWORD cchName) { if (!pFolder || !pidl || !pszName || cchName == 0) { return E_INVALIDARG; } STRRET strRet; HRESULT hr = pFolder->GetDisplayNameOf(pidl, SHGDN_NORMAL, &strRet); if (SUCCEEDED(hr)) { hr = StrRetToBuf(&strRet, pidl, pszName, cchName); if (strRet.uType == STRRET_WSTR && strRet.pOleStr) { CoTaskMemFree(strRet.pOleStr); } } return hr; } BOOL IsLibraryFolder(LPITEMIDLIST pidl) { if (!pidl) return FALSE; LPITEMIDLIST pidlLibraries = NULL; if (SUCCEEDED(SHGetKnownFolderIDList(FOLDERID_Libraries, 0, NULL, &pidlLibraries))) { BOOL isEqual = ILIsEqual(pidl, pidlLibraries); CoTaskMemFree(pidlLibraries); if (isEqual) return TRUE; } IShellFolder* pDesktop = NULL; if (SUCCEEDED(SHGetDesktopFolder(&pDesktop))) { WCHAR szName[MAX_PATH]; if (SUCCEEDED(SafeGetDisplayName(pDesktop, pidl, szName, _countof(szName)))) { if (_wcsicmp(szName, L"库") == 0 || _wcsicmp(szName, L"Libraries") == 0 || wcsstr(szName, L"库") != NULL) { // 包含"库"字样的也可能是库 pDesktop->Release(); return TRUE; } } pDesktop->Release(); } return FALSE; } BOOL IsSystemFolder(LPITEMIDLIST pidl) { if (!pidl) return FALSE; // 通过路径判断系统文件夹 WCHAR szPath[MAX_PATH]; if (SHGetPathFromIDList(pidl, szPath)) { // 常见的系统文件夹路径模式 if (wcsstr(szPath, L"\\Windows\\") != NULL || wcsstr(szPath, L"\\Program Files\\") != NULL || wcsstr(szPath, L"\\ProgramData\\") != NULL || (wcsstr(szPath, L"\\Users\\") != NULL && wcsstr(szPath, L"\\AppData\\") != NULL)) { return TRUE; } } return FALSE; } void CopyPathToClipboard(HWND hWnd, LPCTSTR path) { if (OpenClipboard(hWnd)) { EmptyClipboard(); SIZE_T cbSize = (_tcslen(path) + 1) * sizeof(TCHAR); HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cbSize); if (hGlobal) { LPTSTR pszData = (LPTSTR)GlobalLock(hGlobal); if (pszData) { _tcscpy_s(pszData, _tcslen(path) + 1, path); GlobalUnlock(hGlobal); #ifdef UNICODE SetClipboardData(CF_UNICODETEXT, hGlobal); #else SetClipboardData(CF_TEXT, hGlobal); #endif } else { GlobalFree(hGlobal); } } CloseClipboard(); MessageBox(hWnd, _T("路径已复制到剪贴板"), _T("成功"), MB_ICONINFORMATION); } else { MessageBox(hWnd, _T("无法打开剪贴板"), _T("错误"), MB_ICONERROR); } } HRESULT HandleLibraryFolder(HWND hTreeView, HTREEITEM hParent, LPITEMIDLIST pidlLibrary) { DebugPrint(L"处理库文件夹\n"); return EnumerateLibraryContents(hTreeView, hParent, pidlLibrary); } // 修复的 AddKnownLibraryFolders 函数,避免在已知库中重复添加 HRESULT AddKnownLibraryFolders(HWND hTreeView, HTREEITEM hParent) { DebugPrint(L"添加已知库文件夹\n"); // 检查父节点是否是已知库文件夹本身,如果是则跳过添加 TVITEM tvParent = { 0 }; tvParent.mask = TVIF_HANDLE | TVIF_TEXT; tvParent.hItem = hParent; WCHAR szParentName[MAX_PATH] = L""; tvParent.pszText = szParentName; tvParent.cchTextMax = _countof(szParentName); if (TreeView_GetItem(hTreeView, &tvParent)) { // 检查父节点名称是否是已知库文件夹 static const LPCWSTR knownLibraryNames[] = { L"文档", L"图片", L"音乐", L"视频", L"Documents", L"Pictures", L"Music", L"Videos", NULL }; BOOL isParentKnownLibrary = FALSE; for (int i = 0; knownLibraryNames[i] != NULL; i++) { if (wcscmp(szParentName, knownLibraryNames[i]) == 0) { isParentKnownLibrary = TRUE; break; } } if (isParentKnownLibrary) { DebugPrint(L"父节点是已知库文件夹 %s,跳过添加已知库文件夹\n", szParentName); return S_FALSE; // 跳过添加 } } struct { KNOWNFOLDERID folderId; LPCWSTR name; } libraries[] = { { FOLDERID_Documents, L"文档" }, { FOLDERID_Pictures, L"图片" }, { FOLDERID_Music, L"音乐" }, { FOLDERID_Videos, L"视频" }, }; int addedCount = 0; for (int i = 0; i < ARRAYSIZE(libraries); i++) { LPITEMIDLIST pidl = NULL; HRESULT hr = SHGetKnownFolderIDList(libraries[i].folderId, 0, NULL, &pidl); if (SUCCEEDED(hr) && pidl) { UINT pidlSize = ILGetSize(pidl); if (pidlSize > 0) { LPITEMIDLIST pidlPersistent = (LPITEMIDLIST)CoTaskMemAlloc(pidlSize); if (pidlPersistent) { memcpy(pidlPersistent, pidl, pidlSize); BOOL isLibrary = IsLibraryFolder(pidlPersistent); BOOL isSystem = IsSystemFolder(pidlPersistent); HTREEITEM hNewItem = AddFolderToTreeView(hTreeView, hParent, (LPWSTR)libraries[i].name, pidlPersistent, isLibrary, isSystem); if (hNewItem) { addedCount++; DebugPrint(L"添加已知库文件夹: %s\n", libraries[i].name); } else { CoTaskMemFree(pidlPersistent); } } } CoTaskMemFree(pidl); } } DebugPrint(L"成功添加 %d 个已知库文件夹\n", addedCount); return addedCount > 0 ? S_OK : E_FAIL; } BOOL ShouldSkipFolder(LPCTSTR pszName, BOOL isLibraryContext) { if (!pszName) return FALSE; if (pszName[0] == L'\0' || wcscmp(pszName, L".") == 0 || wcscmp(pszName, L"..") == 0) { return TRUE; } static const LPCWSTR skipFolders[] = { L"Application Data", L"Cookies", L"Local Settings", L"NetHood", L"PrintHood", L"Recent", L"SendTo", L"Templates", L"$Recycle.Bin", L"System32", L"SysWOW64", NULL }; for (int i = 0; skipFolders[i] != NULL; i++) { if (_wcsicmp(pszName, skipFolders[i]) == 0) { return TRUE; } } return FALSE; } HRESULT EnumerateFolderContents(HWND hTreeView, HTREEITEM hParent, LPITEMIDLIST pidlFolder, BOOL isLibraryContext) { DebugPrint(L"枚举文件夹内容\n"); IShellFolder* pFolder = NULL; HRESULT hr = SHBindToObject(NULL, pidlFolder, NULL, IID_IShellFolder, (void**)&pFolder); if (FAILED(hr)) { DebugPrint(L"绑定文件夹失败: 0x%08X\n", hr); return hr; } IEnumIDList* pEnum = NULL; hr = pFolder->EnumObjects(NULL, SHCONTF_FOLDERS, &pEnum); if (FAILED(hr) || !pEnum) { DebugPrint(L"枚举失败: 0x%08X\n", hr); pFolder->Release(); return FAILED(hr) ? hr : E_FAIL; } int itemCount = 0; LPITEMIDLIST pidlChild = NULL; ULONG cFetched = 0; while (SUCCEEDED(pEnum->Next(1, &pidlChild, &cFetched)) && cFetched > 0) { WCHAR szName[MAX_PATH] = L""; hr = SafeGetDisplayName(pFolder, pidlChild, szName, _countof(szName)); if (SUCCEEDED(hr)) { if (ShouldSkipFolder(szName, isLibraryContext)) { CoTaskMemFree(pidlChild); continue; } LPITEMIDLIST pidlCombined = ILCombine(pidlFolder, pidlChild); if (pidlCombined) { BOOL isChildLibrary = IsLibraryFolder(pidlCombined); BOOL isSystemFolder = IsSystemFolder(pidlCombined); UINT pidlSize = ILGetSize(pidlCombined); if (pidlSize > 0) { LPITEMIDLIST pidlPersistent = (LPITEMIDLIST)CoTaskMemAlloc(pidlSize); if (pidlPersistent) { memcpy(pidlPersistent, pidlCombined, pidlSize); HTREEITEM hNewItem = AddFolderToTreeView(hTreeView, hParent, szName, pidlPersistent, isChildLibrary, isSystemFolder); if (hNewItem) { itemCount++; DebugPrint(L"添加文件夹: %s (库: %d, 系统: %d)\n", szName, isChildLibrary, isSystemFolder); } else { CoTaskMemFree(pidlPersistent); } } } CoTaskMemFree(pidlCombined); } } CoTaskMemFree(pidlChild); } pEnum->Release(); pFolder->Release(); DebugPrint(L"枚举完成,添加 %d 个项目\n", itemCount); return S_OK; } // 修复的 EnumerateLibraryContents 函数 - 现在会正确枚举库内容,避免重复添加已知库 HRESULT EnumerateLibraryContents(HWND hTreeView, HTREEITEM hParent, LPITEMIDLIST pidlLibrary) { DebugPrint(L"枚举库内容 - 开始\n"); int totalAdded = 0; // 1. 首先添加已知的库文件夹 HRESULT hr = AddKnownLibraryFolders(hTreeView, hParent); if (SUCCEEDED(hr)) { DebugPrint(L"已知库文件夹添加完成\n"); totalAdded += 4; // 文档、图片、音乐、视频四个已知库 } // 2. 然后枚举库中的其他文件夹(不包括已知的库文件夹) IShellFolder* pFolder = NULL; hr = SHBindToObject(NULL, pidlLibrary, NULL, IID_IShellFolder, (void**)&pFolder); if (FAILED(hr)) { DebugPrint(L"绑定库文件夹失败: 0x%08X\n", hr); return hr; } IEnumIDList* pEnum = NULL; hr = pFolder->EnumObjects(NULL, SHCONTF_FOLDERS, &pEnum); if (FAILED(hr) || !pEnum) { DebugPrint(L"枚举库内容失败: 0x%08X\n", hr); pFolder->Release(); return FAILED(hr) ? hr : E_FAIL; } int enumeratedCount = 0; LPITEMIDLIST pidlChild = NULL; ULONG cFetched = 0; // 定义已知库文件夹的名称列表(多种语言) static const LPCWSTR knownLibraryNames[] = { L"文档", L"图片", L"音乐", L"视频", L"Documents", L"Pictures", L"Music", L"Videos", NULL }; while (SUCCEEDED(pEnum->Next(1, &pidlChild, &cFetched)) && cFetched > 0) { WCHAR szName[MAX_PATH] = L""; hr = SafeGetDisplayName(pFolder, pidlChild, szName, _countof(szName)); if (SUCCEEDED(hr)) { // 检查是否为已知库文件夹 BOOL isKnownLibrary = FALSE; for (int i = 0; knownLibraryNames[i] != NULL; i++) { if (wcscmp(szName, knownLibraryNames[i]) == 0) { isKnownLibrary = TRUE; break; } } // 跳过已知的库文件夹(已经通过AddKnownLibraryFolders添加) if (isKnownLibrary) { DebugPrint(L"跳过已知库文件夹: %s\n", szName); CoTaskMemFree(pidlChild); continue; } if (ShouldSkipFolder(szName, TRUE)) { CoTaskMemFree(pidlChild); continue; } LPITEMIDLIST pidlCombined = ILCombine(pidlLibrary, pidlChild); if (pidlCombined) { BOOL isChildLibrary = IsLibraryFolder(pidlCombined); BOOL isSystemFolder = IsSystemFolder(pidlCombined); UINT pidlSize = ILGetSize(pidlCombined); if (pidlSize > 0) { LPITEMIDLIST pidlPersistent = (LPITEMIDLIST)CoTaskMemAlloc(pidlSize); if (pidlPersistent) { memcpy(pidlPersistent, pidlCombined, pidlSize); HTREEITEM hNewItem = AddFolderToTreeView(hTreeView, hParent, szName, pidlPersistent, isChildLibrary, isSystemFolder); if (hNewItem) { enumeratedCount++; totalAdded++; DebugPrint(L"添加库中项目: %s (库: %d, 系统: %d)\n", szName, isChildLibrary, isSystemFolder); } else { CoTaskMemFree(pidlPersistent); } } } CoTaskMemFree(pidlCombined); } } CoTaskMemFree(pidlChild); } pEnum->Release(); pFolder->Release(); DebugPrint(L"库枚举完成,总共添加 %d 个项目(已知: %d, 枚举: %d)\n", totalAdded, totalAdded - enumeratedCount, enumeratedCount); return S_OK; } void UpdatePathDisplay(HWND hEdit, LPITEMIDLIST pidl, BOOL isLibrary) { if (!hEdit || !pidl) return; WCHAR szDisplay[MAX_PATH * 2] = L""; if (isLibrary) { IShellFolder* pDesktop = NULL; if (SUCCEEDED(SHGetDesktopFolder(&pDesktop))) { WCHAR szDisplayName[MAX_PATH]; if (SUCCEEDED(SafeGetDisplayName(pDesktop, pidl, szDisplayName, _countof(szDisplayName)))) { WCHAR szPath[MAX_PATH]; if (SHGetPathFromIDList(pidl, szPath)) { StringCchPrintf(szDisplay, _countof(szDisplay), L"库: %s\n路径: %s", szDisplayName, szPath); } else { StringCchPrintf(szDisplay, _countof(szDisplay), L"库: %s", szDisplayName); } } pDesktop->Release(); } } else { WCHAR szPath[MAX_PATH]; if (SHGetPathFromIDList(pidl, szPath)) { StringCchCopy(szDisplay, _countof(szDisplay), szPath); } else { IShellFolder* pDesktop = NULL; if (SUCCEEDED(SHGetDesktopFolder(&pDesktop))) { WCHAR szDisplayName[MAX_PATH]; if (SUCCEEDED(SafeGetDisplayName(pDesktop, pidl, szDisplayName, _countof(szDisplayName)))) { StringCchPrintf(szDisplay, _countof(szDisplay), L"虚拟文件夹: %s", szDisplayName); } pDesktop->Release(); } } } SetWindowText(hEdit, szDisplay); } // WndProc 和 WinMain 函数保持不变 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND hTreeView = NULL; static HWND hEditPath = NULL; static HWND hButtonCopy = NULL; static HTREEITEM hDesktopItem = NULL; switch (message) { case WM_CREATE: { // 创建控件 CreateWindow(L"STATIC", L"所选路径:", WS_CHILD | WS_VISIBLE | SS_LEFT, 10, 10, 100, 20, hWnd, NULL, ((LPCREATESTRUCT)lParam)->hInstance, NULL); hEditPath = CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY, 120, 10, 600, 25, hWnd, (HMENU)ID_EDIT_PATH, ((LPCREATESTRUCT)lParam)->hInstance, NULL); hButtonCopy = CreateWindow(L"BUTTON", L"复制路径", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 730, 10, 100, 25, hWnd, (HMENU)ID_BUTTON_COPY, ((LPCREATESTRUCT)lParam)->hInstance, NULL); hTreeView = CreateWindow(WC_TREEVIEW, L"", WS_CHILD | WS_VISIBLE | WS_BORDER | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT | TVS_SHOWSELALWAYS | TVS_TRACKSELECT, 10, 50, 860, 580, hWnd, (HMENU)ID_TREEVIEW, ((LPCREATESTRUCT)lParam)->hInstance, NULL); if (!hTreeView) { MessageBox(hWnd, L"TreeView 创建失败!", L"错误", MB_ICONERROR); return -1; } HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT); if (hFont) { SendMessage(hEditPath, WM_SETFONT, (WPARAM)hFont, TRUE); SendMessage(hButtonCopy, WM_SETFONT, (WPARAM)hFont, TRUE); SendMessage(hTreeView, WM_SETFONT, (WPARAM)hFont, TRUE); } // 初始化桌面根节点 IShellFolder* pDesktopFolder = NULL; HRESULT hr = SHGetDesktopFolder(&pDesktopFolder); if (SUCCEEDED(hr)) { LPITEMIDLIST pidlDesktop = NULL; hr = SHGetSpecialFolderLocation(hWnd, CSIDL_DESKTOP, &pidlDesktop); if (SUCCEEDED(hr)) { WCHAR szName[MAX_PATH]; hr = SafeGetDisplayName(pDesktopFolder, pidlDesktop, szName, _countof(szName)); if (SUCCEEDED(hr)) { UINT pidlSize = ILGetSize(pidlDesktop); if (pidlSize > 0) { LPITEMIDLIST pidlPersistent = (LPITEMIDLIST)CoTaskMemAlloc(pidlSize); if (pidlPersistent) { memcpy(pidlPersistent, pidlDesktop, pidlSize); TVINSERTSTRUCT tvInsert = { 0 }; tvInsert.hParent = TVI_ROOT; tvInsert.hInsertAfter = TVI_LAST; tvInsert.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_CHILDREN; tvInsert.item.pszText = szName; tvInsert.item.lParam = (LPARAM)pidlPersistent; tvInsert.item.cChildren = 1; hDesktopItem = TreeView_InsertItem(hTreeView, &tvInsert); if (hDesktopItem) { DebugPrint(L"添加桌面节点: %s\n", szName); TreeView_Expand(hTreeView, hDesktopItem, TVE_EXPAND); TreeView_SelectItem(hTreeView, hDesktopItem); UpdatePathDisplay(hEditPath, pidlDesktop, FALSE); } else { CoTaskMemFree(pidlPersistent); } } } } CoTaskMemFree(pidlDesktop); } pDesktopFolder->Release(); } break; } case WM_COMMAND: { int wmId = LOWORD(wParam); if (wmId == ID_BUTTON_COPY) { WCHAR szPath[MAX_PATH * 2]; GetWindowText(hEditPath, szPath, _countof(szPath)); if (wcslen(szPath) > 0) { CopyPathToClipboard(hWnd, szPath); } } break; } case WM_NOTIFY: { LPNMHDR pnmh = (LPNMHDR)lParam; if (pnmh->idFrom == ID_TREEVIEW) { switch (pnmh->code) { case TVN_SELCHANGED: { LPNMTREEVIEW pnmtv = (LPNMTREEVIEW)lParam; TVITEM tvItem = { 0 }; tvItem.mask = TVIF_HANDLE | TVIF_PARAM; tvItem.hItem = pnmtv->itemNew.hItem; if (TreeView_GetItem(hTreeView, &tvItem)) { LPITEMIDLIST pidl = (LPITEMIDLIST)tvItem.lParam; if (pidl) { BOOL isLibrary = FALSE; for (int i = 0; i < g_nodeCount; i++) { if (g_treeStates[i].hItem == pnmtv->itemNew.hItem) { isLibrary = g_treeStates[i].isLibrary; break; } } UpdatePathDisplay(hEditPath, pidl, isLibrary); } } break; } case TVN_ITEMEXPANDING: { LPNMTREEVIEW pnmtv = (LPNMTREEVIEW)lParam; if (pnmtv->action == TVE_EXPAND) { TVITEM tvItem = { 0 }; tvItem.mask = TVIF_HANDLE | TVIF_PARAM; tvItem.hItem = pnmtv->itemNew.hItem; if (TreeView_GetItem(hTreeView, &tvItem)) { LPITEMIDLIST pidl = (LPITEMIDLIST)tvItem.lParam; if (pidl) { BOOL isLibraryParent = FALSE; for (int i = 0; i < g_nodeCount; i++) { if (g_treeStates[i].hItem == pnmtv->itemNew.hItem) { isLibraryParent = g_treeStates[i].isLibrary; break; } } HTREEITEM hFirstChild = TreeView_GetChild(hTreeView, pnmtv->itemNew.hItem); if (!hFirstChild) { HRESULT hr = PopulateChildFolders(hTreeView, pnmtv->itemNew.hItem, pidl, isLibraryParent); if (FAILED(hr)) { DebugPrint(L"填充子文件夹失败: 0x%08X\n", hr); } } } } } break; } } } break; } case WM_SIZE: { if (hTreeView && hEditPath && hButtonCopy) { RECT rcClient; GetClientRect(hWnd, &rcClient); MoveWindow(hEditPath, 120, 10, rcClient.right - 240, 25, TRUE); MoveWindow(hButtonCopy, rcClient.right - 110, 10, 100, 25, TRUE); MoveWindow(hTreeView, 10, 50, rcClient.right - 20, rcClient.bottom - 60, TRUE); } break; } case WM_DESTROY: CleanupTreeStates(); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HRESULT hr = CoInitialize(NULL); if (FAILED(hr)) { MessageBox(NULL, _T("COM初始化失败!"), _T("错误"), MB_ICONERROR); return 1; } INITCOMMONCONTROLSEX icex; ZeroMemory(&icex, sizeof(icex)); icex.dwSize = sizeof(INITCOMMONCONTROLSEX); icex.dwICC = ICC_TREEVIEW_CLASSES | ICC_STANDARD_CLASSES | ICC_WIN95_CLASSES; if (!InitCommonControlsEx(&icex)) { InitCommonControls(); } WNDCLASSEX wcex; ZeroMemory(&wcex, sizeof(wcex)); wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.hInstance = hInstance; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszClassName = CLASS_NAME; if (!RegisterClassEx(&wcex)) { MessageBox(NULL, _T("窗口注册失败!"), _T("错误"), MB_ICONERROR); CoUninitialize(); return 1; } HWND hWnd = CreateWindow( CLASS_NAME, _T("桌面浏览器 - 完整库支持版"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 900, 700, NULL, NULL, hInstance, NULL); if (!hWnd) { MessageBox(NULL, _T("窗口创建失败!"), _T("错误"), MB_ICONERROR); CoUninitialize(); return 1; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } CleanupTreeStates(); CoUninitialize(); return (int)msg.wParam; }
最新发布
09-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值