GetClientRect Win API Call from C#

Posted on 2006-04-08
Last Modified: 2013-11-13

What can I use as "RECT" ( in C# when calling an win API

I tried the Rectangle class, but as you'd expect, it threw an access violation.

Here's the api call I'm trying to use:

Here's the code I tried:

        private static extern bool GetClientRect(IntPtr hWnd, Rectangle lpRect);

Question by:salted
    LVL 12

    Accepted Solution

    You need to use this struct

    public struct RECT
        public int left;
        public int top;
        public int right;
        public int bottom;


    Search for GetClientRect
    LVL 14

    Expert Comment

    If you want to get the client rectangle of a .NET form or control, you can use the ClientRectangle property of the object.


      Nayer Naguib
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    Be sure to change your API declaration so that it uses the RECT struct posted by gbzhhu instead of Rectangle.  =)

            private static extern bool GetClientRect(IntPtr hWnd, RECT lpRect);
    LVL 48

    Assisted Solution

    private static extern bool GetClientRect(IntPtr hWnd, ref RECT rect);

    Author Comment

    Thanks everyone!

    This is all really useful (giving the points on this one is going to be tough :)

    I used "out" instead of "ref" - i guess that won't make much difference in this case.

    OK, so it turns out this isn't doing what i expected :(

    To keep it fair, I'm gonna close this question and start a new one (since you've all answered my question here)

    See here for my adapted question:


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    fizzArray2 challenge 1 41
    wordsWithout 49 64
    userCompare  challenge 3 52
    Program to display an alert on Windows Toolbar 2 29
    This is an explanation of a simple data model to help parse a JSON feed
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now