天天看點

在BCB中使用選擇檔案夾對話框

By jingzhongrong

程式設計時的需要,我封裝了一個類。由于BCB有些頭檔案中的結構定義與微軟Windows.h中有些結構的定義相同,是以在使用的時候應該在檔案最前的地方加上一句

#define NO_WIN32_LEAN_AND_MEAN 

下面是類的頭檔案

#define NO_WIN32_LEAN_AND_MEAN

#ifndef CommonH

#define CommonH

#include  <shlobj.h>

#include  <vcl.h>

//---------------------------------------------------------------------------

class  FolderBrowseDialog

{

 // for user

 // usage:

 // declare:

 //    FolderBrowseDialog fd;

 // set properties:

 //    fd.Title = "選擇目錄";

 //    fd.Execute(Application->Handle);

 // result:

 //    fd.FolderName

 //   fd.FolderPath

private :

   BROWSEINFO FInfo;

protected :

   char FFolderName[260];        

   AnsiString FFolderPath;       

   AnsiString __fastcall GetDialogTitle( void );

   void  __fastcall SetDialogTitle(AnsiString title);

   AnsiString __fastcall GetFolderPath(void );

   AnsiString __fastcall GetFolderName(void );

public :

  FolderBrowseDialog(HWND HwndOwner);

  FolderBrowseDialog();     //如果沒有指定句柄,則需使用帶參數的Execute函數

  void __fastcall SetBrowseInfoFlags(UINT ulFlags);     

   bool __fastcall Execute(void);       //打開對話框

  bool  __fastcall Execute(HWND HwndOwner);

   __property AnsiString Title={read=GetDialogTitle, write= SetDialogTitle};

   __property AnsiString FolderName={read= GetFolderName};

  __property AnsiString FolderPath={read= GetFolderPath};

};

使用方法如下:

 FolderBrowseDialog fd;

 set properties:

 fd.Title = "選擇目錄";

  fd.Execute(Application->Handle);

擷取結果

 fd.FolderName

  fd.FolderPath

下面是類的實作檔案:

#include "Common.h"   //上面頭檔案的名字

#define INFO_BUFFER_SIZE 32767

FolderBrowseDialog::FolderBrowseDialog(HWND HwndOwner)

{

   memset( &FInfo,0,sizeof (BROWSEINFO));

  memset(FFolderName,0,260 );

   FInfo.hwndOwner =  HwndOwner;

   FInfo.pszDisplayName =  FFolderName;

   FInfo.lpszTitle = "請選擇目錄" ;

   FInfo.ulFlags =  BIF_RETURNONLYFSDIRS;

}

//---------------------------------------------------------------------------

FolderBrowseDialog::FolderBrowseDialog()

{

   memset(&FInfo,0,sizeof (BROWSEINFO));

   memset(FFolderName,0,260 );

   FInfo.pszDisplayName =  FFolderName;

   FInfo.lpszTitle = "請選擇目錄" ;

   FInfo.ulFlags =  BIF_RETURNONLYFSDIRS;

}

//---------------------------------------------------------------------------

void  __fastcall FolderBrowseDialog::SetBrowseInfoFlags(UINT ulFlags)

{

   FInfo.ulFlags =  ulFlags;

}

//---------------------------------------------------------------------------

bool  __fastcall FolderBrowseDialog::Execute()

{

   LPITEMIDLIST ItemID;

   char  SelectDir[INFO_BUFFER_SIZE];

   memset(SelectDir,0 ,INFO_BUFFER_SIZE);

   ItemID = SHBrowseForFolder(& FInfo);

   if (ItemID)

   {

      SHGetPathFromIDList(ItemID,SelectDir);

      GlobalFree(ItemID);

      FFolderPath =  AnsiString(SelectDir);

      return true ;

   }

   else

   {

       return false ;

   }

}

//---------------------------------------------------------------------------

bool  __fastcall FolderBrowseDialog::Execute(HWND HwndOwner)

{

   FInfo.hwndOwner =  HwndOwner;

   if (Execute())

   {

      return true ;

   }

   else

   {

       return false ;

   }

}

//---------------------------------------------------------------------------

AnsiString __fastcall FolderBrowseDialog::GetDialogTitle()

{

   return  FInfo.lpszTitle;

}

//---------------------------------------------------------------------------

AnsiString __fastcall FolderBrowseDialog::GetFolderName()

{

   return  AnsiString(FFolderName);

}

//---------------------------------------------------------------------------

void  __fastcall FolderBrowseDialog::SetDialogTitle(AnsiString title)

{

   FInfo.lpszTitle =  title.c_str();

}

//---------------------------------------------------------------------------

AnsiString __fastcall FolderBrowseDialog::GetFolderPath()

{

   return  FFolderPath;

}

// ---------------------------------------------------------------------------

添加到工程中便可以使用了

繼續閱讀