MFC如何打开保存文件夹的对话框?

MJJiang 2008-01-14 04:44:44
类似于CFileDialog打开某个具体文件的对话框
只不过现在打开的是一个文件夹

具体背景:由用户指定一个文件夹,读取其路径然后读取其中的文件

谢谢 ^-^
...全文
1404 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Torch009 2008-01-14
  • 打赏
  • 举报
回复
LPITEMIDLIST pidlPath;
char pszDplName[MAX_PATH];
BROWSEINFO biFile;
IMalloc *pDllMlc;
biFile.hwndOwner = m_hWnd;
biFile.pidlRoot = NULL;
biFile.pszDisplayName = NULL;
biFile.lpszTitle = "属性设置";
biFile.ulFlags = BIF_RETURNFSANCESTORS;
biFile.lpfn = NULL;
biFile.lParam = NULL;
biFile.iImage = 0;
if(SUCCEEDED(SHGetMalloc(&pDllMlc)))
{
pidlPath=SHBrowseForFolder(&biFile);
if(pidlPath != NULL)
SHGetPathFromIDList(pidlPath,pszDplName);//pszDplName:文件的路径地址
else return;
pDllMlc->Free(NULL);
pDllMlc->Release();
}
这个才是关键代码。这个不是保存对话框。
caofusheng 2008-01-14
  • 打赏
  • 举报
回复
-- BrowseForFolder.cpp --
//////////////////////////////////////////////////////////////////////
//
// ShellBrowser.cpp: implementation of the CShellBrowser class.
//

#include "stdafx.h"
#include "BrowseForFolder.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
//
// Construction/Destruction
//

CBrowseForFolder::CBrowseForFolder(const HWND hParent /*= NULL*/, const LPITEMIDLIST pidl /*= NULL*/, const int nTitleID /*= 0*/)
{
m_hwnd = NULL;
SetOwner(hParent);
SetRoot(pidl);
SetTitle(nTitleID);
m_bi.lpfn = BrowseCallbackProc;
m_bi.lParam = reinterpret_cast(this);
m_bi.pszDisplayName = m_szSelected;
}

CBrowseForFolder::CBrowseForFolder(const HWND hParent, const LPITEMIDLIST pidl, const CString& strTitle)
{
m_hwnd = NULL;
SetOwner(hParent);
SetRoot(pidl);
SetTitle(strTitle);
m_bi.lpfn = BrowseCallbackProc;
m_bi.lParam = reinterpret_cast(this);
m_bi.pszDisplayName = m_szSelected;
}

CBrowseForFolder::~CBrowseForFolder()
{

}

//////////////////////////////////////////////////////////////////////
//
// Implementation
//

void CBrowseForFolder::SetOwner(const HWND hwndOwner)
{
if (m_hwnd != NULL)
return;

m_bi.hwndOwner = hwndOwner;
}

void CBrowseForFolder::SetRoot(const LPITEMIDLIST pidl)
{
if (m_hwnd != NULL)
return;

m_bi.pidlRoot = pidl;
}

CString CBrowseForFolder::GetTitle() const
{
return m_bi.lpszTitle;
}

void CBrowseForFolder::SetTitle(const CString& strTitle)
{
if (m_hwnd != NULL)
return;

m_pchTitle = std::auto_ptr(new char [static_cast(strTitle.GetLength()) + 1]);
strcpy(m_pchTitle.get(), strTitle);
m_bi.lpszTitle = m_pchTitle.get();
}

bool CBrowseForFolder::SetTitle(const int nTitle)
{
if (nTitle <= 0)
return false;

CString strTitle;
if(!strTitle.LoadString(static_cast(nTitle)))
{
return false;
}
SetTitle(strTitle);
return true;
}

void CBrowseForFolder::SetFlags(const UINT ulFlags)
{
if (m_hwnd != NULL)
return;

m_bi.ulFlags = ulFlags;
}

CString CBrowseForFolder::GetSelectedFolder() const
{
return m_szSelected;
}

