Question

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?

Was it helpful?

Solution

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):

  1. 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.

  2. 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 either SB_HORZ to adjust the horizontal scroll bar, or SB_VERT to adjust the vertical scroll bar.
    • The third parameter (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.
    • The fourth parameter (fRedraw) should probably be set to TRUE.

  3. 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 window
    • wSBflags is either SB_HORZ, SB_VERT, or SB_BOTH
    • wArrows is one of the ESB_* values, depending on what you want

  4. 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:

    • 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 calling SetScrollInfo and/or EnableScrollBar) in response to changes in the window's size.

OTHER TIPS

Cody Gray provided excellent introduction into adding support for scrolling, however what you also have is the help from the WTL itself.

WTL's atlscrl.h offers you classes to inherit from and implement a custom window/control with scrolling.

// Classes in this file:
//
// CScrollImpl<T>
// CScrollWindowImpl<T, TBase, TWinTraits>
// CMapScrollImpl<T>
// CMapScrollWindowImpl<T, TBase, TWinTraits>
// CFSBWindowT<TBase>
// CZoomScrollImpl<T>
// CZoomScrollWindowImpl<T, TBase, TWinTraits>
// CScrollContainerImpl<T, TBase, TWinTraits>
// CScrollContainer

Not so much code/snippets around to demo the use, but there is still one WTL sample that covers the basics and it should be a good starting point for you as well. \Samples\BmpView has a class for scrollable bitmap:

class CBitmapView : 
  public CScrollWindowImpl<CBitmapView>
{
public:

You will see it's really small and it covers most of the complexity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top