c#屏幕上控制的绝对位置

Dra*_*ase 5 c# controls position

我试图在屏幕上获得控件的绝对位置.我正在使用两台显示器,结果并不是那么好......

我正在做的是打开另一个表单来捕获图像,然后将此图像传递给主表单并关闭捕获表单.然后,我希望主窗体出现在捕获图片的相同位置.要获得我想要做的事情的要点,请在Windows上打开Snipping Tool并捕获一个剪辑.然后窗口将出现在拍摄图像的位置.

这是我用来执行此操作的当前代码:

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);
Run Code Online (Sandbox Code Playgroud)

CapturePanel包含存储图片的CaptureBox控件.我也从X位置获取8,从te Y位置获取30来补偿表单的边框和标题栏,但唯一的问题是某些计算机将使用不同的窗口样式,这些数字将会改变.

如果有一种方法可以用来获取窗口的边框和标题宽度/高度,那就太棒了.

编辑

解决方法是:

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
);
Run Code Online (Sandbox Code Playgroud)

在King King的帮助下向我指出了SystemInformation.

Kin*_*ing 6

为了得到Height你的Window caption,你可以试试这个:

int captionHeight = yourForm.PointToScreen(Point.Empty).Y - yourForm.Top;    
Run Code Online (Sandbox Code Playgroud)

要获取Width表单边框,您可以尝试这样做:

int borderWidth = yourForm.PointToScreen(Point.Empty).X - yourForm.Left;
Run Code Online (Sandbox Code Playgroud)

您还可以查看默认标题高度SystemInformation.CaptionHeight.

如果要获取CaptureBox屏幕坐标的位置,可以使用以下PointToScreen方法:

Point loc = CaptureBox.PointToScreen(Point.Empty);
Run Code Online (Sandbox Code Playgroud)