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

youki 1,021 Reputation points
2020-12-22T09:06:12.797+00:00

Hello,
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 (https://learn.microsoft.com/en-us/answers/questions/198940/set-foreground-of-child-window-in-visual-studio.html?childToView=207076#answer-207076):

 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?) ?!
https://stackoverflow.com/questions/5116429/get-window-instance-from-window-handle

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:
https://stackoverflow.com/questions/5822026/get-wpf-window-by-hwnd

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!

Regards

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,801 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.
11,124 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 86,316 Reputation points
    2020-12-22T09:19:31.393+00:00

    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


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.