6

I'm trying to get the absolute position of a control on the screen. I'm using two monitors and the results aren't really that great...

What I'm doing is opening another form to capture an image, then passing this image to the main form and closing the capture form. I then want the main form to appear in the same place the picture was captured. To get a gist of what I'm trying to do as an example, open Snipping Tool on Windows and capture a snip. The window will then appear in the place that the image was taken.

This is the current code I am using to do this:

Location = new Point(Cursor.Position.X - CaptureBox.Width - CapturePanel.Location.X - CaptureBox.Location.X - 8, Cursor.Position.Y - CaptureBox.Height - CapturePanel.Location.Y - CaptureBox.Location.Y - 30);

CapturePanel contains the CaptureBox control which stores the picture. I'm also taking 8 from the X location and 30 from te Y location to compensate for the form's border and title bar, but the only problem with this is that some computers will be using a different window style, and these numbers will change.

If there is a method that can be used to grab the border and title width/height of windows, that would be great.

EDIT

A solution to this would be:

Location = new Point(
    Cursor.Position.X -
    CaptureBox.Width -
    CapturePanel.Location.X -
    CaptureBox.Location.X - 
    SystemInformation.HorizontalResizeBorderThickness,
    Cursor.Position.Y -
    CaptureBox.Height -
    CapturePanel.Location.Y -
    CaptureBox.Location.Y -
    SystemInformation.CaptionHeight -
    SystemInformation.VerticalResizeBorderThickness
);

With help from King King pointing out SystemInformation to me.

Dragonphase
  • 319
  • 2
  • 6
  • 15

1 Answers1

8

To get the Height of your Window caption, you can try this:

int captionHeight = yourForm.PointToScreen(Point.Empty).Y - yourForm.Top;    

To get the Width of the form border, you can try this:

int borderWidth = yourForm.PointToScreen(Point.Empty).X - yourForm.Left;

Also you may want to look at the default caption height by SystemInformation.CaptionHeight.

If you want to get the location of the CaptureBox in screen coordinates, you can use the PointToScreen method:

Point loc = CaptureBox.PointToScreen(Point.Empty);
King King
  • 61,710
  • 16
  • 105
  • 130
  • The first method works, SystemInformation.CaptionHeight was smaller than it should be. What about the window border size? – Dragonphase Aug 25 '13 at 13:29
  • @Dragonphase if you want to get the location of `CaptureBox`, see my updated answer, no need to calculate the `Border size`. – King King Aug 25 '13 at 13:30
  • That won't work, since I'm not passing the location of the captured image to the main form. If I do that, it results in the absolute position (for example, 1440) being added to the currently selected form's location on the primary monitor. I came up with a solution however, its in my main post. – Dragonphase Aug 25 '13 at 13:40
  • @Dragonphase you said you want to place your form at the location of your `CaptureBox` in screen coordinates? – King King Aug 25 '13 at 13:42
  • No, to quote `I then want the main form to appear in the same place the picture was captured` - the location of the `CaptureBox` only changes when it needs to become centered in the `CapturePanel`; to be more specific, I want the `CaptureBox` to appear in exactly the same location as the rectangle that was used to capture the image in the capture form, thus updating the location of the form respectively. – Dragonphase Aug 25 '13 at 13:46