现在的位置: 首页 > 综合 > 正文

关于SHBrowseForFolder的用法。SHBrowseForFolder来初始化选择目录

2013年08月21日 ⁄ 综合 ⁄ 共 2779字 ⁄ 字号 评论关闭

//给你一个函数
int GetPath(HWND hWnd,char *pBuffer)
{
BROWSEINFO bf;
LPITEMIDLIST lpitem;
memset(&bf,0,sizeof BROWSEINFO);
bf.hwndOwner=hWnd;
bf.lpszTitle="选择路径";
bf.ulFlags=BIF_RETURNONLYFSDIRS; //属性你可自己选择
lpitem=SHBrowseForFolder(&bf);
if(lpitem==NULL) //如果没有选择路径则返回 0
return 0;

//如果选择了路径则复制路径,返回路径长度

SHGetPathFromIDList(lpitem,pBuffer);
return lstrlen(pBuffer);

 

 

 用SHBrowseForFolder时,请定义BROWSEINFO中的回调函数,你将可以
控制他的行为。给你一段例子.
//以下程序通SHBrowseForFolder返回用户选择的目录
//具有以下几个特性。
//1。有初始化目录
//2。每次选择的目录自动增加字符/sictech
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
switch(uMsg){
case BFFM_INITIALIZED:
::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)"C://Program Files");
break;
case BFFM_SELCHANGED:
{
char curr[MAX_PATH];
SHGetPathFromIDList((LPCITEMIDLIST)lParam,curr);
if(curr[strlen(curr)-1]==92)
sprintf(curr,"%sSictech",curr);
else
sprintf(curr,"%s//Sictech",curr);
::SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)curr);
}
break;
}
return 0;
}

void CSetPathDlg::OnGetPath()
{
// TODO: Add your control notification handler code here
char Path[MAX_PATH];
LPITEMIDLIST ret;
BROWSEINFO lpbi;
lpbi.hwndOwner=m_hWnd;
lpbi.pidlRoot=NULL;
lpbi.ulFlags=BIF_STATUSTEXT;
lpbi.pszDisplayName=Path;
lpbi.lpszTitle="Get Path";
lpbi.lpfn=BrowseCallbackProc;
ret=SHBrowseForFolder(&lpbi);
if(ret!=NULL){
SHGetPathFromIDList(ret,Path);
if(Path[strlen(Path)-1]==92)
sprintf(Path,"%sSictech",Path);
else
sprintf(Path,"%s//Sictech",Path);
}
}

 

 

CCriticalSection cs;
CString g_strCurrDir;

void SetCurrDirForSHBrowseForFolder(const CString& strCurrDir)
{
 CGuardLock<CCriticalSection> Guard(&cs);
 g_strCurrDir = strCurrDir;
}

int CALLBACK FECFolderProc(HWND hWnd, UINT nMsg, LPARAM, LPARAM lpData)
{
    if (BFFM_INITIALIZED == nMsg)
    { 
  CString szPath("");
  {
   CGuardLock<CCriticalSection> Guard(&cs);
   szPath = g_strCurrDir;
  }
        if (_T("////") != szPath.Left(2)) // SHBrowseForFolder does not like UNC path
        {
            int len = szPath.GetLength() - 1;
            if (2 != len && _T('//') == szPath[len])
                szPath.Delete(len); // remove trailing slash (SHBrowseForFolder does not like it)
            ::SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)(LPCTSTR)szPath);
        }
    }
    return 0;
}

CString BrowseForFolderBySH(LPCTSTR lpTitle, CWnd* pWnd, CString strCurrDir)
{
 BROWSEINFO  BrowseDir;
 LPCITEMIDLIST pIdl;
 CString   strFolderPath;

 BrowseDir.hwndOwner  = pWnd->m_hWnd;
 BrowseDir.lpszTitle  = lpTitle;
 BrowseDir.pidlRoot  = NULL;
 BrowseDir.ulFlags  = 0;
 BrowseDir.pszDisplayName= strFolderPath.GetBuffer( MAX_PATH );
 BrowseDir.lParam  = (LPARAM)pWnd;
 BrowseDir.lpfn   = NULL;
 if (!strCurrDir.IsEmpty())
 {
  SetCurrDirForSHBrowseForFolder(strCurrDir);
  BrowseDir.lpfn  = FECFolderProc;
 }

 pIdl = SHBrowseForFolder( &BrowseDir );
 strFolderPath.ReleaseBuffer();
 strFolderPath.Empty();
 if ( pIdl != NULL )
 {
  SHGetPathFromIDList( pIdl, strFolderPath.GetBuffer( MAX_PATH ) );
  strFolderPath.ReleaseBuffer();
 }
 return strFolderPath;

 

抱歉!评论已关闭.