BROWSEINFO结构
BROWSEINFO结构中包含有用户选中目录的重要信息。
●定义
typedef struct_browseinfo
{
HWND hwndOwner; //浏览文件夹对话框的父窗体句柄
LPCITEMIDLIST pidlRoot; //ITEMIDLIST结构的地址,包含浏览时的初始根目录,而且只有被指定的目录和其子目录才显示在 //浏览文件夹对话框中。该成员变量可以是NULL,在此时桌面目录将被使用。
LPSTR pszDisplayName; //保存用户选中的目录字符串的内存地址
LPCSTR lpszTitle; //该浏览文件夹对话框的显示文本,用来提示该浏览文件夹对话框的功能、作用和目的
UINT ulFlags; //该标志位描述了对话框的选项。它可以为0,也可以是一些常量的任意组合
BFFCALLBACK lpfn; //应用程序定义的浏览对话框回调函数的地址。当对话框中的事件发生时,该对话框将调用回调函 //数。该参数可用为NULL。
LPARAM lParam; //对话框传递给回调函数的一个参数指针
int iImage; //与选中目录相关的图像。该图像将被指定为系统图像列表中的索引值。
}BROWSEINFO,*PBROWSEINFO,*LPBROWSEINFO;
ulFlags:该标志位描述了对话框的选项。它可以为0,也可以是以下常量的任意组合:
BIF_BROWSEFORCOMPUTER:返回计算机名。除非用户选中浏览器中的一个计算机名,否则该对话框中的“OK”按钮为灰色。
BIF_BROWSEFORPRINTER:返回打印机名。除非选中一个打印机名,否则“OK”按钮为灰色。
BIF_BROWSEINCLUDEFILES:浏览器将显示目录,同时也显示文件。
BIF_DONTGOBELOWDOMAIN:在树形视窗中,不包含域名底下的网络目录结构。
BIF_EDITBOX:浏览对话框中包含一个编辑框,在该编辑框中用户可以输入选中项的名字。
BIF_RETURNFSANCESTORS:返回文件系统的一个节点。仅仅当选中的是有意义的节点时,“OK”按钮才可以使用。
BIF_RETURNONLYFSDIRS:仅仅返回文件系统的目录。例如:在浏览文件夹对话框中,当选中任意一个目录时,该“OK”按钮可用,而当选中“我的电脑”或“网上邻居”等非有意义的节点时,“OK”按钮为灰色。
BIF_STATUSTEXT:在对话框中包含一个状态区域。通过给对话框发送消息使回调函数设置状态文本。
BIF_VALIDATE:当没有BIF_EDITBOX标志位时,该标志位被忽略。如果用户在编辑框中输入的名字非法,浏览对话框将发送BFFM_VALIDATEFAILED消息给回调函数。
https://baike.baidu.com/item/BROWSEINFO
https://blog.youkuaiyun.com/shuilan0066/article/details/7302904
SHGetPathFromIDList
SHGetPathFromIDList:功能是把项目标志符列表转换为文档系统路径,包含两个参数:
pidl--- 一张相对于namespace的根(桌面)的项目标识符表地址,用于指定一个文档或目录地点;
pszPath---接收文档系统路径的缓冲地址,大小至少是MAX_PATH的字符长度
Parameters
pidl [in]
Type: PCIDLIST_ABSOLUTE
The address of an item identifier list that specifies a file or directory location relative to the root of the namespace (the desktop).
pszPath [out]
Type: LPTSTR
The address of a buffer to receive the file system path. This buffer must be at least MAX_PATH characters in size.
Return value
Type: BOOL
Returns TRUE if successful; otherwise, FALSE.
BrowseCallbackProc
This function specifies an application-defined callback function used with the SHBrowseForFolder function. The Browse For Folder dialog box calls this function to notify the application about events.
Syntax
int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lParam,LPARAM lpData);
Parameters
- hwnd
[in] Handle to the Browse For Folder dialog box. - uMsg
[in] Unsigned integer that specifies the event. The following table shows the possible values.Message Description BFFM_INITIALIZED Indicates that the Browse For Folder dialog box finished initializing. The value of the lParam parameter for the BrowseCallbackProc function is zero. BFFM_SELCHANGED Indicates that the selection in the dialog box changed. The lParam parameter for the BrowseCallbackProc function is a pointer to the ITEMIDLIST structure (PIDL) for the newly selected item. BFFM_VALIDATEFAILED Indicates that the user typed an invalid name into the edit box of the Browse For Folder dialog box. The lParam parameter for the BrowseCallbackProc function is a pointer to a character buffer that contains the invalid name. An application can use this message to inform the user that the name entered is not valid. Return zero to dismiss the dialog box or a nonzero value to continue displaying the dialog box. - lParam
[in] Value that depends on the message contained in the uMsg parameter. - lpData
[in] Application-defined value that was specified in the lParam member of the BROWSEINFO structure that was passed to SHBrowseForFolder.
Return Values
This function always returns zero.
Remarks
You can use the window handle in the hwnd parameter to send messages to a dialog box within your implementation of BrowseCallbackProc. The following table shows the messages that you can send.
Message | Description |
---|---|
BFFM_ENABLEOK | Enables or disables the OK button for the Browse For Folder dialog box. To enable the OK button, set the lParam parameter for this message to a nonzero value. To disable the OK button, set lParam for this message to zero. |
BFFM_SETSELECTION | Selects the specified folder. To use a pointer to an ITEMIDLIST structure (PIDL) to specify the folder, set lParam for this message to the PIDL and set wParam to FALSE. To specify the path to the folder, set the lParam value of the message to point to a NULL-terminated string that contains the path and set wParam to TRUE. |
BFFM_SETSTATUSTEXT | Sets the status text. Set the lParam value of this message to point to a NULL-terminated string that contains the desired text. |
SHBrowseForFolder
WINSHELLAPI LPITEMIDLIST WINAPI SHBrowseForFolder( LPBROWSEINFO lpbi);
https://blog.youkuaiyun.com/qq_23992597/article/details/50906942
SetCurrentDirectory
切换当前进程的当前工作目录。
BOOL WINAPI SetCurrentDirectory( __in LPCTSTR lpPathName);
参数说明
lpPathName [in]
The path to the new current directory. This parameter may specify a relative path or a full path. In either case, the full path of the specified directory is calculated and stored as the current directory. For more information, see File Names, Paths, and Namespaces.
The string must not exceed MAX_PATH characters, including the terminating null character. The final character before the null character must be a backslash ('\'). If you do not specify the backslash, it will be added for you; therefore, specify MAX_PATH-2 characters for the path unless you include the trailing backslash, in which case, specify MAX_PATH-1 characters for the path.
R V
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
例子:
.h
public:
static CString formerPath;
static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData);
.cpp:
CString Page2::formerPath = (CString)"C:\\1Snaps";
void Page2::OnBnClickedSnap()
{
// TODO: Add your control notification handler code here
if (!SetCurrentDirectory(formerPath))//程序运行的时候删除需要使用的文件时会显示:无法
//删除,有程序正在使用。
{
formerPath = "C:\\1Snaps\\";
}
char *buf = new char[MAX_PATH];
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO));
bi.hwndOwner = this->GetSafeHwnd();
bi.pidlRoot = 0;
bi.pszDisplayName = (LPWSTR)(LPCTSTR)buf;
bi.lpszTitle = L"Choose file path to save:";
bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_BROWSEINCLUDEFILES | BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT | BIF_EDITBOX | BIF_VALIDATE;
bi.lpfn = BrowseCallbackProc; //设置CALLBACK函数
bi.lParam = (LPARAM)(LPCTSTR) formerPath; //设置默认路径
bi.iImage = 0;
LPMALLOC pMalloc;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl == NULL)
return ;
if (pidl != NULL)
{
TCHAR * path = new TCHAR[MAX_PATH];
SHGetPathFromIDList(pidl, path);
if (SUCCEEDED(SHGetMalloc(&pMalloc)))//pidl指向的对象用完应该释放
{
pMalloc->Free(pidl);
pMalloc->Release();
}
formerPath = path;
CString folderPath = formerPath + "\\testFolder";
::CreateDirectoryW(folderPath, 0);
delete[] path;
}
delete[] buf;
}
int CALLBACK Page2::BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
switch (uMsg)
{
case BFFM_INITIALIZED: //初始化消息
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
break;
case BFFM_SELCHANGED: //选择路径变化
{
::SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)lpData);
}
break;
default:
break;
}
return 0;
}
run:
注意:需要头文件 #include "stdafx.h"。
整理后的代码:https://download.youkuaiyun.com/download/weixin_40236507/11071765
ShellExecute
ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。有几个API函数都可以实现这些功能,但是在大多数情况下ShellExecute是更多的被使用的,同时它并不是太复杂。
#include <shellapi.h>
ShellExecute函数原型及参数含义如下:
ShellExecute(
hWnd: HWND; {指定父窗口句柄}
Operation: PChar; {指定动作, 譬如: open、runas、print、edit、explore、find [2] }
FileName: PChar; {指定要打开的文件或程序}
Parameters: PChar; {给要打开的程序指定参数; 如果打开的是文件这里应该是 nil}
Directory: PChar; {缺省目录}
ShowCmd: Integer {打开选项}
): HINST;
ShowCmd 参数可选值:SW_HIDE = 0; {隐藏}
SW_SHOWNORMAL = 1; {用最近的大小和位置显示, 激活}
SW_NORMAL = 1; {同 SW_SHOWNORMAL}
SW_SHOWMINIMIZED = 2; {最小化, 激活}
SW_SHOWMAXIMIZED = 3; {最大化, 激活}
SW_MAXIMIZE = 3; {同 SW_SHOWMAXIMIZED}
SW_SHOWNOACTIVATE = 4; {用最近的大小和位置显示, 不激活}
SW_SHOW = 5; {同 SW_SHOWNORMAL}
SW_MINIMIZE = 6; {最小化, 不激活}
SW_SHOWMINNOACTIVE = 7; {同 SW_MINIMIZE}
SW_SHOWNA = 8; {同 SW_SHOWNOACTIVATE}
SW_RESTORE = 9; {同 SW_SHOWNORMAL}
SW_SHOWDEFAULT = 10; {同 SW_SHOWNORMAL}
SW_MAX = 10; {同 SW_SHOWNORMAL}
返回值
执行成功会返回应用程序句柄
返回的HINSTANCE可以将它转换为一个整数(%d),并比较它的值大于还是小于32或比较它的错误代码
返回值大于32表示执行成功
返回值小于32表示执行错误
返回值可能的错误有: = 0 {内存不足}
ERROR_FILE_NOT_FOUND = 2; {文件名错误}
ERROR_PATH_NOT_FOUND = 3; {路径名错误}
ERROR_BAD_FORMAT = 11; {EXE 文件无效}
SE_ERR_SHARE = 26; {发生共享错误}
SE_ERR_ASSOCINCOMPLETE = 27; {文件名不完全或无效}
SE_ERR_DDETIMEOUT = 28; {超时}
SE_ERR_DDEFAIL = 29; {DDE 事务失败}
SE_ERR_DDEBUSY = 30; {正在处理其他 DDE 事务而不能完成该 DDE 事务}
SE_ERR_NOASSOC = 31; {没有相关联的应用程序}
举例:
LPCTSTR strPath = L"C:\\1Snaps\\";
ShellExecuteW(NULL, NULL, _T("explorer"), strPath, NULL, SW_SHOW);
运行: