前几天我发了帖子,询问创建经常在安装程序中出现的路径对话框的使用方法,网友SJcinux(香卜裁)提供了以下方法,使得我可以创建一个基本的路径对话框:
如下面的类定义实现功能该功能:
/**************************头文件DirDialog.h**********************************/
////////////////////////////////////////////////////////////////////////
// DirDialog.h: interface for the CDirDialog class.
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_)
#define AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000class CDirDialog
{
public:
CDirDialog();
virtual ~CDirDialog();
int DoBrowse(CWnd *pwndParent); CString m_strPath;
CString m_strInitDir;
CString m_strSelDir;
CString m_strWindowTitle;
int m_iImageIndex;
};#endif // !defined(AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_)/*****************************实现文件DirDialog.h************************/
///////////////////////////////////////////////////////////////////////////
// DirDialog.cpp: implementation of the CDirDialog class.
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "DirDialog.h"
#include "resource.h"// local includes for implementation
#include <shlobj.h>#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif// Callback function called by SHBrowseForFolder's browse control
// after initialization and when selection changes
static int __stdcall BrowseCtrlCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
CDirDialog* pDirDialogObj = (CDirDialog*)lpData;
if (uMsg == BFFM_INITIALIZED )
{
if( ! pDirDialogObj->m_strSelDir.IsEmpty() )
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)(LPCTSTR)(pDirDialogObj->m_strSelDir));
}
::SendMessage(hwnd, BFFM_ENABLEOK, 0, TRUE);
return 0;
}//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CDirDialog::CDirDialog()
{
m_strWindowTitle = _T("选择目标文件夹");
}CDirDialog::~CDirDialog()
{
}BOOL CDirDialog::DoBrowse(CWnd *pwndParent/*=NULL*/)
{ if( ! m_strSelDir.IsEmpty() )
{
m_strSelDir.TrimRight();
if( m_strSelDir.Right(1) == "\\" || m_strSelDir.Right(1) == "//" )
m_strSelDir = m_strSelDir.Left(m_strSelDir.GetLength() - 1);
} LPMALLOC pMalloc;
if (SHGetMalloc (&pMalloc)!= NOERROR)
return FALSE; BROWSEINFO bInfo;
LPITEMIDLIST pidl;
ZeroMemory ( (PVOID) &bInfo,sizeof (BROWSEINFO)); if (!m_strInitDir.IsEmpty ())
{
OLECHAR olePath[MAX_PATH];
ULONG chEaten;
ULONG dwAttributes;
HRESULT hr;
LPSHELLFOLDER pDesktopFolder;
//
// Get a pointer to the Desktop's IShellFolder interface.
//
if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
{
//
// IShellFolder::ParseDisplayName requires the file name be in Unicode.
//
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strInitDir.GetBuffer(MAX_PATH), -1,
olePath, MAX_PATH); m_strInitDir.ReleaseBuffer (-1);
//
// Convert the path to an ITEMIDLIST.
//
hr = pDesktopFolder->ParseDisplayName(NULL,
NULL,
olePath,
&chEaten,
&pidl,
&dwAttributes);
if (FAILED(hr))
{
pMalloc ->Free (pidl);
pMalloc ->Release ();
return FALSE;
}
bInfo.pidlRoot = pidl; }
}
bInfo.hwndOwner = pwndParent == NULL ? NULL : pwndParent->GetSafeHwnd();
bInfo.pszDisplayName = m_strPath.GetBuffer (MAX_PATH);
bInfo.lpszTitle = m_strWindowTitle;
bInfo.ulFlags = BIF_RETURNFSANCESTORS
| BIF_RETURNONLYFSDIRS
| (FALSE/*m_bStatus*/ ? BIF_STATUSTEXT : 0); bInfo.lpfn = BrowseCtrlCallback; // address of callback function
bInfo.lParam = (LPARAM)this; // pass address of object to callback function if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL)
{
return FALSE;
}
m_strPath.ReleaseBuffer();
m_iImageIndex = bInfo.iImage; if (::SHGetPathFromIDList(pidl, m_strPath.GetBuffer(MAX_PATH)) == FALSE)
{
pMalloc ->Free(pidl);
pMalloc ->Release();
return FALSE;
} m_strPath.ReleaseBuffer(); pMalloc ->Free(pidl);
pMalloc ->Release(); return TRUE;
}
//////////////////////////////////////////////////////////////////////
使用过程中,我又遇到了以下问题,这两个问题都集中在对BROWSEINFO结构中,ulFlags参数的设定:
1.我加入了选项BIF_EDITBOX,使“路径对话框”中有一个编辑对话框来显示选定的全部路径,但是我发现编辑框中只显示被选中的那个对象的名字。请问,如何获得这个对话框的句柄,对其操作呢?
2.我加入了BIF_NEWDIALOGSTYLE和(或)BIF_USENEWUI,希望这个“路径对话框”能够有新建文件夹等功能,但是我编译的时候,编译器提示这两个符号没有定义,我加入了头文件Shlobj.h,仍然是这样,请问怎么回事??在MSDN中,所有注明“Version 5.0”的选项,我都无法使用!!!谢谢!!!
如下面的类定义实现功能该功能:
/**************************头文件DirDialog.h**********************************/
////////////////////////////////////////////////////////////////////////
// DirDialog.h: interface for the CDirDialog class.
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_)
#define AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000class CDirDialog
{
public:
CDirDialog();
virtual ~CDirDialog();
int DoBrowse(CWnd *pwndParent); CString m_strPath;
CString m_strInitDir;
CString m_strSelDir;
CString m_strWindowTitle;
int m_iImageIndex;
};#endif // !defined(AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_)/*****************************实现文件DirDialog.h************************/
///////////////////////////////////////////////////////////////////////////
// DirDialog.cpp: implementation of the CDirDialog class.
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "DirDialog.h"
#include "resource.h"// local includes for implementation
#include <shlobj.h>#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif// Callback function called by SHBrowseForFolder's browse control
// after initialization and when selection changes
static int __stdcall BrowseCtrlCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
CDirDialog* pDirDialogObj = (CDirDialog*)lpData;
if (uMsg == BFFM_INITIALIZED )
{
if( ! pDirDialogObj->m_strSelDir.IsEmpty() )
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)(LPCTSTR)(pDirDialogObj->m_strSelDir));
}
::SendMessage(hwnd, BFFM_ENABLEOK, 0, TRUE);
return 0;
}//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CDirDialog::CDirDialog()
{
m_strWindowTitle = _T("选择目标文件夹");
}CDirDialog::~CDirDialog()
{
}BOOL CDirDialog::DoBrowse(CWnd *pwndParent/*=NULL*/)
{ if( ! m_strSelDir.IsEmpty() )
{
m_strSelDir.TrimRight();
if( m_strSelDir.Right(1) == "\\" || m_strSelDir.Right(1) == "//" )
m_strSelDir = m_strSelDir.Left(m_strSelDir.GetLength() - 1);
} LPMALLOC pMalloc;
if (SHGetMalloc (&pMalloc)!= NOERROR)
return FALSE; BROWSEINFO bInfo;
LPITEMIDLIST pidl;
ZeroMemory ( (PVOID) &bInfo,sizeof (BROWSEINFO)); if (!m_strInitDir.IsEmpty ())
{
OLECHAR olePath[MAX_PATH];
ULONG chEaten;
ULONG dwAttributes;
HRESULT hr;
LPSHELLFOLDER pDesktopFolder;
//
// Get a pointer to the Desktop's IShellFolder interface.
//
if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
{
//
// IShellFolder::ParseDisplayName requires the file name be in Unicode.
//
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strInitDir.GetBuffer(MAX_PATH), -1,
olePath, MAX_PATH); m_strInitDir.ReleaseBuffer (-1);
//
// Convert the path to an ITEMIDLIST.
//
hr = pDesktopFolder->ParseDisplayName(NULL,
NULL,
olePath,
&chEaten,
&pidl,
&dwAttributes);
if (FAILED(hr))
{
pMalloc ->Free (pidl);
pMalloc ->Release ();
return FALSE;
}
bInfo.pidlRoot = pidl; }
}
bInfo.hwndOwner = pwndParent == NULL ? NULL : pwndParent->GetSafeHwnd();
bInfo.pszDisplayName = m_strPath.GetBuffer (MAX_PATH);
bInfo.lpszTitle = m_strWindowTitle;
bInfo.ulFlags = BIF_RETURNFSANCESTORS
| BIF_RETURNONLYFSDIRS
| (FALSE/*m_bStatus*/ ? BIF_STATUSTEXT : 0); bInfo.lpfn = BrowseCtrlCallback; // address of callback function
bInfo.lParam = (LPARAM)this; // pass address of object to callback function if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL)
{
return FALSE;
}
m_strPath.ReleaseBuffer();
m_iImageIndex = bInfo.iImage; if (::SHGetPathFromIDList(pidl, m_strPath.GetBuffer(MAX_PATH)) == FALSE)
{
pMalloc ->Free(pidl);
pMalloc ->Release();
return FALSE;
} m_strPath.ReleaseBuffer(); pMalloc ->Free(pidl);
pMalloc ->Release(); return TRUE;
}
//////////////////////////////////////////////////////////////////////
使用过程中,我又遇到了以下问题,这两个问题都集中在对BROWSEINFO结构中,ulFlags参数的设定:
1.我加入了选项BIF_EDITBOX,使“路径对话框”中有一个编辑对话框来显示选定的全部路径,但是我发现编辑框中只显示被选中的那个对象的名字。请问,如何获得这个对话框的句柄,对其操作呢?
2.我加入了BIF_NEWDIALOGSTYLE和(或)BIF_USENEWUI,希望这个“路径对话框”能够有新建文件夹等功能,但是我编译的时候,编译器提示这两个符号没有定义,我加入了头文件Shlobj.h,仍然是这样,请问怎么回事??在MSDN中,所有注明“Version 5.0”的选项,我都无法使用!!!谢谢!!!
在MSDN中,所有注明“Version 5.0”的选项,我都无法使用!!!
=======
你应该到MS的网站上下载最新的platform sdk或者手工定义那些常量
{ CString sFolderPath;
BROWSEINFO bi;
char Buffer[MAX_PATH];
//初始化入口参数bi开始
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = Buffer;//此参数如为NULL则不能显示对话框(null 也行 --king 04/3/30)
bi.lpszTitle = "修改接收路径";
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.iImage=IDR_MAINFRAME;
//初始化入口参数bi结束
LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框
if(pIDList)
{
SHGetPathFromIDList(pIDList, Buffer);//!重要!取得文件夹路径到Buffer里
m_strSrcDir = sFolderPath;
UpdateData(false);
sFolderPath = Buffer;//将路径保存在一个CString对象里
}// LPMALLOC lpMalloc;
// if(FAILED(SHGetMalloc(&lpMalloc))) return;
// //释放内存
// lpMalloc->Free(pIDList);
// lpMalloc->Release();
}
int __stdcall CFileDialogST::BrowseCtrlCallback(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
static HWND heditWnd = NULL;//Edit的句柄
if (uMsg != BFFM_INITIALIZED)
{
if(heditWnd==NULL)
{
heditWnd = FindWindowEx(hWnd,NULL,"Edit",NULL);//得以句柄
}
}
if(uMsg == BFFM_SELCHANGED)
{
LPITEMIDLIST pidl=(LPITEMIDLIST)(lParam);
char wlj[256];
if (::SHGetPathFromIDList(pidl, wlj))
{
SetWindowText(heditWnd,wlj);//设置文本
}
}
return 0;
} 2.我加入了BIF_NEWDIALOGSTYLE和(或)BIF_USENEWUI,希望这个“路径对话框”能够有新建文件夹等功能,但是我编译的时候,编译器提示这两个符号没有定义,我加入了头文件Shlobj.h,仍然是这样,请问怎么回事??在MSDN中,所有注明“Version 5.0”的选项,我都无法使用!!!
: 查到这些宏的内容,然后直接使用。
#ifndef BIF_NEWDIALOGSTYLE
#define BIF_NEWDIALOGSTYLE 0x0040
#define BIF_USENEWUI (BIF_NEWDIALOGSTYLE | BIF_EDITBOX)
#endif