GetClientRect Win API Call from C#

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

What can I use as "RECT" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_6cqa.asp) 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

gbzhhu earned 1200 total points
ID: 16408969
You need to use this struct

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

See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/pinvoke.asp

Search for GetClientRect
LVL 14

Expert Comment

ID: 16409542
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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
ID: 16409618
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

AlexFM earned 400 total points
ID: 16410335
private static extern bool GetClientRect(IntPtr hWnd, ref RECT rect);

Author Comment

ID: 16411777
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:



Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

621 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