bool CBrowseForFolder::SelectFolder()
{
bool bRet = false;

LPITEMIDLIST pidl;
if ((pidl = ::SHBrowseForFolder(&m_bi)) != NULL)
{
m_strPath.Empty();
if (SUCCEEDED(::SHGetPathFromIDList(pidl, m_szSelected)))
{
bRet = true;
m_strPath = m_szSelected;
}

LPMALLOC pMalloc;
//Retrieve a pointer to the shell's IMalloc interface
if (SUCCEEDED(SHGetMalloc(&pMalloc)))
{
// free the PIDL that SHBrowseForFolder returned to us.
pMalloc->Free(pidl);
// release the shell's IMalloc interface
(void)pMalloc->Release();
}
}
m_hwnd = NULL;

return bRet;
}

void CBrowseForFolder::OnInit() const
{

}

void CBrowseForFolder::OnSelChanged(const LPITEMIDLIST pidl) const
{
(void)pidl;
}

void CBrowseForFolder::EnableOK(const bool bEnable) const
{
if (m_hwnd == NULL)
return;

(void)SendMessage(m_hwnd, BFFM_ENABLEOK, static_cast(bEnable), NULL);
}

void CBrowseForFolder::SetSelection(const LPITEMIDLIST pidl) const
{
if (m_hwnd == NULL)
return;

(void)SendMessage(m_hwnd, BFFM_SETSELECTION, FALSE, reinterpret_cast(pidl));
}

void CBrowseForFolder::SetSelection(const CString& strPath) const
{
if (m_hwnd == NULL)
return;

(void)SendMessage(m_hwnd, BFFM_SETSELECTION, TRUE, reinterpret_cast(LPCTSTR(strPath)));
}

void CBrowseForFolder::SetStatusText(const CString& strText) const
{
if (m_hwnd == NULL)
return;

(void)SendMessage(m_hwnd, BFFM_SETSTATUSTEXT, NULL, reinterpret_cast(LPCTSTR(strText)));
}

int __stdcall CBrowseForFolder::BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
CBrowseForFolder* pbff = reinterpret_cast(lpData);
pbff->m_hwnd = hwnd;
if (uMsg == BFFM_INITIALIZED)
pbff->OnInit();
else if (uMsg == BFFM_SELCHANGED)
pbff->OnSelChanged(reinterpret_cast(lParam));

return 0;
}
caofusheng 2008-01-14
  • 打赏
  • 举报
回复
这儿有一个完善的SHBrowseForFolder封装类看你能用得上吗
-- BrowseForfolder.h --

//////////////////////////////////////////////////////////////////////
//
// ShellBrowser.h: interface for the CShellBrowser class.
//
// Copyright 1998 Scott D. Killen
//
//////////////////////////////////////////////////////////////////////

#ifndef __SHELLBROWSER_H__
#define __SHELLBROWSER_H__

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#include
#include

//////////////////////////////////////////////////////////////////////
//
// CShellBrowser
//

