Cómo crear barras de desplazamiento

Al crear una ventana emergente o secundaria superpuesta, puede agregar barras de desplazamiento estándar mediante la función CreateWindowEx y especificar WS_HSCROLL, WS_VSCROLL o ambos estilos.

Lo que necesita saber

Tecnologías

Requisitos previos

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

Instrucciones

Crear una barra de desplazamiento

En el ejemplo siguiente se crea una ventana con barras de desplazamiento horizontales y verticales estándar.

    hwnd = CreateWindowEx( 
        0,                     // no extended styles 
        g_szWindowClass,       // global string containing name of window class
        g_szTitle,             // global string containing title bar text 
        WS_OVERLAPPEDWINDOW |  
            WS_HSCROLL | WS_VSCROLL, // window styles 
        CW_USEDEFAULT,         // default horizontal position 
        CW_USEDEFAULT,         // default vertical position 
        CW_USEDEFAULT,         // default width 
        CW_USEDEFAULT,         // default height 
        (HWND) NULL,           // no parent for overlapped windows 
        (HMENU) NULL,          // use the window class menu 
        g_hInst,               // global instance handle  
        (PVOID) NULL           // pointer not needed 
    ); 

Para procesar mensajes de barra de desplazamiento para estas barras de desplazamiento, debe incluir el código adecuado en el procedimiento de ventana principal.

Puede usar la función CreateWindowEx para crear una barra de desplazamiento especificando la clase de ventana SCROLLBAR. Esto crea una barra de desplazamiento horizontal o vertical, en función de si SBS_HORZ o SBS_VERT se especifica como estilo de ventana. También se puede especificar el tamaño de la barra de desplazamiento y su posición con respecto a su ventana primaria.

En el ejemplo siguiente se crea una barra de desplazamiento horizontal que se coloca a lo largo de la parte inferior del área cliente de la ventana primaria.

// Description:
//   Creates a horizontal scroll bar along the bottom of the parent 
//   window's area.
// Parameters:
//   hwndParent - handle to the parent window.
//   sbHeight - height, in pixels, of the scroll bar.
// Returns:
//   The handle to the scroll bar.
HWND CreateAHorizontalScrollBar(HWND hwndParent, int sbHeight)
{
    RECT rect;

    // Get the dimensions of the parent window's client area;
    if (!GetClientRect(hwndParent, &rect))
        return NULL;

    // Create the scroll bar.
    return (CreateWindowEx( 
            0,                      // no extended styles 
            L"SCROLLBAR",           // scroll bar control class 
            (PTSTR) NULL,           // no window text 
            WS_CHILD | WS_VISIBLE   // window styles  
                | SBS_HORZ,         // horizontal scroll bar style 
            rect.left,              // horizontal position 
            rect.bottom - sbHeight, // vertical position 
            rect.right,             // width of the scroll bar 
            sbHeight,               // height of the scroll bar
            hwndParent,             // handle to main window 
            (HMENU) NULL,           // no menu 
            g_hInst,                // instance owning this window 
            (PVOID) NULL            // pointer not needed 
        )); 
}

Usar barras de desplazamiento

Demostración de controles comunes de Windows (CppWindowsCommonControls)