views:

393

answers:

2

If you open Notepad, add ten lines and resize the editor to only show nine, the thumb track (the draggable block on a scroll bar) is almost the full length of the vertical scroll bar. If you add a couple of thousand rows, the thumb track is only a few pixels high.

I'd like to duplicate this bahaviour in a TCustomControl descendant where I implemented a horizontal scroll bar. So I added WS_HSCROLL to my window style (in CreateParams) and implemented a handler for WM_HSCROLL. Along the way, I use SetScrollRange and SetScrollPos to manage the range and the position of the thumb track - but the little bugger remains a near-perfect square.

What am I missing?

+5  A: 
Wouter van Nifterick
+1 so nice to have a visual! nice touch
Argalatyr
This is on scroll bar controls? I'm working with the scrollbars on the window.
Cobus Kruger
+3  A: 

The documentation recommends using SetScrollInfo rather than SetScrollRange and SetScrollPos.

As Wouter's answer points out, you also need to set the page size. The position and range tell the OS where the center of the thumb belongs, but the page size tells it how much of the range is visible, and that's what determines the size of the thumb. You have to use SetScrollInfo for that; as a bonus, it lets you set the position, range, and page size all at once.


TCustomControl differs from TWinControl in just one way: It has a canvas. TScrollingWinControl differs in just one way, too: It has scroll bars. Adding a canvas to a TScrollingWinControl descendant should be much easier than adding scroll bars to a TCustomControl descendant — it's less code to copy and paste from the VCL source code. Change your control's base class and then see where you are.

Even if that's not an option, you'd still do well to look at how TScrollingWinControl and TControlScrollBar work together.

Rob Kennedy
+1, but I'm not sure that inheriting from `TScrollingWinControl` is less work in the long run. At least on Win9X it also severely limits the available scroll range (16 bit coordinates). Even on the NT line it would limit number of lines to 2^31 `div` (line height) in pixels. I would rather use `SetScrollInfo()` and calculate everything in code.
mghie
I don't agree with the TScrollingWinControl part of your answer either, partly because my scrolling needs to be rather complex. But I used SetScrollInfo and it works a treat, thanks.
Cobus Kruger