如何获取面板的可见边界

发布于 2024-09-13 20:55:35 字数 4783 浏览 3 评论 0原文

我有一个面板,它可能位于也可能不位于其他面板中。我正在尝试计算出该面板的可见边界。我认为 GetWindowRect 或 GetClientRect 可以解决问题,但没有什么乐趣。

为了测试这一点,我创建了一个带有面板和多行文本框的表单。面板比表单大(即它延伸到表单底部以下)

所以如果我的表单是 300 x 300。并且面板位于 10,10 并且是 100 x 500 我想要一些东西来告诉我可见区域是 100, 290(假设面板的 0,0 相对起始点总体为 10,10。

是否存在这样的方法?

我尝试了几种不同的方法就像获取我感兴趣的面板的父句柄并对其进行测试,但我不能总是确定直接父级将是决定可见性的面板,

这是我编写的测试应用程序的代码。 >GetWindowRect 或 GetClientRect

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace clientRectTest
{
public partial class Form1 : Form
{
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public Int32 left;
        public Int32 top;
        public Int32 right;
        public Int32 bottom;

        public static RECT FromRectangle(Rectangle rectangle)
        {
            RECT win32rect = new RECT();
            win32rect.top = rectangle.Top;
            win32rect.bottom = rectangle.Bottom;
            win32rect.left = rectangle.Left;
            win32rect.right = rectangle.Right;
            return win32rect;
        }
    }

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowDC(IntPtr hWnd);


    public Form1()
    {
        InitializeComponent();
        this.AutoScrollMinSize = new Size(250, 500);

    }


    protected override void OnMouseClick(MouseEventArgs e)
    {
        NewLine("Click Location: " + e.Location.ToString());
        base.OnMouseClick(e);
    }

    protected override void OnResize(EventArgs e)
    {
        this.textBox1.Text = "Panel Size" + this.panel1.Size.ToString();

        NewLine( "Form Size" + this.Size.ToString());
        //  NewLine("Panel PARENT Client Rectangle: " + getPanelClientRectangle(this.panel1.Parent.Handle));


        NewLine("Panel Client Rectangle: " + getPanelClientRectangle(this.panel1.Handle));
        NewLine("Panel Window Rectangle: " + getPanelWindowRectangle(this.panel1.Handle));
        NewLine("Panel Window Bounts: " + this.panel1.Bounds.ToString());


        NewLine("Panel DC Client Rectangle: " + getPanelDCClientRectangle(this.panel1.Handle));
        NewLine("Panel DC Window Rectangle: " + getPanelDCWindowRectangle(this.panel1.Handle));

        NewLine("Panel Location: " + this.panel1.Location.ToString());


        base.OnResize(e);
    }

    private string getPanelDCClientRectangle(IntPtr handle)
    {
        string cr = string.Empty;

        RECT r1 = new RECT();
        IntPtr dc = GetWindowDC(handle);
        GetClientRect(dc, out r1);

        cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
         + ", " + r1.bottom.ToString();
        Point thisLocation = this.Location;

        return cr;
    }

    private string getPanelDCWindowRectangle(IntPtr handle)
    {
        string cr = string.Empty;

        RECT r1 = new RECT();
        IntPtr dc = GetWindowDC(handle);
        GetWindowRect(dc, out r1);

        cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
         + ", " + r1.bottom.ToString();
        Point thisLocation = this.Location;

        return cr;
    }

    private string getPanelWindowRectangle(IntPtr handle)
    {
        string cr = string.Empty;

        RECT r1 = new RECT();

        GetWindowRect(handle, out r1);

        cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
         + ", " + r1.bottom.ToString();
        Point thisLocation = this.Location;

        return cr;
    }

    private string getPanelClientRectangle(IntPtr handle)
    {
        string cr = string.Empty;

        RECT r1 = new RECT();

        GetClientRect(handle, out r1);

        cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
         + ", " + r1.bottom.ToString();
        Point thisLocation = this.Location;

        return cr;
    }

    private void NewLine(string p)
    {
        this.textBox1.Text += Environment.NewLine + p;
    }
}
}