class CBrowseForFolder
{
public:
CBrowseForFolder(const HWND hParent = NULL,
const LPITEMIDLIST pidl = NULL,
const int nTitleID = 0);

CBrowseForFolder(const HWND hParent,
const LPITEMIDLIST pidl,
const CString& strTitle);

virtual ~CBrowseForFolder() = 0;

//
// Set the handle of the owner window for the dialog box.
//
void SetOwner(const HWND hwndOwner);

//
// Set the root of the heirarchy that will be browsed. Get pidl from
// SHGetSpecialFolderLocation. This can be set to NULL to use the Virtual Folder
// that represents the Windows Desktop.
//
void SetRoot(const LPITEMIDLIST pidl);

//
// Access a string that is displayed above the tree view control in the dialog box.
// This string can be used to specify instructions to the user. strTitle is a
// CString containing the text to be displayed. nTitle is the index of a string
// resource to be loaded. The return value is false if the resource could not be
// loaded.
//
CString GetTitle() const;
void SetTitle(const CString& strTitle);
bool SetTitle(const int nTitle);

//
// ulFlags = Value specifying the types of folders to be listed in the dialog box
// as well as other options. This member can include zero or more of the following
// values:
//
// BIF_BROWSEFORCOMPUTER Only returns computers. If the user selects
// anything other than a computer, the OK button
// is grayed.
//
// BIF_BROWSEFORPRINTER Only returns printers. If the user selects
// anything other than a printer, the OK button
// is grayed.
//
// BIF_DONTGOBELOWDOMAIN Does not include network folders below the
// domain level in the tree view control.
//
// BIF_RETURNFSANCESTORS Only returns file system ancestors. If the user
// selects anything other than a file system
// ancestor, the OK button is grayed.
//
// BIF_RETURNONLYFSDIRS Only returns file system directories. If the
// user selects folders that are not part of the
// file system, the OK button is grayed.
//
// BIF_STATUSTEXT Includes a status area in the dialog box. The
// callback function can set the status text by
// sending messages to the dialog box.
//
UINT GetFlags() const;
void SetFlags(const UINT ulFlags);

//
// Call GetSelectedFolder to retrieve the folder selected by the user.
//
CString GetSelectedFolder() const;

//
// Function to retreive the image associated with the selected folder. The image is
// specified as an index to the system image list.
//
int GetImage() const;

//
// Call SelectFolder to display the dialog and get a selection from the user. Use
// GetSelectedFolder and GetImage to get the results of the dialog.
//
bool SelectFolder();

protected:
//
// OnInit is called before the dialog is displayed on the screen.
//
virtual void OnInit() const;

//
// OnSelChanged is called whenever the user selects a different directory. pidl is
// the LPITEMIDLIST of the new selection. Use SHGetPathFromIDList to retrieve the
// path of the selection.
//
virtual void OnSelChanged(const LPITEMIDLIST pidl) const;

//
// Call EnableOK to enable the OK button on the active dialog. If bEnable is true
// then the button is enabled, otherwise it is disabled.
// NOTE -- This function should only be called within overrides of OnInit and
// OnSelChanged.
//
void EnableOK(const bool bEnable) const;

//
// Call SetSelection to set the selection in the active dialog. pidl is the
// LPITEMIDLIST
// of the path to be selected. strPath is a CString containing the path to be
// selected.
// NOTE -- This function should only be called within overrides of OnInit and
// OnSelChanged.
//
void SetSelection(const LPITEMIDLIST pidl) const;
void SetSelection(const CString& strPath) const;

//
// Call SetStatusText to set the text in the Status area in the active dialog.
// strText is the text to be displayed.
// NOTE -- This function should only be called within overrides of OnInit and
// OnSelChanged.
//
void SetStatusText(const CString& strText) const;

private:
static int __stdcall BrowseCallbackProc(HWND hwnd,
UINT uMsg,
LPARAM lParam,
LPARAM lpData);

typedef std::auto_ptr AUTO_STR;
AUTO_STR m_pchTitle;

BROWSEINFO m_bi;
char m_szSelected[MAX_PATH];
CString m_strPath;
HWND m_hwnd;
};

inline UINT CBrowseForFolder::GetFlags() const
{
return m_bi.ulFlags;
}

inline int CBrowseForFolder::GetImage() const
{
return m_bi.iImage;
}

#endif // __SHELLBROWSER_H__
caofusheng 2008-01-14
  • 打赏
  • 举报
回复

::CoInitialize(NULL);
LPITEMIDLIST pidlPath;
char pszDplName[MAX_PATH];
BROWSEINFO biFile;
IMalloc *pDllMlc;
biFile.hwndOwner = m_hWnd;
biFile.pidlRoot = NULL;
biFile.pszDisplayName = NULL;
biFile.lpszTitle = "属性设置";
biFile.ulFlags = BIF_RETURNFSANCESTORS;
biFile.lpfn = NULL;
biFile.lParam = NULL;
biFile.iImage = 0;
if(SUCCEEDED(SHGetMalloc(&pDllMlc)))
{
pidlPath=SHBrowseForFolder(&biFile);
if(pidlPath != NULL)
SHGetPathFromIDList(pidlPath,pszDplName);//pszDplName:文件的路径地址
else return;
pDllMlc->Free(NULL);
pDllMlc->Release();
}
::CoUninitialize();
内存泄漏 2008-01-14
  • 打赏
  • 举报
