如何找到一个控制的可见部分?部分

2023-09-03 07:47:33 作者:思念幻化成海

我的父母是 ScrollableControl 控制。如何找到控制这实际上对用户可见的部分?两者都是长方形的 - 有没有有趣的业务地区

I have a control whose parent is a ScrollableControl. How do I find the part of the control that's actually visible to the user? Both are rectangular - there's no funny business with Regions.

推荐答案

我觉得GetVisibleRectangle方法,我写了下面就是你请求。这与滚动连续运行产生了以下的输出作为控制已滚动:

I think the GetVisibleRectangle method I wrote below is what you were requesting. Successive runs of this with scrolling yielded the following output as the control was scrolled:

{X = 0,Y = 0,宽度= 0,高度= 0} - 将Button4被滚出视。请注意,这里的值是 Rectangle.Empty 。 {X = 211,Y = 36,宽度= 25,高度= 13} - 将Button4已滚动这样的左上角是可见 {X = 161,Y = 36,宽度= 75,身高= 13} - 将Button4已滚动所以上半部分和整个宽度是可见 {X = 161,Y = 26,宽度= 75,身高= 23} - 将Button4已滚动是完全可见 {X=0,Y=0,Width=0,Height=0} - button4 was scrolled out of view. Note that the value here is Rectangle.Empty. {X=211,Y=36,Width=25,Height=13} - button4 was scrolled so the upper left corner was visible {X=161,Y=36,Width=75,Height=13} - button4 was scrolled so the top half and entire width was visible {X=161,Y=26,Width=75,Height=23} - button4 was scrolled to be entirely visible

请注意如何,除了宽度和高度改变的X,Y也改变了与滚动。

Note how in addition to the Width and Height changes that the X,Y also changed with scrolling.

来源:

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;
}