SHGetDesktopFolder()如何指向指定盘符(如:E盘)

郑沐洋 2014-02-08 10:40:37
如题,我在做一个资源管理器,现在程序每次运行首先指向的是桌面,我想让它指向E盘根目录,请问大家如何修改??程序如下:
void DirTreeView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
CTreeCtrl &Tree=GetTreeCtrl();

LPSHELLFOLDER lpsf=NULL; //IShellFolder接口
LPITEMIDLIST lpi=NULL; //ITERMIDLSIT结构体指针
HRESULT hr;
TV_ITEM tvi;

CImageList m_Image;//记录的是与树型控件关联的图像列表
SHFILEINFO sfi;//存放文件信息的结构
HIMAGELIST sys_small_icon;
sys_small_icon = (HIMAGELIST)SHGetFileInfo(NULL,0,&sfi,sizeof(sfi),
SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
m_Image.Attach(sys_small_icon);
Tree.SetImageList(&m_Image,TVSIL_NORMAL);//将系统图像列表设为树控件的图象列表
m_Image.Detach ();

// 得到指向“桌面”的指针
hr=SHGetDesktopFolder(&lpsf);
//SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOP,&lpi);
SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOP,&lpi);

if (SUCCEEDED(hr))
{
Tree.DeleteAllItems();

char szBuff[MAX_PATH];//存放显示名称的缓冲区
TV_INSERTSTRUCT tvis; //向树型控件中插入节点时使用的结构

tvi.mask = TVIF_TEXT | TVIF_IMAGE |
TVIF_SELECTEDIMAGE | TVIF_CHILDREN ; //设置要插入的树节点信息
if(GetName(lpsf,lpi,SHGDN_NORMAL,szBuff)==FALSE)
return;
tvi.pszText = szBuff;//设置显示名称

GetNormalAndSelectedIcons(lpi, &tvi);
tvis.item = tvi;
tvis.hParent = TVI_ROOT;

hParent = GetTreeCtrl().InsertItem(&tvis);//插入根节点
FillTreeView(lpsf, NULL, hParent);
GetTreeCtrl().Expand (hParent,TVE_EXPAND);//展开“桌面”节点
lpsf->Release ();
}
}
...全文
520 39 打赏 收藏 转发到动态 举报
写回复
用AI写文章
39 条回复
切换为时间正序
请发表友善的回复…
发表回复
郑沐洋 2014-02-13
  • 打赏
  • 举报
回复
schlafenhamster 2014-02-13
  • 打赏
  • 举报
回复
“根节点是桌面,我就是想让他跳到E盘” 初始化 tree 时,把 插入 E: 节点的 htreeitem 记 下来, 所有节点 插入 完 后,选择 这个 E 节点 为 当前 选择项。 在 list 中 , 通过 遍历 E:,填充 listctrl 的 内容。 遍历 e 可以 使用 FindFileFirst 和 FindFileNext 如果 熟悉 pidl 可以 使用 pidl 来 遍历 E:
郑沐洋 2014-02-13
  • 打赏
  • 举报
回复
引用 32 楼 schlafenhamster 的回复:
不需要用 pidl。(这是另一套) 直接 FindFileFirst 和 FindFileNext 遍历 E:
根节点是桌面,我就是想让他跳到E盘,求代码,我实在是不会,看了好多资料了,还是不会改
赵4老师 2014-02-12
  • 打赏
  • 举报
回复
ILCreateFromPath Function -------------------------------------------------------------------------------- Returns the ITEMIDLIST structure associated with a specified file path. Syntax PIDLIST_ABSOLUTE ILCreateFromPath( LPCSTR pszPath ); Parameters pszPath [in] A NULL-terminated Unicode string that contains the path. This string should be no more than MAX_PATH characters in length, including the terminating NULL character. Return Value Returns a pointer to an ITEMIDLIST structure that corresponds to the path. Remarks Call ILFree to release the ITEMIDLIST when you are finished with it. Function Information Minimum DLL Version shell32.dll version 5.0 or later Custom Implementation No Header shlobj.h Import library shell32.lib Minimum operating systems Windows 2000 Unicode Implemented as ANSI and Unicode versions. VC6: 工程、设置、C/C++、分类:Preprocessor、附加包含路径:填写附加头文件所在目录 逗号间隔多项 工程、设置、Link、分类:Input、附加库路径:填写附加依赖库所在目录 分号间隔多项 工程、设置、Link、分类:Input、对象/库模块:填写附加依赖库的名字.lib 空格间隔多项 VS20xx: 项目、属性、C/C++、附加包含目录:填写附加头文件所在目录 分号间隔多项 项目、属性、链接器、常规、附加库目录:填写附加依赖库所在目录 分号间隔多项 项目、属性、链接器、输入、附加依赖项:填写附加依赖库的名字.lib 空格或分号间隔多项 C:\Microsoft SDK\Include\ShlObj.h C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include\ShlObj.h C:\Program Files\Microsoft SDKs\Windows\v5.0\Include\ShlObj.h C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\ShlObj.h C:\Program Files\Microsoft Visual Studio\VC98\Include\SHLOBJ.H C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\ShlObj.h C:\Microsoft SDK\lib\Shell32.Lib C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Lib\Shell32.Lib C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib\shell32.lib C:\Program Files\Microsoft Visual Studio\VC98\Lib\SHELL32.LIB C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Lib\Shell32.Lib
schlafenhamster 2014-02-12
  • 打赏
  • 举报
