Creating Scroll Bars

When creating a frame window, you can add scroll bars by specifying the FCF_HORZSCROLL flag, FCF_VERTSCROLL flag, or both flags in the WinCreateStdWindow function. This adds horizontal, vertical, or both (as specified) scroll bars to the frame window. The frame window owns the scroll bars and passes notification messages from the scroll bars to the client window. The following code fragment adds scroll bars to a frame window:

    /* Set flags for a main window with scroll bars. */
    ULONG ulFrameControlFlags =
        FCF_STANDARD | FCF_HORZSCROLL | FCF_VERTSCROLL;

    /* Create the window.                            */
    hwndFrame = WinCreateStdWindow(HWND_DESKTOP,
        WS_VISIBLE,
        &ulFrameControlFlags,
        szClientClass,
        szFrameTitle,
        0,
        (HMODULE) NULL,
        0,
        &hwndClient);

Scroll bars created this way have the window identifier FID_HORZSCROLL or FID_VERTSCROLL. To determine the size and position of the scroll bars, the frame window uses the standard size specified by the system values SV_CXVSCROLL and SV_CYHSCROLL. The position always is defined by the right and bottom edges of the frame window.

Another way to create scroll bars is using the WinCreateWindow function. This method is most commonly used for stand-alone scroll bars. Creating scroll bars this way lets you set the size and position of the scroll bars. You also can specify which window should receive notification messages.

The following code fragment creates a stand-alone scroll bar:

    #define ID_SCROLL_BAR 1

    HWND hwndScroll,hwndClient;
    hwndScroll = WinCreateWindow(
        hwndClient,                      /* Scroll-bar parent window       */
        WC_SCROLLBAR,                    /* Preregistered scroll-bar class */
        (PSZ) NULL,                      /* No window title                */
        SBS_VERT | WS_VISIBLE,           /* Vertical style and visible     */
        10, 10,                          /* Position & Size                */
        20, 100,                         /* Size                           */
        hwndClient,                      /* Owner                          */
        HWND_TOP,                        /* Z-order position               */
        ID_SCROLL_BAR,                   /* Scroll-bar identifier          */
        NULL,                            /* No class-specific data         */
        NULL);                           /* No presentation parameters     */


[Back: Using Scroll Bars]
[Next: Retrieving a Scroll-Bar Handle]