How to implement the Drop-down arrow button for a toolbar

Source: Internet
Author: User

Some people in the forum have asked questions like: How to implement a button with a drop-down arrow in the C++/MFC program similar to IE toolbar, as shown in figure

Figure A button with a drop-down arrow in the IE toolbar

This article will show you how to do this, but it's simple:

1, a new MFC/SDI project, all the way to take the default settings.

2, find the CMainFrame::OnCreate () frame window creation function in the Mainframe.cpp file. Add the following line of code at the end:

DWORD dwExStyle = TBSTYLE_EX_DRAWDDARROWS;
   m_wndToolBar.GetToolBarCtrl().SendMessage(TB_SETEXTENDEDSTYLE, 0, (LPARAM)dwExStyle);

The two lines of code are used to enable the toolbar to handle the Drop-down arrows, and then use the SetButtonStyle () method to add a Drop-down arrow button to the selected place, and in the example program, the Drop-down arrow button is added to the File Open menu:

DWORD dwStyle = m_wndToolBar.GetButtonStyle(m_wndToolBar.CommandToIndex(ID_FILE_OPEN));
   dwStyle |= TBSTYLE_DROPDOWN;
   m_wndToolBar.SetButtonStyle(m_wndToolBar.CommandToIndex(ID_FILE_OPEN), dwStyle);

Once you've added the line, compile the program, and then run the program, and you'll see the drop-down arrow. But it's not working at this point.

3, the next thing to do is to add the Drop-down Arrow message processing code and the program to use the menu resources. Now assume that you have created the menu resource and assume that the resource ID for this menu is idr_menu1. As shown in Figure II.

Figure II The newly created menu resource idr_menu1

A TBN_DROPDOWN message map that adds a drop-down arrow to the message map of the CMainFrame class:

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
    //{{AFX_MSG_MAP(CMainFrame)
    ...
    ON_NOTIFY(TBN_DROPDOWN, AFX_IDW_TOOLBAR, OnToolbarDropDown)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

4, in the MainFrame.h header file to add a message processing function declaration:

//{{AFX_MSG(CMainFrame)
    ...
    afx_msg void OnToolbarDropDown(NMTOOLBAR* pnmh, LRESULT* plRes);
    //}}AFX_MSG

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.