179上位机VC MFC获取桌面系统缓存等全部特殊目录
179上位机VC MFC获取桌面系统缓存等全部特殊目录 功能展示 在开发上位机程序时,时常得获取系统特殊目录,如桌面目录,SYSTEM32目录,字体目录,系统盘程序安装目录,文档目录等等,我们当前例程实现系统支持的全部特殊目录的获取,效果如图; 要点提示 系统特殊目录的获取可以用函数SHGetSpecialFolderLocation()来实现;原型为WINSHELLAPI HRESULT WINAPISHGetSpecialFolderLocation( HWND hwndOwner, int nFolder, LPITEMIDLIST *ppidl );hwndOwner父窗口句柄; nFolder获取何种目录,取值在后面罗列出来, ppidl 为LTEMIDLIST结构指针,此结构可以通过函数SHGetPathFromIDList()获取具体的目录;如我们例程SHGetPathFromIDList(pidl, szDir);szDir就为获得的目录; nFolder参数取值 CSIDL_DESKTOP 桌面目录0X00 CSIDL_INTERNET 浏览器目录0X01 CSIDL_PROGRAMS 程序菜单目录0X02 CSIDL_CONTROLS 控制面板目录0X03 CSIDL_PRINTERS 打印机目录0X04 CSIDL_PERSONAL 我的文档0X05 CSIDL_FAVORITES收藏目录0X06 CSIDL_STARTUP 启动目录0X07 CSIDL_RECENT 最近打开目录0X08 CSIDL_SENDTO 发送到目录0X09 CSIDL_BITBUCKET 回收站目录0X0A CSIDL_STARTMENU 快启菜单目录0X0B CSIDL_DESKTOPDIRECTORY 桌面目录0X10 CSIDL_DRIVES 我的电脑目录0X11 CSIDL_NETWORK 网上邻居目录0X12 CSIDL_NETHOOD 网络连接目录0X13 CSIDL_FONTS 字体目录0X14 CSIDL_TEMPLATES 文档模块目录0X15 CSIDL_COMMON_STARTMENU 所有用户开始菜单目录0X16 CSIDL_COMMON_PROGRAMS 所有用户程序目录 0X17 CSIDL_COMMON_STARTUP 所有用户启动目录 0X18 CSIDL_COMMON_DESKTOPDIRECTORY 所有用户桌面目录 0X19 CSIDL_APPDATA 程序数据目录 0X1A CSIDL_PRINTHOOD 网络打印机目录 0X1B CSIDL_ALTSTARTUP 不清楚的目录 0X1D CSIDL_COMMON_ALTSTARTUP 不清楚的目录 0X1E CSIDL_INTERNET_CACHE 浏览器CACHE目录 0X20 CSIDL_COOKIES 浏览器KOOKIES目录 0X21 CSIDL_HISTORY 浏览器历史记录 0X22 CSIDL_MYDOCUMENTS 文档目录 0X05 CSIDL_MYMUSIC 音乐目录 0X0D CSIDL_MYPICTURES 图像目录 0X27 CSIDL_PROGRAM_FILES 安装程序目录 0X26 CSIDL_SYSTEM SYSTEM32目录 0X25 CSIDL_WINDOWS WINDOWS目录 0X24 CSIDL_PROFILE 配置文件目录 0X28 CSIDL_SHELLNEW 新建文件目录21 CSIDL_ADMINTOOLS 管理工具目录 30 CSIDL_LOCAL_APPDATA 当前用户程序数据目录1C CSIDL_COMMON_ADMINTOOLS 所有用户管理工具目录 2F CSIDL_COMMON_APPDATA 所有用户程序数据目录 23 CSIDL_COMMON_DOCUMENTS 所有用户文档目录 2E CSIDL_COMMON_FAVORITES 所有用户收藏目录 1F CSIDL_COMMON_TEMPLATES 所有用户模块目录 2D CSIDL_PROGRAM_FILES_COMMON 所有用户安装程序目录2B CSIDL_FLAG_CREATE 8000 CSIDL_FLAG_DONT_VERIFY 4000 实现功能 1.新建基于对话框的应用程序 2.拖拽一组合框ID为IDC_COMBO1用于显示选择要获得何种目录 拖拽一编辑框ID为IDC_EDIT1用于显示已获得目录; 拖拽一按钮控件,关联函数,用于执行获得目录 3.由于VC6.0部分特殊目录宏未定义,所以我们在主对话框头文件自已定义宏 #define CSIDL_COMMON_APPDATA 0x0023 // All Users\Application Data #define CSIDL_WINDOWS 0x0024 // GetWindowsDirectory() #define CSIDL_SYSTEM 0x0025 // GetSystemDirectory() #define CSIDL_PROGRAM_FILES 0x0026 // C:\Program Files #define CSIDL_MYPICTURES 0x0027 // C:\Program Files\My Pictures #define CSIDL_PROFILE 0x0028 // USERPROFILE #define CSIDL_SYSTEMX86 0x0029 // x86 system directory on RISC #define CSIDL_PROGRAM_FILESX86 0x002a // x86 C:\Program Files on RISC #define CSIDL_PROGRAM_FILES_COMMON 0x002b // C:\Program Files\Common #define CSIDL_PROGRAM_FILES_COMMONX86 0x002c // x86 Program Files\Common onRISC #define CSIDL_COMMON_TEMPLATES 0x002d // All Users\Templates #define CSIDL_COMMON_DOCUMENTS 0x002e // All Users\Documents #define CSIDL_COMMON_ADMINTOOLS 0x002f // All Users\StartMenu\Programs\Administrative Tools #define CSIDL_ADMINTOOLS 0x0030 // <user name>\StartMenu\Programs\Administrative Tools #define CSIDL_CONNECTIONS 0x0031 // Network and Dial-upConnections #define CSIDL_COMMON_MUSIC 0x0035 // All Users\My Music #define CSIDL_COMMON_PICTURES 0x0036 // All Users\My Pictures #define CSIDL_COMMON_VIDEO 0x0037 // All Users\My Video #define CSIDL_RESOURCES 0x0038 // Resource Direcotry #define CSIDL_RESOURCES_LOCALIZED 0x0039 // Localized Resource Direcotry #define CSIDL_COMMON_OEM_LINKS 0x003a // Links to All Users OEMspecific apps 更多的宏定义可以下载程序复制使用 4.添加自定义函数 void InitCom(); void SaveData();用于向组合框添加文本及保存数据到INI文件中;再实现按钮的函数功能OnGetpath(); 下面是三个函数的函数体部分 - void CGkbc8Dlg::InitCom()
- {
- CComboBox* pCombox = (CComboBox*)GetDlgItem(IDC_COMBO1);
- pCombox->InsertString(0,"桌面目录0X00");
- pCombox->InsertString(0,"浏览器目录0X01");
- pCombox->InsertString(0,"程序菜单目录0X02");
- pCombox->InsertString(0,"控制面板目录0X03");
- pCombox->InsertString(0,"打印机目录0X04");
- pCombox->InsertString(0,"我的文档0X05");
- pCombox->InsertString(0,"收藏目录0X06");
- pCombox->InsertString(0,"启动目录0X07");
- pCombox->InsertString(0,"最近打开目录0X08");
- pCombox->InsertString(0,"发送到目录0X09");
- pCombox->InsertString(0,"回收站目录0X0A");
- pCombox->InsertString(0,"快启菜单目录0X0B");
- pCombox->InsertString(0,"桌面目录0X10");
- pCombox->InsertString(0,"我的电脑目录0X11");
- pCombox->InsertString(0,"网上邻居目录0X12");
- pCombox->InsertString(0,"网络连接目录0X13");
- pCombox->InsertString(0,"字体目录0X14");
- pCombox->InsertString(0,"文档模块目录0X15");
- pCombox->InsertString(0,"所有用户开始菜单目录0X16");
- pCombox->InsertString(0,"所有用户程序目录0X17");
- pCombox->InsertString(0,"所有用户启动目录0X18");
- pCombox->InsertString(0,"所有用户桌面目录0X19");
- pCombox->InsertString(0,"程序数据目录0X1A");
- pCombox->InsertString(0,"网络打印机目录0X1B");
- pCombox->InsertString(0,"不清楚的目录0X1D");
- pCombox->InsertString(0,"不清楚的目录0X1E");
-
复制代码- pCombox->InsertString(0,"浏览器CACHE目录0X20");
- pCombox->InsertString(0,"浏览器KOOKIES目录0X21");
- pCombox->InsertString(0,"浏览器历史记录0X22");
- pCombox->InsertString(0,"文档目录0X05");
- pCombox->InsertString(0,"音乐目录0X0D");
- pCombox->InsertString(0,"图像目录0X27");
- pCombox->InsertString(0,"安装程序目录0X26");
- pCombox->InsertString(0,"SYSTEM32目录0X25");
- pCombox->InsertString(0,"WINDOWS目录0X24");
- pCombox->InsertString(0,"配置文件目录0X28");
- pCombox->SetCurSel(0);
- }
- void CGkbc8Dlg::OnGetpath()
- {
- char buf[MAX_PATH];
- ZeroMemory(buf,MAX_PATH);
- GetModuleFileName(NULL,buf,MAX_PATH);
- CString sPath(buf);
- sPath=sPath.Left(sPath.ReverseFind('\\')+1);
- sPath+="工控编程吧.ini";
- //
- CComboBox* pCombox = (CComboBox*)GetDlgItem(IDC_COMBO1);
- CString sKey;
- pCombox->GetWindowText(sKey);
- int nValue=::GetPrivateProfileInt("DIRECTORY",sKey,0X00,sPath);
- //
- LPITEMIDLIST pidl;
- LPMALLOC pShellMalloc;
- char szDir[MAX_PATH];
-
- if(SUCCEEDED(SHGetMalloc(&pShellMalloc)))
- {
- if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,nValue,&pidl)))
- {
- SHGetPathFromIDList(pidl, szDir);// 如果成功返回true
- pShellMalloc->Free(pidl);
- SetDlgItemText(IDC_EDIT1,szDir);
- }
- pShellMalloc->Release();
- }
- }
复制代码- void CGkbc8Dlg::SaveData()
- {
- char buf[MAX_PATH];
- ZeroMemory(buf,MAX_PATH);
- GetModuleFileName(NULL,buf,MAX_PATH);
- CString sPath(buf);
- sPath=sPath.Left(sPath.ReverseFind('\\')+1);
- sPath+="工控编程吧.ini";
- //
- CString sData;
- sData.Format("%ld",CSIDL_DESKTOP);
- ::WritePrivateProfileString("DIRECTORY","桌面目录0X00",sData,sPath);
- sData.Format("%ld",CSIDL_INTERNET);
- ::WritePrivateProfileString("DIRECTORY","浏览器目录0X01",sData,sPath);
-
- sData.Format("%ld",CSIDL_PROGRAMS);
- ::WritePrivateProfileString("DIRECTORY","程序菜单目录0X02",sData,sPath);
- sData.Format("%ld",CSIDL_CONTROLS);
- ::WritePrivateProfileString("DIRECTORY","控制面板目录0X03",sData,sPath);
- sData.Format("%ld",CSIDL_PRINTERS);
- ::WritePrivateProfileString("DIRECTORY","打印机目录0X04",sData,sPath);
-
- sData.Format("%ld",CSIDL_PERSONAL);
- ::WritePrivateProfileString("DIRECTORY","我的文档0X05",sData,sPath);
- sData.Format("%ld",CSIDL_FAVORITES);
- ::WritePrivateProfileString("DIRECTORY","收藏目录0X06",sData,sPath);
- sData.Format("%ld",CSIDL_STARTUP);
- ::WritePrivateProfileString("DIRECTORY","启动目录0X07",sData,sPath);
- sData.Format("%ld",CSIDL_RECENT);
- ::WritePrivateProfileString("DIRECTORY","最近打开目录0X08",sData,sPath);
- sData.Format("%ld",CSIDL_SENDTO);
- ::WritePrivateProfileString("DIRECTORY","发送到目录0X09",sData,sPath);
- sData.Format("%ld",CSIDL_BITBUCKET);
- ::WritePrivateProfileString("DIRECTORY","回收站目录0X0A",sData,sPath);
- sData.Format("%ld",CSIDL_STARTMENU);
- ::WritePrivateProfileString("DIRECTORY","快启菜单目录0X0B",sData,sPath);
- sData.Format("%ld",CSIDL_DESKTOPDIRECTORY);
- ::WritePrivateProfileString("DIRECTORY","桌面目录0X10",sData,sPath);
- sData.Format("%ld",CSIDL_DRIVES);
- ::WritePrivateProfileString("DIRECTORY","我的电脑目录0X11",sData,sPath);
- sData.Format("%ld",CSIDL_NETWORK);
- ::WritePrivateProfileString("DIRECTORY","网上邻居目录0X12",sData,sPath);
-
复制代码- sData.Format("%ld",CSIDL_NETHOOD);
- ::WritePrivateProfileString("DIRECTORY","网络连接目录0X13",sData,sPath);
- sData.Format("%ld",CSIDL_FONTS);
- ::WritePrivateProfileString("DIRECTORY","字体目录0X14",sData,sPath);
- sData.Format("%ld",CSIDL_TEMPLATES);
- ::WritePrivateProfileString("DIRECTORY","文档模块目录0X15",sData,sPath);
- sData.Format("%ld",CSIDL_COMMON_STARTMENU);
- ::WritePrivateProfileString("DIRECTORY","所有用户开始菜单目录0X16",sData,sPath);
- sData.Format("%ld",CSIDL_COMMON_PROGRAMS);
- ::WritePrivateProfileString("DIRECTORY","所有用户程序目录0X17",sData,sPath);
- sData.Format("%ld",CSIDL_COMMON_STARTUP);
- ::WritePrivateProfileString("DIRECTORY","所有用户启动目录0X18",sData,sPath);
- sData.Format("%ld",CSIDL_COMMON_DESKTOPDIRECTORY);
- ::WritePrivateProfileString("DIRECTORY","所有用户桌面目录0X19",sData,sPath);
- sData.Format("%ld",CSIDL_APPDATA);
- ::WritePrivateProfileString("DIRECTORY","程序数据目录0X1A",sData,sPath);
- sData.Format("%ld",CSIDL_PRINTHOOD);
- ::WritePrivateProfileString("DIRECTORY","网络打印机目录0X1B",sData,sPath);
- sData.Format("%ld",CSIDL_ALTSTARTUP);
- ::WritePrivateProfileString("DIRECTORY","不清楚的目录0X1D",sData,sPath);
- sData.Format("%ld",CSIDL_COMMON_ALTSTARTUP);
- ::WritePrivateProfileString("DIRECTORY","不清楚的目录0X1E",sData,sPath);
- sData.Format("%ld",CSIDL_INTERNET_CACHE);
- ::WritePrivateProfileString("DIRECTORY","浏览器CACHE目录0X20",sData,sPath);
- sData.Format("%ld",CSIDL_COOKIES);
- ::WritePrivateProfileString("DIRECTORY","浏览器KOOKIES目录0X21",sData,sPath);
- sData.Format("%ld",CSIDL_HISTORY);
- ::WritePrivateProfileString("DIRECTORY","浏览器历史记录0X22",sData,sPath);
- sData.Format("%ld",CSIDL_PERSONAL);
- ::WritePrivateProfileString("DIRECTORY","文档目录0X05",sData,sPath);
- sData.Format("%ld",CSIDL_MYPICTURES);
- ::WritePrivateProfileString("DIRECTORY","图像目录0X27",sData,sPath);
- sData.Format("%ld",CSIDL_PROGRAM_FILES);
- ::WritePrivateProfileString("DIRECTORY","安装程序目录0X26",sData,sPath);
- sData.Format("%ld",CSIDL_SYSTEM);
- ::WritePrivateProfileString("DIRECTORY","SYSTEM32目录0X25",sData,sPath);
- sData.Format("%ld",CSIDL_WINDOWS);
- ::WritePrivateProfileString("DIRECTORY","WINDOWS目录0X24",sData,sPath);
- sData.Format("%ld",CSIDL_PROFILE);
- ::WritePrivateProfileString("DIRECTORY","配置文件目录0X28",sData,sPath);
- }
复制代码最后是在程序初始化时调用函数void InitCom(); 在程序退出时调用函数void SaveData(); 保存的参数文件可以在工程根目录查看内容; 我们来演示整个功能实现过程
|