How to customize a BrowseForFolder dialog

2 01 2009

description2

 

 

We all are familiar with BrowseForFolder dialog. A shell type dialog contains SysTreeView32 control, OK button, Cancel button etc. Does anybody face a situation like to customize this dialog?

That means how to add some custom controls to this dialog or how to rearrange the already existing child controls.

Please look at the image, I have customized the default browse for folder dialog by adding some custom controls to it. This could be achieved by using some Win32 APIs like SetWindowPos, FindWindowEx,GetClientRect, CreateWindowEx etc.

custombrowseforfolderdlg

mycodesnippet2

 

 

Where firstly declare the lpfn to the BrowseCallbackProc procedure. Which is a callback function. Then whenever the browse for folder dialog box initializes it will invoke this callback function with “BFFM_INITIALIZED” as the message. That time we could create custom controls on the dialog.

#define BROWSE_WIDTH      380
#define BROWSE_HEIGHT     530
HWND g_hMyEditBox;
void ShowCustomBrowseForFolderDlg()
{
   BROWSEINFO bi  = { 0 };
   bi.lpszTitle   = _T("Pick a Directory");
   bi.lpfn        = BrowseCallbackProc;
  
   LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
}
int BrowseCallbackProc( HWND hwnd,UINT uMsg,LPARAM lParam,LPARAM lpData)
{
  
   //Initialization callback message
   if(BFFM_INITIALIZED == uMsg)
   {      
      RECT rtDialog,rtButton;
      RECT DesktopRect;
     
      // Get the desktop window
      HWND hDesktop = ::GetDesktopWindow();
     
      // Get the Desktop rect
      ::GetWindowRect(hDesktop, &DesktopRect);
     
      // Move the window to center
      ::MoveWindow(p_hWnd, (DesktopRect.right/2)-(BROWSE_WIDTH/2) ,(DesktopRect.bottom/2)-(BROWSE_HEIGHT/2), BROWSE_WIDTH, BROWSE_HEIGHT, TRUE);
     
      //Find the listview
      HWND hListView = FindWindowEx(p_hWnd,NULL,"SysTreeView32",NULL);
     
      //Find the handles of OK and Cancel buttons
      HWND hOKButton     = FindWindowEx(p_hWnd, NULL, "Button", NULL);
      HWND hCancelButton = FindWindowEx(p_hWnd, OKButton, "Button", NULL);     
     
      //Gets the OK button position
      ::GetClientRect(hOKButton,&rtButton);
     
      //Gets the dimensions BrowseForFolder dialog
      ::GetClientRect(p_hWnd,&rtDialog);
     
      //Relocate the SysTreeView32 control
      ::SetWindowPos( hListView,0,10 , 43, 360,300,0);
     
      //Relocate the OK button
      ::SetWindowPos( hOKButton, 0, 230,460,60,30,0);
     
      //Relocate the Cancel buttton
      ::SetWindowPos( hCancelButton, 0,300,460,60,30,0);
     
      //Create a static control
      HWND hMyStaticCtrl = CreateWindowEx( 0,"STATIC","My Custom Controls",SS_NOTIFY|WS_CHILD|WS_VISIBLE ,20,370,200,25,p_hWnd,0,NULL,NULL);
     
      //Create a radio button
      HWND hMyRaioBtn = CreateWindowEx( 0,"BUTTON","RadioButton",SS_NOTIFY|WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,20,400,200,20,p_hWnd,0,NULL,NULL);
     
      //Create an Edit box
      g_hMyEditBox = CreateWindowEx( 0,"EDIT","",SS_NOTIFY|WS_CHILD|WS_VISIBLE|WS_BORDER,20,430,340,20,p_hWnd,0,NULL,NULL);
   }
   //Selection change message
   else if(BFFM_SELCHANGED == uMsg)
 {
      TCHAR szDir[MAX_PATH*2] = { 0 };
     
      // fail if non-filesystem
      BOOL bRet = SHGetPathFromIDList((LPITEMIDLIST) lParam, szDir);
     
      if(bRet)
      {
         // set the folder path to edit box
         FromHandle(g_hMyEditBox)->SetWindowText(szDir);
      }
   }
  
   return 1;
}

mynote2

 

 

BROWSEINFO structure Contains parameters for the SHBrowseForFolder function and receives information about the folder selected by the user.

Advertisements