How to send a scrollbar grip to the very top position with one single command?
Asked Answered
D

1

5

It is easy to send a control's scrollbar from the very bottom position to the very top position by sending a WM_VSCROLL message REPEATEDLY to the control:

ScrollBox1.Perform(WM_VSCROLL, MakeWParam(SB_PAGEUP, 0), 0);

enter image description here

But this requires knowing how many times I have to send the message to the control to set the scrollbar to the very top position, depending on the proportional size of the scrollbar grip in relation to the scrollbar's height. Alternatively, I can send the message repeatedly for an irrational amount of times which seems not being the best solution.

So how can I send the scrollbar to the very top with ONE SINGLE command independently from the proportional size of the scrollbar grip in relation to the scrollbar's height?

Discommon answered 5/12, 2016 at 11:42 Comment(3)
Why not set scrollbar position to zero?Turnstone
Thank you! Please post this as an answer so I can accept it.Discommon
In the question itself, I found an answer to my potential question which is a much needed thing - "how to scroll a TScrollbox programmatically i.e. from code?". Everywhere else you see the code that alters the scrollbox area and requires managing the viewport, scrollbar widths and what not!Alanson
T
15

To scroll the bar to the top:

ScrollBox1.VertScrollBar.Position := 0;

See Vcl.Forms.TControlScrollBar.Position


Or use the windows API WM_VSCROLL message:

Scroll to top:

ScrollBox1.Perform( WM_VSCROLL, MakeWParam(SB_Top,0),0);

Scroll to bottom:

ScrollBox1.Perform( WM_VSCROLL, MakeWParam(SB_Bottom,0),0);
Turnstone answered 5/12, 2016 at 11:55 Comment(1)
If the scrollbar belongs to a windowed control, you can use the Win32 API SetScrollPos() or SetScrollInfo() function.Alienor

© 2022 - 2024 — McMap. All rights reserved.