45.2.1 Creating Scroll Bars

You can add standard scroll bars to an overlapped, pop-up, or child window by specifying the WS_HSCROLL style, the WS_VSCROLL style, or both styles when creating the window by using the CreateWindowEx function. This adds a horizontal or vertical scroll bar, or both, to the window. The following example creates a window with standard horizontal and vertical scroll bars:

hwnd = CreateWindowEx(

0L, /* no extended styles */

"MyAppClass", /* window class */

"Scroll Bar Application", /* text for window title bar */

WS_OVERLAPPEDWINDOW | /* window styles */

WS_HSCROLL |

WS_VSCROLL,

CW_USEDEFAULT, /* default horizontal position */

CW_USEDEFAULT, /* default vertical position */

CW_USEDEFAULT, /* default width */

CW_USEDEFAULT, /* default height */

(HWND) NULL, /* overlapped windows have no parent */

(HWND) NULL, /* uses window class menu */

hinst, /* this instance owns this window */

(LPVOID) NULL /* pointer not needed */

);

The horizontal and vertical scroll bars included with the new window will be visible, but you must include code to process scroll bar messages in the main window procedure before scrolling will occur.

You can create a scroll bar control by specifying the SCROLLBAR window class when creating the window by using the CreateWindowEx function. This creates a horizontal or vertical scroll bar, depending on whether SBS_VERT or SBS_HORZ is specified as the window style. You specify the scroll bar's size and position. The position is relative to the scroll bar control's parent window. The following example creates a horizontal scroll bar control and positions it in the upper-right corner of the window identified by hwnd:

hwndScroll = CreateWindowEx(

0L, /* no extended styles */

"SCROLLBAR", /* scroll bar control class */

(LPSTR) NULL, /* text for window title bar */

WS_CHILD | SBS_HORZ, /* scroll bar styles */

0, /* horizontal position */

0, /* vertical position */

200, /* width of the scrollbar */

CW_USEDEFAULT, /* default height */

hwnd, /* handle of main window */

(HWND) NULL, /* no menu for a scroll bar */

hinst, /* this instance owns this window */

(LPVOID) NULL /* pointer not needed */

);