回复
有一个N年前写的查询某目录所有jpg文件的函数, 你改成查询*.*就可以了..

LPSTR FindFile(LPSTR dir)
{
static long f=-1;
static char temp[23]="";
if(temp=="" || strcmp(temp,dir))
{
strcpy(temp,dir);
f=-1;
}
_finddata_t fileinfo;
char buf[100];
memset(buf,0,sizeof(buf));
strcat(buf,dir);
strcat(buf,"\\*.jpg");
re_search:
if(f==-1)
{
f=_findfirst(buf,&fileinfo);
if(f==-1)
{
char bufnul[2]="";
return bufnul;
}
}
else
{
if(_findnext(f,&fileinfo)!=0)
{
f=-1;
goto re_search;
}
}
return fileinfo.name;
}


调用的时候, 参数给需要查询的目录, 然后写个循环不断调用该函数, 并对返回值进行判断, 如果是""字符串, 则当前目录没有符合条件的文件, 否则返回文件名, 如果返回的文件名原来返回过, 则证明目录当中所有的文件名都已列举完毕, 此时可以退出循环..
菜牛 2008-01-14
  • 打赏
  • 举报
回复
SHBrowseForFolder Function
MFC打开文件和文件夹 一、利用API实现打开文件对话框和利用MFC来实现打开文件对话框。 方法一:API部分: // OPENFILENAME ofn; // TCHAR szFile[MAX_PATH]; // ZeroMemory(&ofn,sizeof(ofn)); // ofn.lStructSize = sizeof(ofn); // ofn.lpstrFile = szFile; // ofn.lpstrFile[0] = TEXT('/0'); // ofn.nMaxFile = sizeof(szFile); // ofn.lpstrFilter = TEXT("all/0*.*/0jpg/0*.jpg/0bmp/0*.bmp/0"); //定义三个选项,all,text和exe // ofn.nFilterIndex = 1; //定义首选项是哪一个 // ofn.lpstrFileTitle = NULL; // ofn.nMaxFileTitle = 0; // ofn.lpstrInitialDir = NULL; // ofn.Flags = OFN_EXPLORER |OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; // if(GetOpenFileName(&ofn)) // { // ::SetDlgItemText(this->m_hWnd,IDC_EDIT1,szFile); // } 方法二、MFC实现 // CFileDialog dlg(TRUE, NULL, NULL, // OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, // NULL); // // if(dlg.DoModal() == IDOK) // { // szFilename = dlg.GetPathName(); // ::MessageBox(NULL,szFilename,NULL,MB_OK); // } 注意:打开文件的方式和打开文件夹的方式是不同的。打开文件的方式是不断的打开直到出现末端为文件,否则遇到文件夹还要继续打开。而打开文件夹则是只要选择到一个路径的文件夹打开。 下面的这种方式是打开文件夹MFC实现。 static TCHAR strDirName[MAX_PATH]; BROWSEINFO bi; CString szString = TEXT("选择一个源文件子文件夹"); bi.hwndOwner = ::GetFocus(); bi.pidlRoot = NULL; bi.pszDisplayName = strDirName; bi.lpszTitle = szString; bi.ulFlags = BIF_BROWSEFORCOMPUTER | BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = 0; bi.iImage = 0; LPITEMIDLIST pItemIDList = ::SHBrowseForFolder(&bi); if(pItemIDList == NULL) { return ; } ::SHGetPathFromIDList(pItemIDList, strDirName); CString str = strDirName; if(str != "" && str.Right(1) != '//') str += '//'; ::SetDlgItemText(this->m_hWnd,IDC_EDIT1,str);

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