常用的代码片断

1、选择文件夹

 (1) 封装了一个类,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 >= 1000

class 选择目录的对话框类
class  CDirDialog
{
public:
 brief 构造函数
 CDirDialog();
 
 brief 析构函数
 virtual ~CDirDialog();
 
 brief 弹出系统对话框 取得的路径名
 param CWnd *pParent 父窗口对象指针
 return 成功true,失败false
 bool DoBrowse(CWnd *pParent);
 
 brief dialog的标题
 CString m_strDlgTitle;

 brief 最终选择的目录
 CString m_strSelDir;  

 brief 初始化的根目录
 CString m_strRootDir; 
 
 brief 初始化选择的目录
 CString m_strInitDir; 
 
 brief 在目录列表上方显示的标题
 CString m_strTitle;   
 
 brief 当选择目录时,是否可以显示选择的目录
 bool m_bStatus;       
 
 brief 选择成功后 和改目录链接的Image的索引,它是系统 image list的索引
 int  m_iImageIndex;   
protected:
 brief 控制一个目录是否可以选择,并且返回状态信息
 param LPCSTR lpcsSelection 当前选择的目录
 param CString& csStatusText 返回的状态信息
 return  可以选择true, 不可以选择false
 virtual bool SelChanged(LPCSTR lpcsSelection, CString& csStatusText);
 
 brief 选择目录的回调函数
 static int __stdcall BrowseCtrlCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData);
};
#endif // !defined(AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_)

 


(2)DirDialog.cpp文件为:

///
// DirDialog.cpp: implementation of the CDirDialog class.
//
//

#include "stdafx.h"
#include "DirDialog.h"
#include "shlobj.h"

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

int __stdcall CDirDialog::BrowseCtrlCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
 CDirDialog* pDirDialogObj = (CDirDialog*)lpData;
 if (uMsg == BFFM_INITIALIZED )
 {
  if( ! pDirDialogObj->m_strInitDir.IsEmpty() )
   ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)(LPCTSTR)(pDirDialogObj->m_strInitDir));
  if( ! pDirDialogObj->m_strDlgTitle.IsEmpty() )
   ::SetWindowText(hwnd, (LPCTSTR) pDirDialogObj->m_strDlgTitle);
 }
 else if( uMsg == BFFM_SELCHANGED )
 {
  LPITEMIDLIST pidl = (LPITEMIDLIST) lParam;
  char selection[MAX_PATH];
  if( ! ::SHGetPathFromIDList(pidl, selection) )
   selection[0] = _T('/0');
  CString csStatusText;
  bool bOK = pDirDialogObj->SelChanged(selection, csStatusText);
  if( pDirDialogObj->m_bStatus )
   ::SendMessage(hwnd, BFFM_SETSTATUSTEXT , 0, (LPARAM)(LPCSTR)csStatusText);
  BOOL bCanSelect = TRUE ;
  if(!bOK)
   bCanSelect = FALSE ;
  ::SendMessage(hwnd, BFFM_ENABLEOK, 0, bCanSelect);
 }
 return 0;
}


//
// Construction/Destruction
//

CDirDialog::CDirDialog()
{
 m_bStatus = false ;
}

CDirDialog::~CDirDialog()
{
}

//取得的路径名有路径标识符
bool CDirDialog::DoBrowse(CWnd *pParent)
{
 m_strInitDir.TrimRight();
 if( ! m_strInitDir.IsEmpty() )
 {
  if( m_strInitDir.Right(1) == _T("//") || m_strInitDir.Right(1) == _T("//") )
   m_strInitDir = m_strInitDir.Left(m_strInitDir.GetLength() - 1);
  
  //correct selection "X:"
  if( m_strInitDir.Right(1) == _T(":"))
   m_strInitDir+=_T("//");
 }
 
 bool bOK = false ;
 LPMALLOC pMalloc;
 if (SHGetMalloc (&pMalloc)!= NOERROR)
 {
  return bOK;
 }
 
 BROWSEINFO bInfo;
 LPITEMIDLIST pidl;
 ZeroMemory ( (PVOID) &bInfo,sizeof (BROWSEINFO));
 
 if (!m_strRootDir.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.
   //
#ifndef _UNICODE
   MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strRootDir.GetBuffer(MAX_PATH), -1,
    olePath, MAX_PATH);
#else
   memcpy(olePath,m_strRootDir,m_strRootDir.GetLength() * sizeof(TCHAR));
#endif
   m_strRootDir.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 bOK;
   }
   bInfo.pidlRoot = pidl;
   
  }
 }
 if(m_strTitle.IsEmpty())
  m_strTitle = _T("请选择目录");
 bInfo.hwndOwner = pParent->GetSafeHwnd() ;
 bInfo.pszDisplayName = m_strSelDir.GetBuffer (MAX_PATH);
 bInfo.lpszTitle = (LPCTSTR)m_strTitle;
 bInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS| (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)
 {
  m_strSelDir.ReleaseBuffer();
  return bOK;
 }
 m_strSelDir.ReleaseBuffer();
 m_iImageIndex = bInfo.iImage;
 
 bOK = (::SHGetPathFromIDList(pidl,m_strSelDir.GetBuffer(MAX_PATH)) == TRUE );
 m_strSelDir.ReleaseBuffer();
 pMalloc ->Free(pidl);
 pMalloc ->Release();
 if(bOK)
 {
  if(m_strSelDir.IsEmpty())
   m_strSelDir = _T('//');
  else if(m_strSelDir.Right(1) != _T('//') )
   m_strSelDir += _T('//');
 }
 return bOK;
}

bool CDirDialog::SelChanged(LPCSTR lpcsSelection, CString& csStatusText)
{
 bool bOK = false ;
 csStatusText.Empty();
 CString strPath(lpcsSelection);
 if(!strPath.IsEmpty())
 {
  if(m_bStatus)
   csStatusText = strPath ;
  bOK = true;
 }
 return bOK;
}

 

(3)在主函数中调用

 UpdateData(true);
 CDirDialog dlg;
 if(dlg.DoBrowse(this))
 {
  m_strFloderPath = dlg.m_strSelDir;      //获得选中的文件夹路径
  UpdateData(FALSE);
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值