How can I get information about the coordinates and sizes of a window another app from my WPF app?

Alexander Kvitko 41 Reputation points
2023-08-01T16:53:56.8366667+00:00

How can I get information about the coordinates and sizes of a window another app from my WPF app?

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
12,075 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,853 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,651 Reputation points Microsoft External Staff
    2023-08-02T01:52:33.08+00:00

    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.


1 additional answer

Sort by: Most helpful
  1. Castorix31 88,296 Reputation points
    2023-08-01T23:09:37.5266667+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.