编辑:找到更多信息:

我认为行 this.AutoScrollMinSize = new Size(250, 500); 搞乱了我的结果。这似乎使面板达到 500,即使它没有显示 500。将重做我的一些测试用例。我没想到这条线会引起问题。

I have a panel that may or may not be within other panels. I'm trying to work out the visible bounds of that panel. I thought the GetWindowRect or the GetClientRect would do the trick but no joy.

To test this I've created a form with a panel and at multiline text box. The panel is bigger than the form (i.e. it stretches below the bottom of the form)

So if my form is 300 by 300. And the Panel is located at 10,10 and is 100 by 500
I want something that will tell me that the visible area is 100, 290 (assuming the 0,0 relative starting point for the panel which would be at 10,10 over all.

Does such a method exist?

I have tried a few different methods like getting the parent handle of the panel i'm interested in and testing that but I can't always be sure the direct parent will be the one that determines the visibility.

Here is the code of the test application I wrote to test GetWindowRect or the GetClientRect:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace clientRectTest
{
public partial class Form1 : Form
{
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public Int32 left;
        public Int32 top;
        public Int32 right;
        public Int32 bottom;

        public static RECT FromRectangle(Rectangle rectangle)
        {
            RECT win32rect = new RECT();
            win32rect.top = rectangle.Top;
            win32rect.bottom = rectangle.Bottom;
            win32rect.left = rectangle.Left;
            win32rect.right = rectangle.Right;
            return win32rect;
        }
    }

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowDC(IntPtr hWnd);


    public Form1()
    {
        InitializeComponent();
        this.AutoScrollMinSize = new Size(250, 500);

    }


    protected override void OnMouseClick(MouseEventArgs e)
    {
        NewLine("Click Location: " + e.Location.ToString());
        base.OnMouseClick(e);
    }

    protected override void OnResize(EventArgs e)
    {
        this.textBox1.Text = "Panel Size" + this.panel1.Size.ToString();

        NewLine( "Form Size" + this.Size.ToString());
        //  NewLine("Panel PARENT Client Rectangle: " + getPanelClientRectangle(this.panel1.Parent.Handle));


        NewLine("Panel Client Rectangle: " + getPanelClientRectangle(this.panel1.Handle));
        NewLine("Panel Window Rectangle: " + getPanelWindowRectangle(this.panel1.Handle));
        NewLine("Panel Window Bounts: " + this.panel1.Bounds.ToString());


        NewLine("Panel DC Client Rectangle: " + getPanelDCClientRectangle(this.panel1.Handle));
        NewLine("Panel DC Window Rectangle: " + getPanelDCWindowRectangle(this.panel1.Handle));

        NewLine("Panel Location: " + this.panel1.Location.ToString());


        base.OnResize(e);
    }

    private string getPanelDCClientRectangle(IntPtr handle)
    {
        string cr = string.Empty;

        RECT r1 = new RECT();
        IntPtr dc = GetWindowDC(handle);
        GetClientRect(dc, out r1);

        cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
         + ", " + r1.bottom.ToString();
        Point thisLocation = this.Location;

        return cr;
    }

    private string getPanelDCWindowRectangle(IntPtr handle)
    {
        string cr = string.Empty;

        RECT r1 = new RECT();
        IntPtr dc = GetWindowDC(handle);
        GetWindowRect(dc, out r1);

        cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
         + ", " + r1.bottom.ToString();
        Point thisLocation = this.Location;

        return cr;
    }

    private string getPanelWindowRectangle(IntPtr handle)
    {
        string cr = string.Empty;

        RECT r1 = new RECT();

        GetWindowRect(handle, out r1);

        cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
         + ", " + r1.bottom.ToString();
        Point thisLocation = this.Location;

        return cr;
    }

    private string getPanelClientRectangle(IntPtr handle)
    {
        string cr = string.Empty;

        RECT r1 = new RECT();

        GetClientRect(handle, out r1);

        cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
         + ", " + r1.bottom.ToString();
        Point thisLocation = this.Location;

        return cr;
    }

    private void NewLine(string p)
    {
        this.textBox1.Text += Environment.NewLine + p;
    }
}
}

