Recent

Author Topic: A reliable method to detect if the scrollbar is currently visible in a TreeView.  (Read 1345 times)

jamie

  • Hero Member
  • *****
  • Posts: 6090
basically as it states..

 I have this case where I have the bottom of a treeview anchored to its parent (a Form) and when sizing the parent the treeview can get taller than the number of items in the list showing and thus the scrollbar will disappear.

 At this point I want to limit the form's ability to grow any taller because it does not paint the background after that point. I am custom drawing all the items but using the provided coordinates supplied in the Item Draw events of course.

 There does not seem to be any direct way to determine if the scrollbars are showing etc.

 Also I tried the TVM_HITTEST by sending it a message, that does not work on my windows 10 PC for some reason.. ? I send the message to the control and it never populates the info record nor does it generate any errors ?
 
 In any case, anyone know how to determine if the scrollbar is showing ? in a TreeView ?

The only true wisdom is knowing you know nothing

paweld

  • Hero Member
  • *****
  • Posts: 970
for vertical scrollbar:
Code: Pascal  [Select][+][-]
  1. uses Windows;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   style: LongInt;
  6.   ScrollBarVisible: Boolean;
  7. begin
  8.   style := GetWindowLong(TreeView1.Handle, GWL_STYLE);
  9.   ScrollBarVisible := ((style and WS_VSCROLL) = 0)
  10. end;  
  11.  
  12. // or
  13.  
  14. procedure TForm1.Button1Click(Sender: TObject);
  15. var
  16.   Info: TScrollInfo;
  17.   ScrollBarVisible: Boolean;
  18. begin
  19.   Info.cbSize := SizeOf(Info);
  20.   Info.fMask := SIF_RANGE or SIF_PAGE;
  21.   Win32Check(GetScrollInfo(TreeView1.Handle, SB_VERT, Info));
  22.   ScrollBarVisible := (Info.nMin <> Info.nMax) and (Info.nPage <= Info.nMax);
  23. end;

unfortunately it always shows true for a horizontal scrollbar
Best regards / Pozdrawiam
paweld

jamie

  • Hero Member
  • *****
  • Posts: 6090
Your first example is to verify that the scrollbar was enabled at window creation time so that won't do.

The second , getscrollinfo is what I tried but the values comeup wrong for doing this.
The values report the scroll bar should not be showing but they still are.

The issue is the size of the arrow buttons etc.

I have comeup with an idea though and that is to measure the client width against that of the control border. They should be the same if no scrollbar is showing.
The only true wisdom is knowing you know nothing

paweld

  • Hero Member
  • *****
  • Posts: 970
in fact, the result of these functions differs depending on the setting of the ScrollBars property, but by combining both and the value of ScrollBars, you can work out a schema.
previously I only checked on ScrollBars := ssAutoBoth
Windows 10, Lazarus trunk, FPC 3.2.0

Video: https://drive.google.com/file/d/1yw8nPk3hdYUf0hgAW3mREP-KaZ10uuby/view?usp=sharing
Best regards / Pozdrawiam
paweld

jamie

  • Hero Member
  • *****
  • Posts: 6090
using the GetScrolinfo isn't reliable like I said, if you drag a window too fast you can get it much larger well after the scroll bars have disappeared and what happens you will get old data because it stops updating once the client area passes any child controls.

 So what I've done which seems to work is in the OnSize event or any event of that nature is to test the ClientWidth with the Control.Width to indicate if the vertical scrollbar is showing..

 If Control.Width-Control.ClientWidth < 5 Then its not showing..

 The less than 5 accounts for my border spaces etc..
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018