Hi,@Alexander Kvitko. Welcome Microsoft Q&A.
To get information about the coordinates and sizes of a window from another app in your WPF app, you could use the Windows API functions provided by the user32.dll library. Specifically, you can use the FindWindow
function to find the window handle of the target window and then use the GetWindowRect
function to retrieve the window's position and size. Here's a sample you could refer to:
using System;
using System.Runtime.InteropServices;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Window1 w = new Window1();
w.Show();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string targetWindowTitle = "Window1";
WindowInfo windowInfo = WindowUtils.GetWindowInfo(targetWindowTitle);
if (windowInfo != null)
{
MessageBox.Show($"Left: {windowInfo.Left}, Top: {windowInfo.Top}, Width: {windowInfo.Width}, Height: {windowInfo.Height}");
}
else
{
MessageBox.Show("Window not found or error occurred.");
}
}
}
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public class WindowInfo
{
public int Left { get; set; }
public int Top { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
public static class WindowUtils
{
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public static WindowInfo GetWindowInfo(string windowTitle)
{
IntPtr hWnd = FindWindow(null, windowTitle);
if (hWnd == IntPtr.Zero)
{
return null;
}
RECT rect;
if (GetWindowRect(hWnd, out rect))
{
WindowInfo windowInfo = new WindowInfo
{
Left = rect.Left,
Top = rect.Top,
Width = rect.Right - rect.Left,
Height = rect.Bottom - rect.Top
};
return windowInfo;
}
return null;
}
}
Update:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Window1 w = new Window1();
w.Show();
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
IntPtr currentWindowHandle = GetForegroundWindow();
private void Button_Click(object sender, RoutedEventArgs e)
{
IntPtr currentWindowHandle = WindowHandleHelper.GetCurrentWindowHandle();
string currentWindowTitle = WindowHandleHelper.GetWindowTitle(currentWindowHandle);
WindowInfo windowInfo = WindowHandleHelper.GetWindowInfo(currentWindowTitle);
if (windowInfo != null)
{
MessageBox.Show($"Left: {windowInfo.Left}, Top: {windowInfo.Top}, Width: {windowInfo.Width}, Height: {windowInfo.Height}");
}
else
{
MessageBox.Show("Window not found or error occurred.");
}
}
}
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public class WindowInfo
{
public int Left { get; set; }
public int Top { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
public class WindowHandleHelper
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public static WindowInfo GetWindowInfo(string windowTitle)
{
IntPtr hWnd = FindWindow(null, windowTitle);
if (hWnd == IntPtr.Zero)
{
return null;
}
RECT rect;
if (GetWindowRect(hWnd, out rect))
{
WindowInfo windowInfo = new WindowInfo
{
Left = rect.Left,
Top = rect.Top,
Width = rect.Right - rect.Left,
Height = rect.Bottom - rect.Top
};
return windowInfo;
}
return null;
}
public static IntPtr GetCurrentWindowHandle()
{
return GetForegroundWindow();
}
public static string GetWindowTitle(IntPtr windowHandle)
{
const int nChars = 256;
StringBuilder windowTitle = new StringBuilder(nChars);
if (GetWindowText(windowHandle, windowTitle, nChars) > 0)
{
return windowTitle.ToString();
}
return string.Empty;
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.