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)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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;
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)
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:
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.