Compartir a través de


Cómo crear un control de List-View

En este tema se muestra cómo crear un control de vista de lista. Para crear un control de vista de lista, use la función CreateWindow o CreateWindowEx y especifique la clase de ventana WC_LISTVIEW .

También se puede crear un control de vista de lista como parte de una plantilla de cuadro de diálogo. Debe especificar WC_LISTVIEW como nombre de clase. Para usar un control de vista de lista como parte de una plantilla de cuadro de diálogo, debe llamar a InitCommonControls o InitCommonControlsEx antes de crear una instancia del cuadro de diálogo.

Lo que necesita saber

Tecnologías

Requisitos previos

  • C/C++
  • Programación de la interfaz de usuario de Windows

Instrucciones

En primer lugar, registre la clase de ventana llamando a la función InitCommonControlsEx y especificando el bit de ICC_LISTVIEW_CLASSES en la estructura INITCOMMONCONTROLSEX correspondiente. Esto garantiza que se cargue el archivo DLL de controles comunes. A continuación, use la función CreateWindow o CreateWindowEx y especifique la clase de ventana WC_LISTVIEW .

Nota

De forma predeterminada, un control de vista de lista usa la fuente de título del icono. Sin embargo, puede usar un mensaje de WM_SETFONT para especificar la fuente que se usará para el texto. Debe enviar este mensaje antes de insertar los elementos. El control usa las dimensiones de la fuente especificada por el mensaje WM_SETFONT para determinar el espaciado y el diseño. También puede personalizar la fuente de cada elemento. Para obtener más información, consulte Dibujo personalizado.

 

En el siguiente ejemplo de código de C++ se crea un control de vista de lista en la vista de informe.

// 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 application 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);
}

Normalmente, las aplicaciones de vista de lista permiten al usuario cambiar de una vista a otra.

En el siguiente ejemplo de código de C++ se cambia el estilo de ventana de la vista de lista, que a su vez cambia la vista.

// 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 complement of LVS_TYPEMASK.

Referencia de control de vista de lista

Acerca de los controles de List-View

Uso de controles de List-View