回复
不需要用 pidl。(这是另一套) 直接 FindFileFirst 和 FindFileNext 遍历 E:
郑沐洋 2014-02-12
  • 打赏
  • 举报
回复
引用 17 楼 zhao4zhong1 的回复:
[quote=引用 13 楼 SXJIAKE 的回复:] 直接用 ILCreateFromPath
ILCreateFromPath Function -------------------------------------------------------------------------------- Returns the ITEMIDLIST structure associated with a specified file path. Syntax PIDLIST_ABSOLUTE ILCreateFromPath( LPCSTR pszPath ); Parameters pszPath [in] A NULL-terminated Unicode string that contains the path. This string should be no more than MAX_PATH characters in length, including the terminating NULL character. Return Value Returns a pointer to an ITEMIDLIST structure that corresponds to the path. Remarks Call ILFree to release the ITEMIDLIST when you are finished with it. Function Information Minimum DLL Version shell32.dll version 5.0 or later Custom Implementation No Header shlobj.h Import library shell32.lib Minimum operating systems Windows 2000 Unicode Implemented as ANSI and Unicode versions. --------------------------------------------------------------------------------
void DirTreeView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();
    CTreeCtrl &Tree=GetTreeCtrl();

    LPSHELLFOLDER lpsf=NULL; //IShellFolder接口
    LPITEMIDLIST  lpi=NULL; //ITERMIDLSIT结构体指针
    lpi=ILCreateFromPath(_T("E:\\"));//★
//  HRESULT hr;
    TV_ITEM tvi;

    CImageList m_Image;//记录的是与树型控件关联的图像列表
    SHFILEINFO sfi;//存放文件信息的结构
    HIMAGELIST sys_small_icon;
    sys_small_icon = (HIMAGELIST)SHGetFileInfo(NULL,0,&sfi,sizeof(sfi),
                     SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
    m_Image.Attach(sys_small_icon);
    Tree.SetImageList(&m_Image,TVSIL_NORMAL);//将系统图像列表设为树控件的图象列表
    m_Image.Detach ();

    // 得到指向“桌面”的指针
//  hr=SHGetDesktopFolder(&lpsf);
    //SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOP,&lpi);
//  SHGetSpecialFolderLocation(NULL,CSIDL_DRIVES ,&lpi);

//  if (SUCCEEDED(hr)) {
        Tree.DeleteAllItems();

        char szBuff[MAX_PATH];//存放显示名称的缓冲区
        TV_INSERTSTRUCT tvis; //向树型控件中插入节点时使用的结构

        tvi.mask = TVIF_TEXT | TVIF_IMAGE |
                   TVIF_SELECTEDIMAGE | TVIF_CHILDREN ; //设置要插入的树节点信息
        if(GetName(lpsf,lpi,SHGDN_NORMAL,szBuff)==FALSE)
            return;
        tvi.pszText = szBuff;//设置显示名称

        GetNormalAndSelectedIcons(lpi, &tvi);
        tvis.item = tvi;
        tvis.hParent = TVI_ROOT;

        hParent = GetTreeCtrl().InsertItem(&tvis);//插入根节点
        FillTreeView(lpsf, NULL, hParent);
        GetTreeCtrl().Expand (hParent,TVE_EXPAND);//展开节点
        lpsf->Release ();
        ILFree(lpi);//★这句可能不应放在这里,应放在lpi指向内容不再使用时
//  }
}
[/quote]赵老师,想来想去,觉得你的方法是对的,但是没有用过这个lpi=ILCreateFromPath(_T("E:\\"));,怎么声明它?每次运行都是不成功的
schlafenhamster 2014-02-12
  • 打赏
  • 举报
回复
filetree.GetSelectedItem();
郑沐洋 2014-02-12
  • 打赏
  • 举报
回复
引用 26 楼 schlafenhamster 的回复:
HTREEITEM m_hE =m_tree.InsertItem();// 插入一个E:
m_tree.SelectItem(m_hE);

我想调用这个函数,可是如下图,point是一个点,我怎么给他赋值呢?
郑沐洋 2014-02-12
  • 打赏
  • 举报
回复
引用 32 楼 schlafenhamster 的回复:
不需要用 pidl。(这是另一套) 直接 FindFileFirst 和 FindFileNext 遍历 E:
我试试看,我是小白!不会啊!
郑沐洋 2014-02-12
  • 打赏
  • 举报
回复
有人吗?
schlafenhamster 2014-02-11
  • 打赏
  • 举报
回复
HTREEITEM m_hE =m_tree.InsertItem();// 插入一个E: m_tree.SelectItem(m_hE);
郑沐洋 2014-02-11
  • 打赏
  • 举报
回复
引用 24 楼 schlafenhamster 的回复:
tree selectItem (文档E:) list 填充 E:的 内容:
代码!!!!
schlafenhamster 2014-02-11
  • 打赏
  • 举报
回复
tree selectItem (文档E:) list 填充 E:的 内容:
郑沐洋 2014-02-11
  • 打赏
  • 举报
回复
引用 21 楼 zhao4zhong1 的回复:
http://www.baidu.com/s?wd=ILCreateFromPath+site%3Amicrosoft.com&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_sug3=18&rsv_sug4=77&inputT=4500

如上图,老师,我现在运行是如上图的,如何是程序一运行就如下图!
郑沐洋 2014-02-11
  • 打赏
  • 举报
回复
引用 17 楼 zhao4zhong1 的回复:
[quote=引用 13 楼 SXJIAKE 的回复:] 直接用 ILCreateFromPath
ILCreateFromPath Function -------------------------------------------------------------------------------- Returns the ITEMIDLIST structure associated with a specified file path. Syntax PIDLIST_ABSOLUTE ILCreateFromPath( LPCSTR pszPath ); Parameters pszPath [in] A NULL-terminated Unicode string that contains the path. This string should be no more than MAX_PATH characters in length, including the terminating NULL character. Return Value Returns a pointer to an ITEMIDLIST structure that corresponds to the path. Remarks Call ILFree to release the ITEMIDLIST when you are finished with it. Function Information Minimum DLL Version shell32.dll version 5.0 or later Custom Implementation No Header shlobj.h Import library shell32.lib Minimum operating systems Windows 2000 Unicode Implemented as ANSI and Unicode versions. --------------------------------------------------------------------------------
void DirTreeView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();
    CTreeCtrl &Tree=GetTreeCtrl();

    LPSHELLFOLDER lpsf=NULL; //IShellFolder接口
    LPITEMIDLIST  lpi=NULL; //ITERMIDLSIT结构体指针
    lpi=ILCreateFromPath(_T("E:\\"));//★
//  HRESULT hr;
    TV_ITEM tvi;

    CImageList m_Image;//记录的是与树型控件关联的图像列表
    SHFILEINFO sfi;//存放文件信息的结构
    HIMAGELIST sys_small_icon;
    sys_small_icon = (HIMAGELIST)SHGetFileInfo(NULL,0,&sfi,sizeof(sfi),
                     SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
    m_Image.Attach(sys_small_icon);
    Tree.SetImageList(&m_Image,TVSIL_NORMAL);//将系统图像列表设为树控件的图象列表
    m_Image.Detach ();

    // 得到指向“桌面”的指针
//  hr=SHGetDesktopFolder(&lpsf);
    //SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOP,&lpi);
//  SHGetSpecialFolderLocation(NULL,CSIDL_DRIVES ,&lpi);

//  if (SUCCEEDED(hr)) {
        Tree.DeleteAllItems();

        char szBuff[MAX_PATH];//存放显示名称的缓冲区
        TV_INSERTSTRUCT tvis; //向树型控件中插入节点时使用的结构

        tvi.mask = TVIF_TEXT | TVIF_IMAGE |
                   TVIF_SELECTEDIMAGE | TVIF_CHILDREN ; //设置要插入的树节点信息
        if(GetName(lpsf,lpi,SHGDN_NORMAL,szBuff)==FALSE)
            return;
        tvi.pszText = szBuff;//设置显示名称

        GetNormalAndSelectedIcons(lpi, &tvi);
        tvis.item = tvi;
        tvis.hParent = TVI_ROOT;

        hParent = GetTreeCtrl().InsertItem(&tvis);//插入根节点
        FillTreeView(lpsf, NULL, hParent);
        GetTreeCtrl().Expand (hParent,TVE_EXPAND);//展开节点
        lpsf->Release ();
        ILFree(lpi);//★这句可能不应放在这里,应放在lpi指向内容不再使用时
//  }
}
[/quote]error C2065: 'ILCreateFromPath' : undeclared identifier
郑沐洋 2014-02-11
  • 打赏
  • 举报
