Condividi tramite


Come gestire i pulsanti a discesa

Un pulsante a discesa può presentare agli utenti un elenco di opzioni. Per creare questo stile di pulsante, specificare lo stile di BTNS_DROPDOWN (detto anche TBSTYLE_DROPDOWN per la compatibilità con le versioni precedenti dei controlli comuni). Per visualizzare un pulsante a discesa con una freccia, è necessario impostare anche lo stile della barra degli strumenti TBSTYLE_EX_DRAWDDARROWS inviando un messaggio TB_edizione Standard TEXTENDEDSTYLE.

La figura seguente mostra un pulsante a discesa "Apri" con il menu di scelta rapida aperto e che mostra un elenco di file. In questo esempio la barra degli strumenti ha lo stile 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

La figura seguente mostra la stessa barra degli strumenti, questa volta senza lo stile TBSTYLE_EX_DRAWDDARROWS.

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

Quando gli utenti fanno clic su un pulsante della barra degli strumenti che utilizza lo stile BTNS_DROPDOWN, il controllo barra degli strumenti invia alla finestra padre un codice di notifica TBN_DROPDOWN.

Informazioni importanti

Tecnologie

Prerequisiti

  • C/C++
  • Programmazione dell'interfaccia utente di Windows

Istruzioni

Gestire un pulsante a discesa

Nell'esempio di codice seguente viene illustrato come un'applicazione può supportare un pulsante a discesa in un controllo barra degli strumenti.

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;
}

Uso dei controlli barra degli strumenti

Demo dei controlli comuni di Windows (CppWindowsCommonControls)