设为首页 加入收藏

TOP

CFileDialog的各种风格的目录/文件夹选择对话框---(CFolderDialog)(一)
2014-11-23 20:17:46 】 浏览:835
Tags:CFileDialog 各种 风格 目录 文件夹 选择 对话 --- CFolderDialog

1. 标准的文件夹选择对话框:可以使用在非MFC程序中,调用的标准API SHBrowserForFolder。
源码:
[cpp]
#include "shlobj.h"
#include

// 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
#include

#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

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇实战VC时间控制函数 下一篇vector彻彻底底干干净净清理内存..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目