天下

记录修行的印记

为SHBrowseForFolder指定初始目录

SHBrowseForFolder可以用来得到一个用户选择的目录。

可是有时候会有需要去指定一个初始目录,比如希望上次用户选择的目录可以保存下来。这该如何去做?

在BROWSEINFO结构体中提供了一个成员,这是一个指向函数的指针,通过这个回调函数,可以处理初始化的时候需要做的一些事情。

其中函数名是固定的:BrowseCallbackProc()

BrowseCallbackProc Function


Specifies an application-defined callback function used to send messages to, and process messages from, a Browse dialog box displayed in response to a call to SHBrowseForFolder.

Syntax

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

Parameters

hwnd
The window handle of the browse dialog box.
uMsg
The dialog box event that generated the message. One of the following values.
BFFM_INITIALIZED
The dialog box has finished initializing.
BFFM_IUNKNOWN
An IUnknown interface is available to the dialog box.
BFFM_SELCHANGED
The selection has changed in the dialog box.
BFFM_VALIDATEFAILED
Version 4.71. The user typed an invalid name into the dialog's edit box. A nonexistent folder is considered an invalid name.
lParam
A value whose meaning depends on the event specified in uMsg as follows:
uMsg lParam
BFFM_INITIALIZED Not used, value is NULL.
BFFM_IUNKNOWN A pointer to an IUnknown interface.
BFFM_SELCHANGED A pointer to an item identifier list (PIDL) identifying the newly selected item.
BFFM_VALIDATEFAILED A pointer to a string containing the invalid name. An application can use this data in an error dialog informing the user that the name was not valid.
lpData
An application-defined value that was specified in the lParam member of the BROWSEINFO structure used in the call to SHBrowseForFolder.

Return Value

Returns zero except in the case of BFFM_VALIDATEFAILED. For that flag, returns zero to dismiss the dialog or nonzero to keep the dialog displayed.

Remarks

To attach your BrowseCallbackProc to a dialog, specify its address in the lpfn member of the BROWSEINFO structure used in a SHBrowseForFolder call.

BrowseCallbackProc can also send messages to the dialog box through SendMessage, controlling these aspects of that dialog box.

  • OK button enabled/disabled
  • OK button text
  • Selected folder
  • Expanded folder
  • Status text
Set the SendMessage function's Msg parameter to one of the following values, providing additional information in the wParam and lParam parameters as indicated for each message type.

BFFM_ENABLEOK

Enables or disables the dialog box's OK button.

  • wParam. Not used.
  • lParam. To enable, set to a nonzero value. To disable, set to zero.

BFFM_SETOKTEXT

Version 6.0. Sets the text that is displayed on the dialog box's OK button.

  • wParam. Not used.
  • lParam. A pointer to a null-terminated Unicode string containing the desired text.

BFFM_SETSELECTION

Specifies the path of a folder to select. The path can be specified as a string or a PIDL.

To use a string:

  • wParam. Set to TRUE.
  • lParam. A pointer to the null-terminated Unicode string that specifies the path.

To use a PIDL:

  • wParam. Set to FALSE.
  • lParam. The PIDL that specifies the path.

BFFM_SETEXPANDED

Version 6.0. Specifies the path of a folder to expand in the Browse dialog box. The path can be specified as a Unicode string or a PIDL.

To use a Unicode string:

  • wParam. Set to TRUE
  • lParam. A pointer to the null-terminated Unicode string that specifies the path.

To use a PIDL:

  • wParam. Set to FALSE
  • lParam. The PIDL that specifies the path.

BFFM_SETSTATUSTEXT

Sets the status text. Set lpData to point to a null-terminated string with the desired text.

  • wParam. Not used.
  • lParam. A pointer to a null-terminated string containing the desired text.
具体写法如下:

 

int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)   
{   
    
if  (uMsg == BFFM_INITIALIZED )
    {   
        ::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,lpData);   
    }   
    
return 0;  
}
 

    //使用:
    CString strLastPath = GetLastOpenDirectory();
    LPITEMIDLIST pidlRoot 
= NULL;

    BROWSEINFO bi;
    ZeroMemory(
&bi,sizeof(BROWSEINFO));
    bi.hwndOwner    
= GetSafeHwnd();
    bi.lpszTitle    
= _T("请选择目录");
    bi.ulFlags      
= BIF_RETURNONLYFSDIRS | BIF_USENEWUI | BIF_NONEWFOLDERBUTTON; 
    bi.lpfn         
= NULL;
    bi.pidlRoot     
= pidlRoot;
    bi.lParam       
= (LPARAM)strLastPath.GetBuffer();
    bi.lpfn         
= BrowseCallbackProc;

    if(pidl == NULL)
        return;

    SHGetPathFromIDList(pidl, csPath.GetBuffer(MAX_PATH));
    csPath.ReleaseBuffer();


posted on 2010-12-13 17:49 天下 阅读(1697) 评论(0)  编辑 收藏 引用 所属分类: Win32


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2020年2月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
1234567

导航

统计

常用链接

留言簿(4)

随笔分类(377)

随笔档案(327)

链接

最新随笔

搜索

最新评论