Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 996927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:57:08+00:00 2026-05-16T06:57:08+00:00

I have a panel that may or may not be within other panels. I’m

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:57:08+00:00Added an answer on May 16, 2026 at 6:57 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a repeater that creates n-number of panels. I am trying to dynamically
I have an Panel control that I need to maintain position across postbacks. I
i have a flow panel that i'm adding extra items to it at runtime
I have a stack panel inside of an expander panel that I programaticaly adds
I have a Java swing application with a panel that contains three JComboBoxe s
I'm using asp.net/c# and have a number of Collapsible Panel Extenders that have been
I have Datalist that is inside an updatepanel and it is in panel in
I have a form that will multiple Panel controls stacked on top of each
I have an ASP.Net 2.0 page that contains two UpdatePanels. The first panel contains
I have a number of asp.net AJAX update panels on a page, that can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.