Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Customizing the "Browse for folder" dialog

0.00/5 (No votes)
20 Apr 2003 1  
This Program customizes the "Browse For folder dialog" with a few lines of code...

Sample Image - Customize_FolderDialog_1.jpg

Introduction

I was just working with WIN32 apis and I thought that when we use the browse for folder dialog boxes, at that time there is nothing that shows us the full path and so, I have written this program.

How It Works?

This code is fully based on the win32 apis. I am giving this functionality using the basic functions; let us discuss them one by one...

Logic

This program uses the simple win32 api function calls like , FindWindowEx, CreateWindow etc. With these functions we can create the edit box and the static control. The CreateWindowEx function is used to create the child controls that are one edit box and the one static control. Then just add it to the Browse for folder dialog.

Code

Firstly Declare the lpofn to the BorwseCallbackProc Procedure. Then the whenever the browse for folder dialog box initializes that time it will send the message to the procedure that is "BFFM_INITIALIZED". That time create the edit and the static controls like below...

//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);

Here the ghInstance is the instance of the dialog box. The "EDIT" and "STATIC" are the names of the classes. and The flags sets the style of the contols. Just then we will have to resize the list view contol but firstly we will find it on the "Browse For Folder" dialog, That's so easy just use the FindWindowEx function.

HWND ListView=FindWindowEx(hwnd,NULL,"SysTreeView32",NULL);

In this way we have find the handle of the List iew then resize it. Use the GetWindowRect function to get the rectangles of the "Browse For Folder" Dialog. This will take place in the following way...

//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,20,SWP_SHOWWINDOW);
SetWindowPos(caption,HWND_BOTTOM,(ListViewRect.left-Dialog.left),
            (ListViewRect.top-Dialog.top )+152,290,16,SWP_SHOWWINDOW);

And then I have used the SetFont function to set the fonts of the static and edit control.

//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);
   
}

In this way we have resized the contols. But main thing is not over that if we select any folder then the edit contol must show it. For this purpose the callback message "BFFM_SELCHANGED" and use the SetWindowText api to set the text of the edit contol.

//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);
        
}

That's all and in this way we can customize the "Browse For Folder" dialog box.

Conclusion

Use this code for educational purpose only. Mail your comments at : yogmj@hotmail.com and to pick some COOL windows utilities isit my homepage at http://www.stechome.netfirms.com/ I am waiting to hear from you.

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