I have created my own custom drawn list with checkboxesin WTL, I want to make it scrollable now, the thing is that I am subclassing a Static Text control over which I draw.. And I don't know if static controls support scrolling in any way.. Anyway my question is how do I make my custom made controll scrollable, do I have to impement the mechanism myself?
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/or WS_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:
hwnd
) would be the handle to your control window.fnBar
) should be either SB_HORZ
to adjust the horizontal scroll bar, or SB_VERT
to adjust the vertical scroll bar.lpsi
) is a pointer to a SCROLLINFO
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.fRedraw
) should probably be set to TRUE
.
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 either SB_HORZ
, SB_VERT
, or SB_BOTH
wArrows
is one of the ESB_*
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/or WM_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:
SetScrollInfo
function to update the thumb to its new positionScrollWindowEx
function.
The custom control's window procedure will also need to handle the WM_SIZE
message to update the scroll bar state (by calling SetScrollInfo
and/or EnableScrollBar
) in response to changes in the window's size.