How to get the window from the Process in WPF?

SaranRaj Chandrasekaran 5 Reputation points
2023-05-10T11:02:56.15+00:00

I have an application (A) in WPF. I launched another application (B) from the first application (A). I need to get the window of application (B).

I tried with "Process" to get the window of application (B), I can get the windowHandle by using the Process, but when I tried to get this as a window it returns null.

The application (B) is launched by using the Process.Start();. Below I have attached the code snippet.

Please suggest how to get the window of application (B) from the Process or any other solution.

I tried like this in c#.

window = new Window();
window.Show();

ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = "D:\Samples\ButtonSample\bin\Debug\ButtonSample.exe";
processInfo.ErrorDialog = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;

Process proc = Process.Start(processInfo); // Here the application(B) is launched

string fileName = Path.GetFileNameWithoutExtension("D:\Samples\ButtonSample\bin\Debug\ButtonSample.exe");
Process[] p = Process.GetProcessesByName(fileName);
IntPtr handle = p[0].MainWindowHandle;
Window window = HwndSource.FromHwnd(handle)?.RootVisual as Window;
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,710 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,621 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 83,101 Reputation points
    2023-05-10T11:35:21.6466667+00:00

    HwndSource.FromHwnd is for the current process

    What are you trying to do ?

    (the Win32 handle can be sufficient or UI Automation to manipulate XAML controls)


  2. Hui Liu-MSFT 47,336 Reputation points Microsoft Vendor
    2023-05-15T07:17:59.93+00:00

    Hi,@SaranRaj Chandrasekaran. Is there an update for this question? Did the following code help you?

     <StackPanel>
            <TextBlock Text="a"/>
            <Button Click="Button_Click" Content="open" Height="50"/>
        </StackPanel>
    
    
    

    Codebedhind:

    
      public partial class MainWindow : Window
        {
          
            public MainWindow()
            {
                InitializeComponent();
               
    
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                ProcessStartInfo processInfo = new ProcessStartInfo();
                processInfo.FileName = "C:\\...\\WpfAppB.exe";
                processInfo.ErrorDialog = true;
                processInfo.UseShellExecute = false;
                processInfo.RedirectStandardOutput = true;
    
                Process processB = Process.Start(processInfo);
                if (processB != null)
                {
                    IntPtr mainWindowHandle = processB.MainWindowHandle;
    
                    User32.Rect windowRect = new User32.Rect();
                    User32.GetWindowRect(mainWindowHandle, ref windowRect);
    
                    Point mousePosition = Mouse.GetPosition(null);
    
                    double screenWidth = SystemParameters.PrimaryScreenWidth;
                    double screenHeight = SystemParameters.PrimaryScreenHeight;
    
                    double x = windowRect.Left + mousePosition.X - screenWidth;
                    double y = windowRect.Top + mousePosition.Y - screenHeight;
                    MessageBox.Show("location"+ x + y);
                }
    
            }
        }
        public static class User32
        {
            [DllImport("User32.dll")]
            public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
    
            [StructLayout(LayoutKind.Sequential)]
            public struct Rect
            {
                public int Left;
                public int Top;
                public int Right;
                public int Bottom;
            }
        }
    

    ApplicationB:

     <Grid>
            <TextBlock Text="b"/>
        </Grid>
    
    

    The result:

    enter image description here


    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.

    0 comments No comments