Share via


Comment créer un contrôle List-View

Cette rubrique montre comment créer un contrôle list-view. Pour créer un contrôle d’affichage de liste, vous utilisez la fonction CreateWindow ou CreateWindowEx et spécifiez la classe de fenêtre WC_LISTVIEW .

Un contrôle d’affichage de liste peut également être créé dans le cadre d’un modèle de boîte de dialogue. Vous devez spécifier WC_LISTVIEW comme nom de classe. Pour utiliser un contrôle d’affichage de liste dans le cadre d’un modèle de boîte de dialogue, vous devez appeler InitCommonControls ou InitCommonControlsEx avant de créer un instance de la boîte de dialogue.

Bon à savoir

Technologies

Prérequis

  • C/C++
  • Programmation de l’interface utilisateur Windows

Instructions

Inscrivez d’abord la classe window en appelant la fonction InitCommonControlsEx et en spécifiant le bit ICC_LISTVIEW_CLASSES dans la structure INITCOMMONCONTROLSEX associée. Cela garantit que la DLL de contrôles courants est chargée. Ensuite, utilisez la fonction CreateWindow ou CreateWindowEx et spécifiez la classe de fenêtre WC_LISTVIEW .

Notes

Par défaut, un contrôle d’affichage de liste utilise la police de titre de l’icône. Toutefois, vous pouvez utiliser un message WM_SETFONT pour spécifier la police à utiliser pour le texte. Vous devez envoyer ce message avant d’insérer des éléments. Le contrôle utilise les dimensions de la police spécifiées par le message WM_SETFONT pour déterminer l’espacement et la disposition. Vous pouvez également personnaliser la police pour chaque élément. Pour plus d’informations, consultez Dessin personnalisé.

 

L’exemple de code C++ suivant crée un contrôle list-view en mode rapport.

// CreateListView: Creates a list-view control in report view.
// Returns the handle to the new control
// TO DO:  The calling procedure should determine whether the handle is NULL, in case 
// of an error in creation.
//
// HINST hInst: The global handle to the applicadtion instance.
// HWND  hWndParent: The handle to the control's parent window. 
//
HWND CreateListView (HWND hwndParent) 
{
    INITCOMMONCONTROLSEX icex;           // Structure for control initialization.
    icex.dwICC = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icex);

    RECT rcClient;                       // The parent window's client area.

    GetClientRect (hwndParent, &rcClient); 

    // Create the list-view window in report view with label editing enabled.
    HWND hWndListView = CreateWindow(WC_LISTVIEW, 
                                     L"",
                                     WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
                                     0, 0,
                                     rcClient.right - rcClient.left,
                                     rcClient.bottom - rcClient.top,
                                     hwndParent,
                                     (HMENU)IDM_CODE_SAMPLES,
                                     g_hInst,
                                     NULL); 

    return (hWndListView);
}

En règle générale, les applications d’affichage de liste permettent à l’utilisateur de passer d’une vue à l’autre.

L’exemple de code C++ suivant modifie le style de fenêtre de l’affichage de liste, ce qui à son tour modifie la vue.

// SetView: Sets a list-view's window style to change the view.
// hWndListView: A handle to the list-view control. 
// dwView:       A value specifying the new view style.
//
VOID SetView(HWND hWndListView, DWORD dwView) 
{ 
    // Retrieve the current window style. 
    DWORD dwStyle = GetWindowLong(hWndListView, GWL_STYLE); 
    
    // Set the window style only if the view bits changed.
    if ((dwStyle & LVS_TYPEMASK) != dwView) 
    {
        SetWindowLong(hWndListView,
                      GWL_STYLE,
                      (dwStyle & ~LVS_TYPEMASK) | dwView);
    }               // Logical OR'ing of dwView with the result of 
}                   // a bitwise AND between dwStyle and 
                    // the Unary complenent of LVS_TYPEMASK.

Informations de référence sur les contrôles List-View

À propos des contrôles List-View

Utilisation de contrôles List-View