如何处理下拉按钮

下拉按钮可为用户提供一个选项列表。 要创建这种样式的按钮,请指定 BTNS_DROPDOWN 样式(也称为 TBSTYLE_DROPDOWN,以便与以前版本的公共控件兼容)。 要显示带箭头的下拉按钮,还必须通过发送 TB_SETEXTENDEDSTYLE 消息来设置 TBSTYLE_EX_DRAWDDARROWS 工具栏样式。

下图显示一个下拉“打开”按钮,其中上下文菜单打开并显示文件列表。 在此示例中,工具栏具有 TBSTYLE_EX_DRAWDDARROWS 样式。

screen shot of a dialog box with three toolbar items represented by icons; one has an expanded drop-down arrow and a three-item context menu

下图显示同一工具栏,但不具有 TBSTYLE_EX_DRAWDDARROWS 样式。

screen shot of a previous dialog box, but the icon with the context menu has no dropdown arrow

当用户单击使用 BTNS_DROPDOWN 样式的工具栏按钮时,工具栏控件会向其父窗口发送 TBN_DROPDOWN 通知代码。

需要了解的事项

技术

先决条件

  • C/C++
  • Windows 用户界面编程

说明

处理下拉按钮

下面的代码示例演示应用程序如何支持工具栏控件中的下拉按钮。

BOOL DoNotify(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    #define lpnm   ((LPNMHDR)lParam)
    #define lpnmTB ((LPNMTOOLBAR)lParam)

    switch(lpnm->code)
    {
        case TBN_DROPDOWN:
        {
            // Get the coordinates of the button.
            RECT rc;
            SendMessage(lpnmTB->hdr.hwndFrom, TB_GETRECT, (WPARAM)lpnmTB->iItem, (LPARAM)&rc);

            // Convert to screen coordinates.            
            MapWindowPoints(lpnmTB->hdr.hwndFrom, HWND_DESKTOP, (LPPOINT)&rc, 2);                         
        
            // Get the menu.
            HMENU hMenuLoaded = LoadMenu(g_hinst, MAKEINTRESOURCE(IDR_POPUP)); 
         
            // Get the submenu for the first menu item.
            HMENU hPopupMenu = GetSubMenu(hMenuLoaded, 0);

            // Set up the pop-up menu.
            // In case the toolbar is too close to the bottom of the screen, 
            // set rcExclude equal to the button rectangle and the menu will appear above 
            // the button, and not below it.
         
            TPMPARAMS tpm;
         
            tpm.cbSize    = sizeof(TPMPARAMS);
            tpm.rcExclude = rc;
         
            // Show the menu and wait for input. 
            // If the user selects an item, its WM_COMMAND is sent.
         
            TrackPopupMenuEx(hPopupMenu, 
                             TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL, 
                             rc.left, rc.bottom, g_hwndMain, &tpm);

            DestroyMenu(hMenuLoaded);
         
        return (FALSE);
      
        }
    }
   
    return FALSE;
}

使用工具栏控件

Windows 通用控件演示 (CppWindowsCommonControls)