Yes, you'll have to implement it entirely by hand. That's the drawback of not using a built-in control. It probably would have been a better idea to start off with a ListBox and then customize that to your desire. That way, you would get all of the scrolling, selection, and other logic for free.
The steps are roughly as follows (there are probably ATL/WTL idioms for some or all of these, but any ATL/WTL programmer can convert back and forth from raw Win32):
Add the
WS_HSCROLL
and/orWS_VSCROLL
window styles to your custom static control, depending on if you want a horizontal, vertical, or both scroll bars. You would add these to list of window styles passed in to the CreateWindow/CreateWindowEx function.By default, those scroll bars won't do anything at all. You need to tell them what to do using the
SetScrollInfo
function. In your case:- The first parameter (
hwnd
) would be the handle to your control window. - The second parameter (
fnBar
) should be eitherSB_HORZ
to adjust the horizontal scroll bar, orSB_VERT
to adjust the vertical scroll bar. - The third parameter (
lpsi
) is a pointer to aSCROLLINFO
structure, filled in with the desired scrolling parameters, including the current position of the thumb, the minimum and maximum values, and the "page" size used to set up the proportional scroll bar. - The fourth parameter (
fRedraw
) should probably be set toTRUE
.
- The first parameter (
You will also need the
EnableScrollBar
function to enable/disable the scroll bar as appropriate. Like the previous function,hwnd
is a handle to your control windowwSBflags
is eitherSB_HORZ
,SB_VERT
, orSB_BOTH
wArrows
is one of theESB_*
values, depending on what you want
Finally, you will want to write code in your custom control's window procedure to handle the
WM_HSCROLL
and/orWM_VSCROLL
messages. These are sent to the window whenever the scroll bar is moved. Inside of the handler for these messages, you will want to do the following things to update the control's state:- Call the
SetScrollInfo
function to update the thumb to its new position - Redraw the contents of your control in accordance with the scrolled distance. There are multiple ways of doing this, but I'd probably use the
ScrollWindowEx
function.
The custom control's window procedure will also need to handle the
WM_SIZE
message to update the scroll bar state (by callingSetScrollInfo
and/orEnableScrollBar
) in response to changes in the window's size.- Call the