How to find the visible part of the control?

I have a control whose parent element is ScrollableControl. How to find the part of the control that is really visible to the user? Both are rectangular - there are no fun things to do with the regions.

+5
source share
3 answers

I think the GetVisibleRectangle method that I wrote below is what you requested. Successive runs of this with scrolling gave the following result: scrolling the control:

  • {X = 0, Y = 0, Width = 0, Height = 0} - button 4 was scrolled out of sight. Please note that here is the meaning Rectangle.Empty.
  • {X = 211, Y = 36, = 25, = 13} - 4, .
  • {X = 161, Y = 36, = 75, = 13} - 4, .
  • {X = 161, Y = 26, = 75, = 23} - 4,

, X Y .

:

private void button1_Click(object sender, EventArgs e)
{
    Rectangle r = GetVisibleRectangle(this.panel1, button4);
    System.Diagnostics.Trace.WriteLine(r.ToString());
}

public static Rectangle GetVisibleRectangle(ScrollableControl sc, Control child)
{
    Rectangle work = child.Bounds;
    work.Intersect(sc.ClientRectangle);
    return work;
}
+10

AutoScrollPosition . X Y , (0,0). , X Y, . , x y 200, 200 ; x y 100, , 100 , 100 . AutoScrollPosition {-200, 0}; {-100,0}.

: MSDN: ScrollableControl.AutoScrollPosition

+3
0
source

All Articles