1

I've implemented some new WPF stuff in a old WinForm app project and I used to run it as the following:

    WpfWindow win = new WpfWindow(); // is a subclass of System.Windows.Window
    System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(win);
    WindowInteropHelper helper = new WindowInteropHelper(win);
    helper.Owner = this.Handle;

    win.Show();

now the question is, how can I get a list of opened Windows? In a native WPF project I simply do:

var windows = Application.Current.Windows

but I did not find any way in a form project.

Mike97
  • 596
  • 5
  • 20
  • To get a list of open forms for a windows project [Application.OpenForms](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.openforms?view=windowsdesktop-6.0) – Karen Payne Jul 24 '22 at 18:48
  • Hi Karen and thanks for your comment, but as I stated in the title I'm running a WPF Window, i.e. System.Windows.Window. Not sure your suggestion will work....or I'm wrong? – Mike97 Jul 24 '22 at 18:54
  • Hi, I was keying off `but I did not find any way in a form project.` – Karen Payne Jul 24 '22 at 19:44
  • See [How to: Get all Windows in an Application](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/app-development/how-to-get-all-windows-in-an-application?view=netframeworkdesktop-4.8) for WPF – Karen Payne Jul 24 '22 at 19:45
  • 1
    You can do the same thing that the current AppDomain does with the `Application.OpenForms` collection: build a `List`, say `openWindows`. When you create a new Window, add it to the collection, subscribe to the `Closed` event (using a method, not a Lambda). When you close the Window, the event is raised, cast `sender` to `System.Windows.Window`, remove the event handler and remove the Window from the collection (i.e., `var w = sender as System.Windows.Window; w.Closed -= [Closed Handler]; openWindows.Remove(w);`). – Jimi Jul 24 '22 at 20:11
  • Hi @Jimi, I had already thought about a List of Window objects but was hoping for a native solution, and +1 for the event handler tip. For the second solution, if i'm not mistaken, I have to add the Window to a collection anyway, so the first solution appear to be more easy. Let you know – Mike97 Jul 24 '22 at 22:40
  • @Karen, thanks again but the link you posted points to a solution (which is already mentioned above in my question) that can work only if you run a native WPF app while this is not the case because it's a WinForm project with some WPF funtionalities say "transplanted", where Application.Current is simply null so cannot help here. – Mike97 Jul 24 '22 at 22:51
  • Well, you could extend the *concept*, build a small class Factory that initializes your Windows, subscribes to the event and handles the `List`. It could expose a generic method, e.g., `public T OpenWindow(Form owner) where T : System.Windows.Window, new() { // Create a new Window of Type T, subscribe to the Closed event, add the Window to a collection and return the instance }`, so you just need to call the public method of the Factory, the rest is automatic. The Factory of course exposes methods that allow to query or modify the Collection. – Jimi Jul 24 '22 at 23:20
  • Guys, I've accepted the answer from @Reza Aghaei. Thanks anyway – Mike97 Jul 25 '22 at 13:50

1 Answers1

1

Before you show the first Window, for example in startup of your application, create an instance of System.Windows.Application (and forget it).

var app = new System.Windows.Application();

Later, in your WinForms application, you can easily find all WPF windows:

var windows = System.Windows.Application.Current.Windows

Note:

  • Do this only once, for example in startup of the application. Look at source code of the application class.

  • It's pretty similar to the way that a WPF app does it:

    public partial class App : System.Windows.Application 
    {
        ...
        ...
        ...         
        public static void Main() 
        {
            WpfApp1.App app = new WpfApp1.App();
            app.InitializeComponent();
            app.Run();
        }
    }
    
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398