Here's how to translate a point in window coordinates to screen coordinates and how to get the size of the window:
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Button x:Name="ButtonClientToScreen" Content="ButtonClientToScreen" Height="20" Click="ButtonClientToScreen_Click"></Button>
<Button x:Name="ButtonGetClientRect" Content="ButtonGetClientRect" Height="20" Click="ButtonGetClientRect_Click" ></Button>
</StackPanel>
</Grid>
</Window>
Code-behind:
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace WpfApplication2
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private IntPtr GetHandle()
{
var helper = new WindowInteropHelper(this);
var handle = helper.Handle;
return handle;
}
private void ButtonClientToScreen_Click(object sender, RoutedEventArgs e)
{
// convert a point in window coords to screen coords
var handle = GetHandle();
var point = new NativePoint {x = 50, y = 50};
var clientToScreen = NativeMethods.ClientToScreen(handle, ref point);
if (clientToScreen)
{
MessageBox.Show(this, string.Format("ClientToScreen: {0}", point));
}
}
private void ButtonGetClientRect_Click(object sender, RoutedEventArgs e)
{
// get window size
var handle = GetHandle();
NativeRect rect;
var clientRect = NativeMethods.GetClientRect(handle, out rect);
if (clientRect)
{
MessageBox.Show(this, string.Format("GetClientRect: {0}", rect));
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct NativeRect
{
public int left;
public int top;
public int right;
public int bottom;
public override string ToString()
{
return $"Left: {left}, Top: {top}, Right: {right}, Bottom: {bottom}";
}
}
[StructLayout(LayoutKind.Sequential)]
public struct NativePoint
{
public int x;
public int y;
public override string ToString()
{
return $"X: {x}, Y: {y}";
}
}
public class NativeMethods
{
[DllImport("user32.dll", EntryPoint = "GetClientRect")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetClientRect([In] IntPtr hWnd, [Out] out NativeRect lpRect);
[DllImport("user32.dll", EntryPoint = "ClientToScreen")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ClientToScreen([In] IntPtr hWnd, ref NativePoint lpPoint);
}
}
To transform the window rect to screen coordinates :
- call GetClientRect
- call ClientToScreen with x=0, y=0
- offset received rect by GetClientRect by the amount received by ClientToScreen
EDIT 1:
try move/resize the window and press the buttons, you'll understand how all this works.
EDIT 2:
Here's how to convert your window coords/size to screen:
private void ButtonWindowToScreenRect_Click(object sender, RoutedEventArgs e)
{
var handle = GetHandle();
var point = new NativePoint();
NativeMethods.ClientToScreen(handle, ref point);
NativeRect rect;
NativeMethods.GetClientRect(handle, out rect);
var win2screenRect = new NativeRect
{
left = rect.left + point.x,
right = rect.right + point.x,
top = rect.top + point.y,
bottom = rect.bottom + point.y
};
MessageBox.Show(this, win2screenRect.ToString());
}