How to find the real size ("logical area") of TScrollBox

I need to find the entire size (also called the "logical area") of the TScrollBox - as the opposite of the visible area that you get through the Width and Height property (or ClientWidth ClientHeight).

I want to create some controls inside this TScrollBox. One of them (called TViewer) should be as tall as the TScrollBox itself. The fact is that during creation, the TScrollBox scrolls down to show the last control created. Thus, using Top = 1 will not work, because my control will have top = 1, which is not the top of the logical area.


Delphi 7

+4
source share
6 answers
  • Drop the component, such as TLabel, on the TScrollBox.
  • Set the properties of the Left and Top component to 0.
  • Set the Visible component property to False.

Now you always have an origin. Logical Height now:

myScrollBox.Height + (myOriginControl.Top * -1); 
+2
source

Look at the scroll bars:

 ScrollBox1.VertScrollBar.Range ScrollBox1.HorzScrollBar.Range 

It can be less than the height and width if the logical scroll area is not larger than the physical area (in this case, the scroll bars are not visible)

Or use this to get the maximum value of both:

 var AHeight, AWidth: Integer; begin AHeight := Max(ScrollBox1.VertScrollBar.Range, ScrollBox1.Height); AWidth := Max(ScrollBox1.HorzScrollBar.Range, ScrollBox1.Width); ShowMessageFmt('%d,%d', [AHeight, AWidth]); end; 

Edit From @Altar's comments, I can add a logical height and / or width is not a problem. If you want to add any control that occupies the entire height of the scrollbar, use AHeight from the calculation above, but set Top to a negative VertScrollBar.Position element, something like this:

 procedure TForm2.Button3Click(Sender: TObject); var AHeight, AWidth: Integer; Btn: TButton; begin AHeight := Max(ScrollBox1.VertScrollBar.Range, Height); AWidth := Max(ScrollBox1.HorzScrollBar.Range, Width); Btn := TButton.Create(Self); Btn.Parent := ScrollBox1; Btn.Left := 15; Btn.Top := -ScrollBox1.VertScrollBar.Position; Btn.Height := AHeight; end; 
+2
source

Perhaps ScrollBox.HorzScrollBar.Range and ScrollBox.VertScrollBar.Range + the corresponding .Position is what you need.

+2
source

I'm not sure I understand exactly what you want to do, but to get the full area defined as “scrollable”, you have to write ScrollBox.HorScrollBar.Range + ScrollBox.Clientwidth (and the same for the vertical section). Scrolling always subtracts Visible page size of the total. Therefore, if you determine the height of 1000 pixels, displaying 100 pixels - it will have a scroll range of 900. You need to add to the client to get the rest.

In addition, to get the correct “top” position, you will need to read Canvas.Cliprect.Top, since the scroll window does not change the top position of the sub-controls. Windows handles this for you and only tells you what to do again as soon as the scroll bars are initialized.

Since you want to create a control that reaches the same full scroll area, I assume that you are creating a kind editor?

If so, you are likely to get better results by looking at SynEdit and fetching a base class that will add scrollbars to the regular TCustomControl (which is pretty simple). This way you can control both the coloring and the layout of your controls.

Here is the one I wrote for Lazarus and FreePascal some time ago. If you add messages to the uses clause and the prefix of message handlers with WM, and not with TLM, it will be compiled under Delphi.

(the code is long, should have used an external link): http://delphimax.wordpress.com/2010/09/20/platform-independent-image-component-for-lazarus/

+1
source

I tried to do this, and believe me, I could not. If you have instances of the controls that are inside the TScrollBox, you can use them to calculate (not exactly) the area.

0
source

Here is a complicated (but complete) solution:

  • I create my first child control during TScrollBox.Create (when TScrollBox does not yet have a scrollbar)
  • Install Child.Top:= 1
  • Create the rest of the child controls (this may cause the TScrollBox to display the scroll bar)
  • Scroll TScrollBox Up
  • Use one of the above solutions to calculate the height of the TScrollBox
0
source

Source: https://habr.com/ru/post/1332415/


All Articles