How to Create a drop down button in Windows Vista, 7?

MenuIn this post, I’m introducing the new button style which is introduced in Windows Vista and above. It’s no more painful to create a Drop down button with Windows Vista, earlier it was accomplished by placing two controls side by side and handle it’s message separately.

Windows Vista introduced new button style BS_SPLITBUTTON and you can set this new style by specifying as parameter of CreateWindow API if you’re dynamically creating button control or by modifying the style using ModifyStyle API. To use this style Windows Vista or higher required .


Using with CreateWindow/CButton::Create API

[sourcecode language='cpp']
HWND hWndButtonMain = CreateWindow(_T(“Button”), _T(“Split Button”),
WS_CHILD | WS_VISIBLE | BS_SPLITBUTTON,
10, 10, 120, 50,
m_hWnd, (HMENU)IDC_BTN_BUTTON1, AfxGetApp()->m_hInstance, NULL);
[/sourcecode]

Or

[sourcecode language='cpp']
CButton* pButton = new CButton;
pButton->Create( _T( “Split Button”), WS_CHILD | WS_VISIBLE | BS_SPLITBUTTON,CRect(10,10,120,50),this,IDC_BUTTON1);
[/sourcecode]

Or modifying Existing control

[sourcecode language='cpp']
m_SplitButton.ModifyStyle(0, BS_SPLITBUTTON );
[/sourcecode]

Handling Dropdown Event

Add message handler for BCN_DROPDOWN in your message handler or in message map of your MFC window class

[sourcecode language='cpp']
ON_NOTIFY(BCN_DROPDOWN, IDC_BUTTON1, &CControlSampleDlg::OnBnDropDownButton1)
[/sourcecode]

In the message Handler, Display the popup menu and the menu item can be handled using ON_COMMAND message with associated menu ID.

[sourcecode language='cpp']
void CControlSampleDlg::OnBnDropDownButton1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMBCDROPDOWN pDropDown = reinterpret_cast(pNMHDR);

Button_SetDropDownState( m_SplitButton, TRUE );

RECT rc;

// Get the bounding rectangle of the client area
::GetWindowRect(m_SplitButton, &rc);

HMENU hmenu = LoadMenu(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_MENU1));
HMENU hmenuTrackPopup;

hmenuTrackPopup = GetSubMenu(hmenu, 0);
TrackPopupMenuEx(hmenuTrackPopup, TPM_RIGHTBUTTON, rc.left, rc.bottom, m_hWnd, NULL);
DestroyMenu(hmenu);

Button_SetDropDownState( m_SplitButton, FALSE );
*pResult = 0;
}
[/sourcecode]

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Reju
    Posted June 1, 2009 at 10:43 pm | Permalink

    Hi Sarat,
    I am new to windows programming and came across your blog through this post. I was searching for a way to click the “Organize” split button in the windows explorer in Vista and then click on the menu items Layout;Navigation pane. I need to do this in VB script. I was unable to make the GetMenu or GetSubMenu functions to work… would you be able to guide me…appreciate your help

    Thanks,
    Reju

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>