5
votes

A. Create a Delphi VCL Forms application.

B. Put a TTreeView on the form, name it tvTest and fill it with items and set the size of the Treeview, so scrollbars are visible on the TreeView, for example:

enter image description here

C. Put a button on the form and in its click handler write this code:

  procedure TForm1.btnScrollClick(Sender: TObject);
  begin
    tvTest.ScrollBy(tvTest.Width, 0);
  end;

D. Now run the program and click the button. Supposedly the horizontal scrollbar should scroll from left to right. But nothing happens. Why?

So how can I make the scrollbars (and with the scrollbars of course the content) scroll from left to right, from right to left, down or up?

Delphi 10.1 Berlin Update 2
Windows 7 x64 SP1

EDIT: When I use this code (similar to Sami's suggestion):

tvTest.ScrollBy(-3, -3);

...I get this piece of modern art:

enter image description here

1
try tvTest.ScrollBy(3, 3); and click the button for 5 time you will see why - Ilyes
This makes only the CONTENT of the TreeView scroll, not the scrollbars. But after having clicked the button a few times, I dragged the form partially outside of the screen area and when I dragged it back the TreeView content was again where it was before. So the TreeView was not really scrolled, but only it SEEMED scrolled and when repainted it was like before!! Why? And how can I really scroll the Treeview? - user1580348

1 Answers

6
votes

To scroll a TreeView send it (or Perform) WM_VSCROLL and/or WM_HSCROLL messages.

tvTest.Perform(WM_HSCROLL, MakeWParam(SB_LINERIGHT, 0), 0);

or

tvTest.Perform(WM_VSCROLL, MakeWParam(SB_LINEDOWN, 0), 0);

See the corresponding documentations of messages for parameters.

ScrollBy is VCL's wrapper for ScrollWindow API, it shifts the contents of a control. It's a shortcut to paint a part of the client of a control that is scrolled, revealed (empty) parts should be additionally painted. It is normally used by internal implementation of a control. Not what you're looking for.