选择文件夹对话框代码

来源:互联网 发布:gif图片大小修改软件 编辑:程序博客网 时间:2024/06/06 06:38

用了一下,还不错。原文地址:http://blog.csdn.net/sunxysong/archive/2005/08/17/456603.aspx

在windows日常编程中,时常要用到"打开文件","保存文件","选择文件夹"等系统对话框.对于前
两者MFC中都有已封装好的类直接可以使用,而对于后一种形式的系统对话框MFC中还没有将其直接封装成类.但是MFC中虽提供了相关的几个函数和相应的结构来解决实现"选择文件夹"对话框的问题.
 现将其写成一个函数,以便日后方便使用.(在MFC环境下,包含必要的头文件)


//////////////////////打开选择文件夹对话框代码/////////////
//MFC下
#include <direct.h>
#include <shlobj.h> 

CString GetDirPath(int type) //type保留
{
 BROWSEINFO lpbi;
 char lpDir[MAX_PATH];
 CString path="";

 lpbi.pidlRoot=NULL;
 lpbi.hwndOwner =this->m_hWnd ;
 lpbi.lpszTitle ="选择文件夹:";
 lpbi.ulFlags =0;//BIF_BROWSEINCLUDEFILES;
 lpbi.pszDisplayName =lpDir;
 lpbi.lpfn=NULL;
 lpbi.lParam=0;
 lpbi.iImage=0;

 LPITEMIDLIST lpidl= SHBrowseForFolder (&lpbi);

 if(lpidl && SHGetPathFromIDList (lpidl,lpDir))
 {
  int i=strlen(lpDir);
  if(lpDir[i-1]!='//')
  {
   lpDir[i]='//';
   lpDir[i+1]='/0';
  }
  //AfxMessageBox(lpDir);
  path =CString(lpDir);
 }
 return path;
}
//函数返回选择的文件夹路径path

//////////////////////////////////////////////////////////////////


//附带实现该功能的MSDN参考:

////////////////////////一.函数//////////////////////////////////

//1.函数原形
LPITEMIDLIST SHBrowseForFolder(LPBROWSEINFO lpbi);

//2.参数:

lpbi
[in, out] Pointer to a BROWSEINFO structure. On entry, this structure conveys information used to display the dialog box. On exit, it receives information concerning the selected folder.

//3.返回值:

Returns a pointer to an item identifier list (PIDL) specifying the location of the selected folder relative to the root of the namespace. If the user chooses the Cancel button in the dialog box, the return value is NULL.
It is possible that the PIDL returned is that of a folder shortcut rather than a folder. For a full discussion of this case, see the Remarks section.

The display name of the object selected is returned in the buffer pointed to by the pszDisplayName member of the BROWSEINFO parameter structure.

////////////////////////二.参数结构/////////////////////////////

//其中函数参数的结构如下:

typedef struct _browseinfo {
    HWND hwndOwner;
    LPCITEMIDLIST pidlRoot;
    LPTSTR pszDisplayName;
    LPCTSTR lpszTitle;
    UINT ulFlags;
    BFFCALLBACK lpfn;
    LPARAM lParam;
    int iImage;
} BROWSEINFO, *PBROWSEINFO, *LPBROWSEINFO;


//参数含义:


hwndOwner
Handle to the owner window for the dialog box.

pidlRoot
Pointer to an item identifier list (PIDL) specifying the location of the root folder from which to start browsing. Only the specified folder and any subfolders that are beneath it in the namespace hierarchy will appear in the dialog box. This member can be NULL; in that case, the namespace root (the desktop folder) is used.

pszDisplayName
Address of a buffer to receive the display name of the folder selected by the user. The size of this buffer is assumed to be MAX_PATH characters.

lpszTitle
Address of a null-terminated string that is displayed above the tree view control in the dialog box. This string can be used to specify instructions to the user.

ulFlags
Flags specifying the options for the dialog box. This member can include zero or a combination of the following values.

BIF_BROWSEFORCOMPUTER
Only return computers. If the user selects anything other than a computer, the OK button is grayed.

BIF_BROWSEFORPRINTER
Only allow the selection of printers. If the user selects anything other than a printer, the OK button is grayed.
In Microsoft Windows XP, the best practice is to use an XP-style dialog, setting the root of the dialog to the Printers and Faxes folder (CSIDL_PRINTERS).

BIF_BROWSEINCLUDEFILES
Version 4.71. The browse dialog box will display files as well as folders.

BIF_BROWSEINCLUDEURLS
Version 5.0. The browse dialog box can display URLs. The BIF_USENEWUI and BIF_BROWSEINCLUDEFILES flags must also be set. If these three flags are not set, the browser dialog box will reject URLs. Even when these flags are set, the browse dialog box will only display URLs if the folder that contains the selected item supports them. When the folder's IShellFolder::GetAttributesOf method is called to request the selected item's attributes, the folder must set the SFGAO_FOLDER attribute flag. Otherwise, the browse dialog box will not display the URL.

BIF_DONTGOBELOWDOMAIN
Do not include network folders below the domain level in the dialog box's tree view control.

BIF_EDITBOX
Version 4.71. Include an edit control in the browse dialog box that allows the user to type the name of an item.

BIF_NEWDIALOGSTYLE
Version 5.0. Use the new user interface. Setting this flag provides the user with a larger dialog box that can be resized. The dialog box has several new capabilities including: drag-and-drop capability within the dialog box, reordering, shortcut menus, new folders, delete, and other shortcut menu commands. To use this flag, you must call OleInitialize or CoInitialize before calling SHBrowseForFolder.

BIF_NONEWFOLDERBUTTON
Version 6.0. Do not include the New Folder button in the browse dialog box.

BIF_NOTRANSLATETARGETS
Version 6.0. When the selected item is a shortcut, return the PIDL of the shortcut itself rather than its target.

BIF_RETURNFSANCESTORS
Only return file system ancestors. An ancestor is a subfolder that is beneath the root folder in the namespace hierarchy. If the user selects an ancestor of the root folder that is not part of the file system, the OK button is grayed.

BIF_RETURNONLYFSDIRS
Only return file system directories. If the user selects folders that are not part of the file system, the OK button is grayed.

BIF_SHAREABLE
Version 5.0. The browse dialog box can display shareable resources on remote systems. It is intended for applications that want to expose remote shares on a local system. The BIF_NEWDIALOGSTYLE flag must also be set.

BIF_STATUSTEXT
Include a status area in the dialog box. The callback function can set the status text by sending messages to the dialog box. This flag is not supported when BIF_NEWDIALOGSTYLE is specified.

BIF_UAHINT
Version 6.0. When combined with BIF_NEWDIALOGSTYLE, adds a usage hint to the dialog box in place of the edit box. BIF_EDITBOX overrides this flag.

BIF_USENEWUI
Version 5.0. Use the new user interface, including an edit box. This flag is equivalent to BIF_EDITBOX | BIF_NEWDIALOGSTYLE. To use BIF_USENEWUI, you must call OleInitialize or CoInitialize before calling SHBrowseForFolder.

BIF_VALIDATE
Version 4.71. If the user types an invalid name into the edit box, the browse dialog box will call the application's BrowseCallbackProc with the BFFM_VALIDATEFAILED message. This flag is ignored if BIF_EDITBOX is not specified.

lpfn
Address of an application-defined function that the dialog box calls when an event occurs. For more information, see the BrowseCallbackProc function. This member can be NULL.

lParam
Application-defined value that the dialog box passes to the callback function, if one is specified.

iImage
Variable to receive the image associated with the selected folder. The image is specified as an index to the system image list.


 
原创粉丝点击