SHBrowseForFolder  BFFM_IUNKNOWN 怎么用?

HelloDan 2009-10-26 03:20:24
我想在SHBrowseForFolder中过滤掉CD/DVD,但不知要怎样做,因为我对com熟悉,有没有人可以提示一下的?谢谢!


Custom Filtering

As of Windows XP, SHBrowseForFolder supports custom filtering on the contents of the dialog box. To create a custom filter, follow these steps.

1. Set the BIF_NEWDIALOGSTYLE flag in the ulFlags member of the BROWSEINFO structure pointed to by the lpbi parameter.
2. Specify a callback function in the lpfn member of that same BROWSEINFO structure.
3. Code the callback function to receive the BFFM_INITIALIZED and BFFM_IUNKNOWN messages. On receipt of the BFFM_IUNKNOWN message, the callback function's lParam parameter contains a pointer to the dialog box's implementation of IUnknown. Call QueryInterface on that IUnknown to obtain a pointer to an instance of IFolderFilterSite.
4. Create an object that implements IFolderFilter.
5. Call IFolderFilterSite::SetFilter, passing to it a pointer to your IFolderFilter. IFolderFilter methods can then be used to include and exclude items from the tree.
6. Once the filter is created, the IFolderFilterSite interface is no longer needed. Call IFolderFilterSite::Release if you have no further use for it.
...全文
337 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jingzhongrong 2009-10-28
  • 打赏
  • 举报
回复
调用SHBrowserForFolder之前先
::CoInitialize(NULL)
HelloDan 2009-10-27
  • 打赏
  • 举报
回复

//我这样做了。。。

// The following code to get a folder path which you //select.
BOOL ShellGetOutPath(HANDLE hDlg, LPTSTR lpszRoot, LPTSTR lpszPath, LPCTSTR lpszDesc/* = 0*/)
{
BOOL bRet;
//TCHAR szPath[MAX_PATH];
LPITEMIDLIST lpil;
//HGLOBAL hgMem;

// the following segment define and initialize the BROWSEINFO, prepareing for the SHBrowseForFolder API
BROWSEINFO bi;
bi.hwndOwner=(HWND) hDlg;
IShellFolder *ppshf;
SHGetDesktopFolder(&ppshf);
if(!ppshf)
return FALSE;
LPITEMIDLIST pidlRoot = NULL;
ppshf->ParseDisplayName((HWND)hDlg, NULL, lpszRoot, NULL, &pidlRoot, NULL);
if(!pidlRoot)
return FALSE;
bi.pidlRoot = pidlRoot;
bi.pszDisplayName = lpszPath;//szPath;
//strMsg.LoadString(IDS_STR_FOLDER_OUTPUT);
bi.lpszTitle = lpszDesc;
bi.ulFlags = /*BIF_DONTGOBELOWDOMAIN |*/BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE ;
bi.lpfn = BFFCALLBACK(BrowseCallbackProcInit);
bi.lParam = (LPARAM)lpszPath;
lpil = SHBrowseForFolder(&bi);
if(lpil == NULL)
return FALSE;
bRet = SHGetPathFromIDList(lpil, lpszPath);
CoTaskMemFree(lpil);
//CString strPath = lpszPath;
TCHAR szDrive[_MAX_DRIVE];
_tsplitpath(lpszPath, szDrive, NULL, NULL, NULL);
return bRet;
}


// the callback function for the BROWSEINFO, to implement your own function
// here I try to do that:
// 1, when the user slelect CD/DVD drives, the OK and Make New Folder button
// will be disable.
// 2, Change the title of the dialogbox.
int CALLBACK BrowseCallbackProcInit(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
TCHAR szPath[MAX_PATH];
// TCHAR szTargetPath[MAX_PATH];
TCHAR szDrive[MAX_PATH], szDir[MAX_PATH], szFile[MAX_PATH], szExt[MAX_PATH];
LPCITEMIDLIST pidl;
// WIN32_FIND_DATA FileData;
// HANDLE hSearch;
static HWND hMakeNewFolder=NULL;

TCHAR szWindosText[MAX_PATH+1];
switch(uMsg)
{
case BFFM_SELCHANGED:
{
pidl = LPCITEMIDLIST(lParam);
SHGetPathFromIDList(pidl, szPath);
if (szPath[0] == '\0')
SendMessage(hwnd, BFFM_ENABLEOK, lParam, 0);
_tsplitpath(szPath, szDrive, szDir, szFile, szExt);
TCHAR szRoot[MAX_PATH+1];
lstrcpyn(szRoot,szPath, MAX_PATH);
::PathStripToRoot(szRoot);
// disable when select CD/DVD drive, otherwise enables
if(GetDriveType(szRoot)==DRIVE_CDROM)
{
HWND btnHwnd=GetDlgItem(hwnd,IDOK);
::EnableWindow( btnHwnd, FALSE);
if(hMakeNewFolder)
::EnableWindow(hMakeNewFolder,FALSE);
//SendMessage(hwnd,BFFM_SETSELECTION ,TRUE ,(LPARAM)(_T("C:\\")));
}
else
{
HWND btnHwnd=GetDlgItem(hwnd,IDOK);
::EnableWindow( btnHwnd, TRUE);
if(hMakeNewFolder)
::EnableWindow(hMakeNewFolder,TRUE);
}

}
break;
case BFFM_INITIALIZED:
{
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)(LPCTSTR)lpData);
//if (szLastPath[0] == '\0')
// SendMessage(hwnd, BFFM_ENABLEOK, lParam, 0);
//Change the caption of the Browse for Folder caption
CString szCaption((LPCTSTR)lpData);
if(szCaption.GetLength()>2)
{
szCaption.LoadString(IDS_SAVE_AGENT_INSTALLER);
SetWindowText(hwnd,szCaption);
}
HWND hChild=GetWindow(hwnd, GW_CHILD);
//Get the tree view handle
//while(hChild)
//{
// TCHAR szClass[256];
// GetClassName(hChild, szClass, 255);
// //
// if (strcmp(szClass, _T("SHBrowseForFolder ShellNameSpace Control")) == 0)
// {
// hTree=hChild;
// break;
// }
// hChild = GetNextWindow(hChild, GW_HWNDNEXT);
//}
while(hChild)
{
GetWindowText(hChild,szWindosText,MAX_PATH);
TCHAR szText[]=_T("&Make New Folder");
if(lstrcmp(szWindosText,szText)==0)
{
//find and keep the make new folder button handle
hMakeNewFolder=hChild;
//::OutputDebugString(_T("find the control"));
//::OutputDebugString(szWindosText);
break;
}
hChild = GetNextWindow(hChild, GW_HWNDNEXT);
}

}
break;
}
return 0;
}

jeff_5388 2009-10-26
  • 打赏
  • 举报
回复
class CFolderFilter : public IFolderFilter
{

...


};

int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)

if (uMsg == BFFM_IUNKNOWN) {
IUnknown * lpUnk = (IUnknown*)lParam;
CComQIPtr<IFolderFilterSite> spSite(lpUnk);
CFolderFilter * lpFilter = new CFolderFilter;
spSite->SetFilter(lpFilter);
}



BROWSEINFO bi = { 0 };
...
bi.lpfn = BrowseCallbackProc;
HelloDan 2009-10-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liumenghappy 的回复:]
不懂,帮你顶起
[/Quote]

谢谢!
liumenghappy 2009-10-26
  • 打赏
  • 举报
回复
不懂,帮你顶起

16,551

社区成员

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

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

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