Get child windows of WPF wrapper from Windows Forms/ an other thread

youki 991 Reputation points

I wants to activate (set foreground) a child window in Visual Studio (and any other WPF wrapper) from my windows forms app.

I'm getting the handles by the following as mentioned in my previous post (

 public static IntPtr GetProcessMainWindowHandle()  
     Process process = null;  
     if (GetCursorPos(out Point point))  
         IntPtr hWnd = WindowFromPoint(point);  
         GetWindowThreadProcessId(hWnd, out uint pid);  
         process = Process.GetProcessById((int)pid);  
     return process.MainWindowHandle;  

 public static IntPtr GetHandle()  
     IntPtr hWnd = IntPtr.Zero;  
     if (GetCursorPos(out Point point))  
         hWnd = WindowFromPoint(point);  
     return hWnd;  

It always returns null:

HwndSource source = HwndSource.FromHwnd(processWindowHandle) as HwndSource;  

Spy++ always shows an other handle.

I think i have to read it again and investigate it more intense but if i understand it right, they say that it's not possible to access a WPF child window of an other thread (app domain?) ?!

This doesn't work (i can't declare a variable of type Window), also not the solution from the guy who said that it works for him in VS2010:

1st try:

Window window = (Window)HwndSource.FromHwnd(hWnd).RootVisual  

2nd try:

  var hwnd = _dte.MainWindow.HWnd;  
  var window = HwndSource.FromHwnd((IntPtr)hwnd);  
  dynamic customWindow = window.RootVisual;  
  UIElement content = customWindow.Content;  

3rd try:
WindowInteropHelper doesn't help because the wrapper isn't the owner, it's the parent (the childs don't get minimized when the wrapper is minimized).

(I have seen it would be possible with UI Automation but if i'm right i have to define it for every single application where it's a WPF wrapper?!)

Any help would be great!


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,660 questions
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,164 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 81,341 Reputation points

    WPF children are not real windows (no HWND)
    You can enumerate them with UI Automation
    A test with Inspect on a WPF TextBlock : WPF-Text-Block.jpg