CFileDialog的各种风格的目录/文件夹选择对话框---(CFolderDialog)
1. 标准的文件夹选择对话框:可以使用在非MFC程序中,调用的标准API SHBrowserForFolder。
源码:
[cpp]
#include "shlobj.h"
#include <STRING>
// Function name : GetFolder
// Description : Open and get Folder Dialog.
// Return type : true means click ok, false mean no select and cancel.
// Argument : folder path reference
// Argument : dialog window caption
// Argument : parent window handle
bool GetFolder(std::string& folderpath, const char* szCaption = NULL, HWND hOwner = NULL)
{
bool retVal = false;
// The BROWSEINFO struct tells the shell
// how it should display the dialog.
BROWSEINFO bi;
memset(&bi, 0, sizeof(bi));
bi.ulFlags = BIF_USENEWUI;
bi.hwndOwner = hOwner;
bi.lpszTitle = szCaption;
// must call this if using BIF_USENEWUI
::OleInitialize(NULL);
// Show the dialog and get the itemIDList for the selected folder.
LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);
if(pIDL != NULL)
{
// Create a buffer to store the path, then get the path.
char buffer[_MAX_PATH] = {'\0'};
if(::SHGetPathFromIDList(pIDL, buffer) != 0)
{
// Set the string value.
folderpath = buffer;
retVal = true;
}
// free the item id list
CoTaskMemFree(pIDL);
}
::OleUninitialize();
return retVal;
}
调用:
[cpp]
std::string szPath("");
if (GetFolder(szPath, "Select a folder.") == true)
{
printf("You selected: \"%s\".\n", szPath.c_str());
}
else
{
printf("No folder selected!\n");
}
界面:
2. 带导航栏的文件夹选择对话框:只在MFC程序中使用,从MFC的CFileDialog派生。
源码-头文件-Folder_dialog.h:
[cpp]print?#pragma once
// CFolderDialog dialog
class CFolderDialog : public CFileDialog
{
DECLARE_DYNAMIC(CFolderDialog)
public:
CFolderDialog(CString* pPath, CWnd* pParentWnd = NULL);
static WNDPROC m_wndProc;
CString* m_pPath;
protected:
DECLARE_MESSAGE_MAP()
private:
virtual void OnInitDone();
virtual void OnFileNameChange();
virtual void OnFolderChange();
void ChangeFolder();
};
#pragma once
// CFolderDialog dialog
class CFolderDialog : public CFileDialog
{
DECLARE_DYNAMIC(CFolderDialog)
public:
CFolderDialog(CString* pPath, CWnd* pParentWnd = NULL);
static WNDPROC m_wndProc;
CString* m_pPath;
protected:
DECLARE_MESSAGE_MAP()
private:
virtual void OnInitDone();
virtual void OnFileNameChange();
virtual void OnFolderChange();
void ChangeFolder();
};
源码-Folder_dialog.cpp:
[cpp]print?#include "stdafx.h"
#include "folder_dialog.h"
#include <DLGS.H>
#include <WINUSER.H>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CFolderDialog
IMPLEMENT_DYNAMIC(CFolderDialog, CFileDialog)
WNDPROC CFolderDialog::m_wndProc = NULL;
// Function name : CFolderDialog::CFolderDialog
// Description : Constructor
// Return type :
// Argument : CString* pPath ; represent string where selected folder wil be saved
CFolderDialog::CFolderDialog(CString* pPath, CWnd* pParentWnd) : CFileDialog(true, NULL, _T("*..*"), 6UL, NULL, pParentWnd)
{
m_pPath = pPath;
}
BEGIN_MESSAGE_MAP(CFolderDialog, CFileDialog)
END_MESSAGE_MAP()
// Function name : WindowProcNew
// Description : Call this function when user navigate into CFileDialog.
// Return type &n
补充:软件开发 , Vc ,