force SHBrowseForFolder() to show desired directory

给你一囗甜甜゛ 提交于 2019-12-12 11:33:01

问题


I've been searching online and fighting this thing for over an hour and still can't seem to get it to work. Most people seem to be satisfied when they get it this far on forums etc. but mine still doesn't work. I'm trying to force the SHBrowseForFolder() function to start in a folder of my choosing.

char current[MAX_PATH];
strcpy(current,"C:\\Users");

char outbuf[MAX_PATH];
BROWSEINFO bis;
bis.hwndOwner = NULL;
bis.pidlRoot = NULL;
bis.pszDisplayName = outbuf;
bis.lpszTitle = (LPCSTR)"HERE";
bis.ulFlags = BIF_NEWDIALOGSTYLE|BIF_RETURNONLYFSDIRS;
bis.lpfn = NULL;
bis.lParam = (LPARAM)current;

SHBrowseForFolder(
    &bis
);

It seems like this should be a relatively simple task. :/ At the moment, the above code is still showing the default: the Desktop folder. Beyond starting in a specific folder, if possible, I'd also like it to ONLY show that folder and below, with no access to parent directories.

What am I missing here?


回答1:


You can also send a BFFM_SETSELECTION message from your BrowseCallbackProc, like:

int FAR PASCAL BrowseNotify(HWND hWnd, UINT iMessage, long wParam, LPARAM lParam)
{   if (iMessage == BFFM_INITIALIZED)
    {   SendMessage(hWnd, BFFM_SETSELECTION, 1, (LPARAM) szInitialPathName);    // Set initial folder
        return 1;
    }
    return 0;
}



回答2:


Set the BFFCALLBACK (lpfn) to a BrowseCallbackProc. From there you can call SendMessage with BFFM_SETEXPANDED to specify the path of a folder to expand in the Browse dialog box.

See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb773205(v=vs.85).aspx and http://msdn.microsoft.com/en-us/library/windows/desktop/bb762598(v=vs.85).aspx

From my experience, that folder dialog is a bit flaky - it often scrolls the desired directory out of view and looks suboptimal. Just one of the joys of Windows...

Also, there is no way I have discovered to get it to show only that directory and its subs. The parent directories always seem to be there.




回答3:


Set BIF.PidlRoot to the PIDL you don't want the user to browse below, select and expand the folder you want initially focused and selected - do as above - and it should work.

Jens. :)



来源:https://stackoverflow.com/questions/17471771/force-shbrowseforfolder-to-show-desired-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!