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 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 di TB_SETEXTENDEDSTYLE.

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.

schermata di una finestra di dialogo con tre elementi della barra degli strumenti rappresentati dalle icone; uno ha una freccia a discesa espansa e un menu di scelta rapida a tre voci

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

schermata di una finestra di dialogo precedente, ma l'icona con il menu di scelta rapida non dispone di una freccia a discesa

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

Cosa è necessario sapere

Tecnologie

Prerequisiti

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

Disposizioni

Gestire un pulsante a tendina

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 della barra degli strumenti

demo dei controlli comuni di Windows (CppWindowsCommonControls)