Click here to Skip to main content
15,893,923 members
Articles / Desktop Programming / MFC

Customizing the "Browse for folder" dialog

Rate me:
Please Sign up or sign in to vote.
3.47/5 (12 votes)
20 Apr 20032 min read 131.2K   4K   30  
This Program customizes the "Browse For folder dialog" with a few lines of code...
////////////////////////////////////////////////////////////////////////////////////
//             The Program to customize the "Browse for folder" dialog.           //
//                                                                                //
//                                                                                //
// Date : 20/4/2003 (dd/mm/yy).                                                   //
//                                                                                //
// By : Yogesh Madhukarrao Joshi. (Engg Second Year I.T).                         //
//      Aditya Engineering College, Beed.                                         //
//      Pin Code - 431122.                                                        //
//      (State : Maharashtra) India.                                              //
//                                                                                //
// Mail Me at : yogmj@hotmail.com                                                 //
//                                                                                //
// visit my website for the COOL software programs at:                            //
//                                                                                //
// http://www.stechome.netfirms.com                                               //
//                                                                                //
////////////////////////////////////////////////////////////////////////////////////

//The program below customize the "Browse for folder" dialog.

#include<windows.h>
#include<windowsx.h>
#include<commctrl.h>
#include<shlobj.h>

#include "resource.h"

//Handles for the windows
HWND hdialog;
HINSTANCE ghInstance;
HWND edit;
//Folder Name that has been selected
TCHAR FolderName[MAX_PATH];
TCHAR c[MAX_PATH];
//Return value of the SHGetPathFromIDList
BOOL t;


//This will set the font of the controls
void SetFont(HWND hwnd,LPTSTR FontName,int FontSize)
{
	
	HFONT hf;
	LOGFONT lf={0};
	HDC hdc=GetDC(hwnd);
	
	GetObject(GetWindowFont(hwnd),sizeof(lf),&lf);
	lf.lfWeight = FW_REGULAR;
	lf.lfHeight = (LONG)FontSize;
	lstrcpy( lf.lfFaceName, FontName );
	hf=CreateFontIndirect(&lf);
	SetBkMode(hdc,OPAQUE);
	SendMessage(hwnd,WM_SETFONT,(WPARAM)hf,TRUE);
	ReleaseDC(hwnd,hdc);
   
}

//Browse dialog callback procedure
int __stdcall BrowseCallbackProc(HWND  hwnd,UINT  uMsg,LPARAM  lParam,LPARAM  lpData)
{

	//Initialization callback message
	if(uMsg==BFFM_INITIALIZED)
	{
		//Rectangles for getting the positions
		RECT ListViewRect,Dialog;
		//Create the edit and static control on the dialog box
		edit=CreateWindowEx(0,"EDIT","Yogesh M Joshi.",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,0,100,100,50,hwnd,0,ghInstance,NULL);
		HWND caption=CreateWindowEx(0,"STATIC","You have selected the folder :",WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN,0,100,100,50,hwnd,0,ghInstance,NULL);
		HWND ListView=FindWindowEx(hwnd,NULL,"SysTreeView32",NULL);

		//Gets the dimentions of the windows
		GetWindowRect(hwnd,&Dialog);
		GetWindowRect(ListView,&ListViewRect);

		//Sets the listview controls dimentions
		SetWindowPos(ListView,0,(ListViewRect.left-Dialog.left) ,(ListViewRect.top-Dialog.top )-20,290,170,0);
		//Sets the window positions of edit and dialog controls
		SetWindowPos(edit,HWND_BOTTOM,(ListViewRect.left-Dialog.left),(ListViewRect.top-Dialog.top )+170,290,18,SWP_SHOWWINDOW);
		SetWindowPos(caption,HWND_BOTTOM,(ListViewRect.left-Dialog.left),(ListViewRect.top-Dialog.top )+155,290,14,SWP_SHOWWINDOW);

		//Sets the fonts of edit and static controls
		SetFont(caption,"MS Sans Serif",12);
		SetFont(edit,"MS Sans Serif",12);
		
	}

	//Selection change message
	if(uMsg==BFFM_SELCHANGED)
	{
		t = SHGetPathFromIDList((ITEMIDLIST*)lParam, c);

		//Sets the text of the edit control to the current folder
		SetWindowText(edit,c);
		
    }
	
	return 0;
}

//Callback of the main window
BOOL CALLBACK MainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	if(message==WM_QUIT||message==WM_CLOSE)
		PostQuitMessage(0);
		

	if(message==WM_INITDIALOG)
	{
		hdialog=hDlg;
		
		RECT rc;

		GetWindowRect(hDlg,&rc); 
		int w=rc.right-rc.left, h=rc.bottom-rc.top;
		int cx=GetSystemMetrics(SM_CXSCREEN)/2, cy=GetSystemMetrics(SM_CYSCREEN)/2;
		MoveWindow(hDlg,cx-w/2,cy-h/2,w,h,FALSE);

	}

	if(message==WM_COMMAND)
	{
		if((LOWORD(wParam))==IDOK)
		{
			TCHAR dname[MAX_PATH];
			IMalloc *imalloc; SHGetMalloc(&imalloc);
			BROWSEINFO bi; ZeroMemory(&bi,sizeof(bi));
			bi.hwndOwner=hDlg;
			bi.pszDisplayName=dname;
			bi.lpszTitle = TEXT("This sample will illustrate the customization look at the edit control showing path below...");
			
			#define BIF_NONEWFOLDERBUTTON  0x0200
			
			bi.ulFlags = BIF_NONEWFOLDERBUTTON|BIF_RETURNONLYFSDIRS;
			bi.lpfn = BrowseCallbackProc;
			ITEMIDLIST *pidl = SHBrowseForFolder(&bi);

			//Displays the selected folder
			if (pidl!=NULL)
				MessageBox(hDlg,c,"You have selected the folder",0);
			
			imalloc->Free(pidl);
			imalloc->Release();

		}

		if((LOWORD(wParam))==IDCANCEL)
		{
			PostQuitMessage(0);
			EndDialog(hDlg,0);
		}
	}
			
	return 0;

}

//Main window
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	InitCommonControls();
	ghInstance=hInstance;
	DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,MainDlgProc);
	return 0;
	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer AVAYA India Pvt Ltd.
India India
I have completed my B.E. Degree in IT form Aditya Engineering College, Beed. (Maharashtra) India.

I have completed Diploma In Advanced Computing (DAC) in Feb 07.

Now I am working for AVAYA India Pvt. LTD as software engineer.
Platform : C/C++, Solaris 10, AIX and Windows

Comments and Discussions