Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# absolute position of control on screen

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.

like image 303
Dragonphase Avatar asked Aug 25 '13 13:08

Dragonphase


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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);
like image 200
King King Avatar answered Nov 14 '22 22:11

King King