It’s a specialized edit control (MFC Feature Pack VS2008) with a browse button attached to its right side, when we click on this button, we get an open file dialog or an open folder dialog. See this sample screenshot.
Browse edit control sample
Also, this control allows us to implement our own event handling by overriding OnBrowse
function of this class. This is a cool control which I like a bit too much since I know how painful it is to get one going.
Some tips on how to make such a control…
- Handle
nc calcsize
event, so that you can specify size of the area that the browse button will take which in turn results in edit control resizing its client area to adjust the button.
- Handle
nc paint
to draw the button, also you have to force generate an nc calcsize
message for once in the beginning.
- Handle mouse up and mouse down event.
Usage
- You should be working in VS2008 SP1 or with Feature pack installation
- Add an edit control to a dialog
- Open .h file of this dialog’s class and add a member variable -
CMFCEditBrowseCtrl m_EditBrowse;
- Open .cpp file and add a call to sub class this item in
DoDataExchange
DDX_Control( pDX, IDC_EDIT_FILEBROWSE, m_EditBrowse);
- Then in
OnInitDialog
…
m_EditBrowse.EnableFileBrowseButton();
m_EditBrowse.EnableFolderBrowseButton();
m_EditBrowse.EnableBrowseButton();
- That’s it, now you’ve got the browse edit working.
- Note that the above calls take some parameters (which has default values) look up in MSDN.
Custom Event Handling for Browse Button
Here is a small sample on how to handle custom browse button event handling, taken from MSDN samples…
class CMyBrowseEdit : public CMFCEditBrowseCtrl
{
virtual void OnBrowse()
{
MessageBox(_T("Browse item..."));
SetWindowText(_T("New value!"));
}
};
More…
Use SetBrowseButtonImage
to change browse button image, also has an option to maintain such a bitmap or an icon. There are some more virtual functions which could be useful…
OnDrawBrowseButton
- Override for some additional painting from your side
OnChangeLayout
- Called when browse button mode changes, i.e. from folder to file mode, etc. (I guess so)
Posted in CodeProject, MFC Tagged: CEdit, CMFCEditBrowseCtrl, Extending edit controls, MFC Feature Pack