How to get coordinates of window client area - c#

I can get the coordinates of a windows entire area, and the coordinates of the client area using the GetClientRect and GetWindowRect Win32 calls. My problem is that the GetClientRect always returns 0,0 for the top left. How do I figure out what the actual client region is relative to the window rect?

You can use ClientToScreen to get the coordinates of the upper left (0,0) point in screen coordinates. The RECT returned by GetClientRect will be appropriate to get you the lower right corner (just add to the POINT set by ClientToScreen).

Use ClientToScreen to convert the client coordinates to screen coordinates. The window rect (GetWindowRect) is already in screen coordinates, and includes the non-client area (borders, caption, etc)

And if you are working with WinForms then you can use PointToScreen instead of ClientToScreen for solution proposed by Reed Copsey.

The relation between window rect (with borders etc) and the client rect (inside borders) is most easily found using AdjustWindowRectEx(). Get the window style and ex style of the window, and call that function, to see how much border is on each side.

You can also use the MapWindowPoints function to convert an entire RECT to screen coordinates at once.

Related

How Do I Get the Starting Location of the Client Area in a Form, Relative to the Form's Top-Left Corner?

If I create a Form, Size=(300,300),
then its .ClientRectangle property returns {X=0,Y=0,Width=292,Height=266}
From the Rectangle that we receive, it seems that we get the Size of the Client Area, but not the Location inwhich it starts relative to the form's Top-Left corner(above th title bar and border)..
The X and Y values are always 0,0,
yet we know that there is an offset between the Form's top-left corner, and its Client Area..
(because there is the Title Bar, and form Border)
So how can I get the actual starting point for it?
Going over Control's properties, I couldn't find one,
and as written above, the X,Y part of the .ClientRectangle property always returns 0,0..
The reason I ask this, is because If you want to use the Control.DrawToBitmap() method,
then you need to supply it with a Bitmap object with some size.
If you create a Bitmap in the size of Form.Size, then you can capture the whole form.
It will look like this:
Yet if you want to capture only the client area of the form,
then you can create a Bitmap object with size=Form.ClientRectangle,
but then you need to know at which point the client rectangle starts..
or else, your capture will look like this:
As can be seen, it's indeed in the size of the client area, but it doesn't start at the right location..
So when wanting to capture only the Client Area of the form, we need to know where the client area starts, relative to the Form's real starting point
(the top-left point, at which the border corner is, right above the title bar)
Are you looking for that:
// -8, -30 at my workstation
// so 30 is a size of caption and top border
// 8 is a left border size
Point leftTopShift = PointToClient(this.Location);

Move System.Drawing.Rectangle to a different area on Screen

I am creating a screen capture & crop utility using answer posted here
The code draws a rectangle (System.Drawing.Rectangle) on the screen and saves a cropped image.
I need to make this rectangle movable to a different area on the screen without changing the size.
How can I achieve this?
What did not work for me?
I tried this codeproject article, works very good to move controls on the screen.
For this code to work correctly I would need to draw rectangle on a container control.
Which container can be used to wrapping?
Being a succesful web developer I used to think that I can write for WinForms also... I was wrong :-( Some help would be much appreciated!
In C# Windows Forms to change the location of a rectangle you do the following
rect.Location = new Location(x,y);
Where x and y are the coordinates (relative to the window's origin) that you want to move it to.
Here's a link to The MSDN Page on System.Drawing.Rectangle

GetWindowRect without titlebar

I want to get client area, I know the default answer is GetClientRect but what I get from that function is a rectangle starting at (0,0). So I can't use GetClientRect with CopyFromScreen.
Is there a way to get absolute coordinates with GetClientRect OR somehow learning the titlebar size etc. while using GetWindowRect?
I am using C#.NET.
Use PointToScreen to transform client coordinates to screen coordinates:

C# Mouse Move and Click relative to active window

Hi I have been using code similar to this in a piece of automation I have been working on
public static void LeftClick(int x, int y)
{
Cursor.Position = new System.Drawing.Point(x, y); //<= fails without this
mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
}
However unless I am being dumb this move the mouse to the x,y from the top left of the screen which causes me problems if the active window isn't where Im expecting it to be, can anyone suggest a way of achieving the same functionality with moving the mouse to a point relative to the active window.
Thanks
You need to pinvoke GetWindowRect() to find out where the window is located. So you can adjust x and y by the window position. Visit pinvoke.net for the declarations.
Just subtract the location (relative to the screen) of the window you are targeting.
What you're seeing is indeed the expected behavior. The Cursor.Position property describes the cursor's location in screen coordinates, not relative to your form.
However, every control exposes two handy methods that you can take advantage of to convert between screen coordinates and control coordinates:
The Control.PointToClient method computes the location of the specified screen point into client coordinates. Use this to convert from screen coordinates into client coordinates (i.e., those relative to your control, such as a form).
The Control.PointToScreen method computes the location of the specified client point into scren coordinates. Use this to convert from client coordinates into screen coordinates.
try PointToClient and PointToScreen of the control you are trying to find relative points to.

Find Coordinates for point on screen?

The thing is I have some graphics shown in a form, rectangle for example, and I want to capture when the point gets over thees fields. So I Thoght I try to find the corrrdinates for thees rectangles, but as thay are the coords in the form it dose not match the ones with the mouse location.
So I wonder is there a way to find what coord on the screen a Point has on the screen and not in the form or controller?
Each control hs PointToFoo methods for conversion. Note that you should call this from the parent of the object who's location you want:
Point scrPos = this.PointToScreen(panel1.Location);
Alternatively, you can get the panel's screen coordinates with:
Point scrPos = panel1.PointToScreen(new Point(0,0));
Note that the above two examples could gve different result due to the border-size of the panel.
If you are using the graphics object for the form by calling this.CreateGraphics() within the form, then the coordinates used when you draw the rectangle should be exactly the same as those returned by the click event on the form.
Do you know what coordinates your pointer is in? You can get the coordinates for your window with a call to GetWindowRect() and subtract the top/left from your mouse cursor to get client coordinates.
I seem to remember there being a function to do that for you in fact, but it's been some time since I dabbled in custom GUI controls.

Categories