EDIT: more information found:

I think the line this.AutoScrollMinSize = new Size(250, 500);
was messing up my results. This seemed to make the panel 500 even though it wasn't displaying 500. Will redo some of my test cases. I wouldn't have expeced this line to cause problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

祁梦 2024-09-20 20:55:35

您应该能够使用面板的 DisplayRectangle 属性来获取当前正在显示的矩形。此属性在 Control 中实现,但由 ScrollableControl(System.Windows.Forms.Panel 的父级)覆盖

You should be able to use the DisplayRectangle property of the panel to get the rectangle that is currently being displayed. This property is implemented in Control, but is overriden by ScrollableControl (the parent of System.Windows.Forms.Panel)

爱要勇敢去追 2024-09-20 20:55:35

这就是我最后想到的:

WinSDK.RECT parentRect = new WinSDK.RECT();
            WinSDK.RECT intersectRect = new WinSDK.RECT();

            Rectangle parentRectangle = new Rectangle();
            Rectangle intersectRectangle = new Rectangle();

            // Get the current Handle.
            IntPtr currentHandle = this.Handle;

            // Get next Parent Handle.
            IntPtr parentHandle = WinSDK.GetParent(this.Handle);

            // Get the Rect for the current Window.
            WinSDK.GetWindowRect(this.Handle, out intersectRect);

            // Load Current Window Rect into a System.Drawing.Rectangle
            intersectRectangle = new Rectangle(intersectRect.left, intersectRect.top, intersectRect.right - intersectRect.left, intersectRect.bottom - intersectRect.top );

            // Itterate through all parent windows and get the overlap of the visible areas to find out what's actually visible.
            while (parentHandle != IntPtr.Zero)
            {
                // Get the Rect for the Parent Window.
                WinSDK.GetWindowRect(parentHandle, out parentRect);
                parentRectangle = new Rectangle(parentRect.left, parentRect.top, parentRect.right - parentRect.left, parentRect.bottom - parentRect.top);

                // Get the intersect between the current and parent window.
                intersectRectangle.Intersect(parentRectangle);

                // Set up for next loop.
                currentHandle = parentHandle;
                parentHandle = WinSDK.GetParent(currentHandle);
            }

            return intersectRectangle;

This is what I came up with in the end:

WinSDK.RECT parentRect = new WinSDK.RECT();
            WinSDK.RECT intersectRect = new WinSDK.RECT();

            Rectangle parentRectangle = new Rectangle();
            Rectangle intersectRectangle = new Rectangle();

            // Get the current Handle.
            IntPtr currentHandle = this.Handle;

            // Get next Parent Handle.
            IntPtr parentHandle = WinSDK.GetParent(this.Handle);

            // Get the Rect for the current Window.
            WinSDK.GetWindowRect(this.Handle, out intersectRect);

            // Load Current Window Rect into a System.Drawing.Rectangle
            intersectRectangle = new Rectangle(intersectRect.left, intersectRect.top, intersectRect.right - intersectRect.left, intersectRect.bottom - intersectRect.top );

            // Itterate through all parent windows and get the overlap of the visible areas to find out what's actually visible.
            while (parentHandle != IntPtr.Zero)
            {
                // Get the Rect for the Parent Window.
                WinSDK.GetWindowRect(parentHandle, out parentRect);
                parentRectangle = new Rectangle(parentRect.left, parentRect.top, parentRect.right - parentRect.left, parentRect.bottom - parentRect.top);

                // Get the intersect between the current and parent window.
                intersectRectangle.Intersect(parentRectangle);

                // Set up for next loop.
                currentHandle = parentHandle;
                parentHandle = WinSDK.GetParent(currentHandle);
            }

            return intersectRectangle;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文