?
Solved

GetClientRect Win API Call from C#

Posted on 2006-04-08
5
Medium Priority
?
3,926 Views
Last Modified: 2013-11-13
Hi,

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:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/getclientrect.asp

Here's the code I tried:

        [DllImport("user32.dll")]
        private static extern bool GetClientRect(IntPtr hWnd, Rectangle lpRect);

0
Comment
Question by:salted
5 Comments
 
LVL 12

Accepted Solution

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

[StructLayout(LayoutKind.Sequential)]
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
0
 
LVL 14

Expert Comment

by:nayernaguib
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
0
 
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.  =)

        [DllImport("user32.dll")]
        private static extern bool GetClientRect(IntPtr hWnd, RECT lpRect);
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 400 total points
ID: 16410335
[DllImport("user32.dll")]
private static extern bool GetClientRect(IntPtr hWnd, ref RECT rect);
0
 

Author Comment

by:salted
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:

http://www.experts-exchange.com/Programming/Q_21807300.html

Thanks!
0

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