Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll a TTreeView programmatically?

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

like image 380
user1580348 Avatar asked Nov 29 '16 20:11

user1580348


1 Answers

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.

like image 86
Sertac Akyuz Avatar answered Oct 12 '22 09:10

Sertac Akyuz