What is a split button?
In the previous post we presented the command link button control that is one of the controls introduced with Windows Vista. Another such control is the split button. This is a button that:
- act like a regular button when pressed, but
- display a drop-down menu when its drop-down arrow is presses
This is actually a regular button that has one of the windows styles BS_SPLITBUTTON or BS_DEFSPLITBUTTON set.
How do add a split button
If you work with Microsoft Visual Studio 2008 or newer then you can find “Split Button Control” in the resource editor toolbox. First step is to drag it from toolbox in the resource dialog template.
You can set the caption and the handler for BN_CLICKED just like with any regular push button.
1 2 3 4 |
void CSplitButton2008Dlg::OnBnClickedSplitCodexpert() { ShellExecute(NULL, _T("open"), _T("http://www.codexpert.ro"), NULL, NULL, SW_SHOWNORMAL); } |
However, for the drop down menu you have to define it explicitly.
To specify what is the menu to be displayed use SetDropDownMenu.
1 |
m_buttonCodexpert.SetDropDownMenu(IDR_MENU_CODEXPERT, 0); |
Then, add handlers for the menu items and handle the commands.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
BEGIN_MESSAGE_MAP(CSplitButton2008Dlg, CDialog) /* ... */ ON_COMMAND(ID_CODEXPERT_FORUM, &CSplitButton2008Dlg::OnCodexpertForum) ON_COMMAND(ID_CODEXPERT_BLOG, &CSplitButton2008Dlg::OnCodexpertBlog) ON_COMMAND(ID_CODEXPERT_ARTICLES, &CSplitButton2008Dlg::OnCodexpertArticles) ON_COMMAND(ID_CODEXPERT_RESOURCES, &CSplitButton2008Dlg::OnCodexpertResources) END_MESSAGE_MAP() void CSplitButton2008Dlg::OnCodexpertForum() { ShellExecute(NULL, _T("open"), _T("http://www.codexpert.ro/forum"), NULL, NULL, SW_SHOWNORMAL); } void CSplitButton2008Dlg::OnCodexpertBlog() { ShellExecute(NULL, _T("open"), _T("http://www.codexpert.ro/blog"), NULL, NULL, SW_SHOWNORMAL); } void CSplitButton2008Dlg::OnCodexpertArticles() { ShellExecute(NULL, _T("open"), _T("http://www.codexpert.ro/articole.php"), NULL, NULL, SW_SHOWNORMAL); } void CSplitButton2008Dlg::OnCodexpertResources() { ShellExecute(NULL, _T("open"), _T("http://www.codexpert.ro/resurse.php"), NULL, NULL, SW_SHOWNORMAL); } |
References
Downloads
- Visual Studio 2008 – Split Button Demo (1803)