回复
郑沐洋 2014-02-10
  • 打赏
  • 举报
回复
引用 11 楼 zhao4zhong1 的回复:
参考下面? C:\Microsoft SDK\samples\Web\NetMeeting\NMUI\Util.Cpp
    434: //****************************************************************************
    435: //
    436: // BOOL FGetDirectory(LPTSTR szDir)
    437: //
    438: //****************************************************************************
    439: 
    440: BOOL FGetDirectory(LPTSTR szDir)
    441: {
    442:     BOOL  fRet;
    443:     TCHAR  szPath[MAX_PATH];
    444:     LPITEMIDLIST pidl;
    445:     LPITEMIDLIST pidlRoot;
    446:     LPMALLOC lpMalloc;
    447: 
    448:     BROWSEINFO bi = {
    449:         ghwndMain,
    450:         NULL,
    451:         szPath,
    452:         _szPickDir,
    453:         BIF_RETURNONLYFSDIRS,
    454:         NULL, 0L, 0 };
    455: 
    456:     if (0 != SHGetSpecialFolderLocation(HWND_DESKTOP, CSIDL_DRIVES, &pidlRoot))
    457:         return FALSE;
    458:     if (NULL == pidlRoot)
    459:         return FALSE;
    460: 
    461:     bi.pidlRoot = pidlRoot;
    462:     pidl = SHBrowseForFolder(&bi);
    463: 
    464:     if (NULL != pidl)
    465:         fRet = SHGetPathFromIDList(pidl, szDir);
    466:     else
    467:         fRet = FALSE;
    468: 
    469:     // Get the shell's allocator to free PIDLs
    470:     if (!SHGetMalloc(&lpMalloc) && (NULL != lpMalloc))
    471:     {
    472:         if (NULL != pidlRoot)
    473:         {
    474:             lpMalloc->Free(pidlRoot);
    475:         }
    476: 
    477:         if (NULL != pidl)
    478:         {
    479:             lpMalloc->Free(pidl);
    480:         }
    481: 
    482:         lpMalloc->Release();
    483:     }
    484: 
    485:     return fRet;
    486: }
谢谢赵老师,我研究一下!
赵4老师 2014-02-10
  • 打赏
  • 举报
回复
参考下面? C:\Microsoft SDK\samples\Web\NetMeeting\NMUI\Util.Cpp
    434: //****************************************************************************
    435: //
    436: // BOOL FGetDirectory(LPTSTR szDir)
    437: //
    438: //****************************************************************************
    439: 
    440: BOOL FGetDirectory(LPTSTR szDir)
    441: {
    442:     BOOL  fRet;
    443:     TCHAR  szPath[MAX_PATH];
    444:     LPITEMIDLIST pidl;
    445:     LPITEMIDLIST pidlRoot;
    446:     LPMALLOC lpMalloc;
    447: 
    448:     BROWSEINFO bi = {
    449:         ghwndMain,
    450:         NULL,
    451:         szPath,
    452:         _szPickDir,
    453:         BIF_RETURNONLYFSDIRS,
    454:         NULL, 0L, 0 };
    455: 
    456:     if (0 != SHGetSpecialFolderLocation(HWND_DESKTOP, CSIDL_DRIVES, &pidlRoot))
    457:         return FALSE;
    458:     if (NULL == pidlRoot)
    459:         return FALSE;
    460: 
    461:     bi.pidlRoot = pidlRoot;
    462:     pidl = SHBrowseForFolder(&bi);
    463: 
    464:     if (NULL != pidl)
    465:         fRet = SHGetPathFromIDList(pidl, szDir);
    466:     else
    467:         fRet = FALSE;
    468: 
    469:     // Get the shell's allocator to free PIDLs
    470:     if (!SHGetMalloc(&lpMalloc) && (NULL != lpMalloc))
    471:     {
    472:         if (NULL != pidlRoot)
    473:         {
    474:             lpMalloc->Free(pidlRoot);
    475:         }
    476: 
    477:         if (NULL != pidl)
    478:         {
    479:             lpMalloc->Free(pidl);
    480:         }
    481: 
    482:         lpMalloc->Release();
    483:     }
    484: 
    485:     return fRet;
    486: }
加载更多回复(19)

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