Close all WPF windows from winforms application - c#

I host a WPF application in my winforms application via the ElementHost control. I've implemented a logic which listens to unhandled exceptions on WPF side. If an exception is catched, the ElementHost control should be disposed and all related WPF windows should be closed.
This works quite fine, if there is only one WPF window. Since that WPF application can open up more sub windows (which are undocked windows) those windows are not closed when I dispose the ElementHost control.
Is there an easy way to close that WPF window and all child windows from winforms side?
I have tried Application.OpenForms but the sub WPF windows do not show up (makes sense somehow ;-)).
One remark: I do own the WPF code so I could implement something on the WPF side, but I really would like to stick on the win forms side.
Also I would like to consider situations where the WPF window code might be "stuck" and is not able to react and close it self. That's why I'd like to kill the windows from "outside"

So I made up my mind and followed cdkMoose recommendation to let this be handled by the WPF part. It's probably a good idea to let the clean up be done by the one who has the knowledge about what has to be done. Thanks though!

Related

C# Hosting WPF in Winforms

I am looking at the possibility to host a WPF control inside a Winforms application. The reason for this is some animations inside the WinForms application. WPF should be able to support this way better because it (mostly) uses the graphic card to render instead of the CPU.
Now my question:
Are there any cons of hosting a WPF Control inside a Winforms application? Does the WPF Control still use the graphic card for the rendering or does it loose some of its advantages?
Thanks for your help. If you have any inputs or tips feel free to tell me.
EDIT
I found a similar question (but maybe that one is a bit more general, I focus more on the rendering):
Any disadvantage to using an ElementHost to host a WPF UserControl in a Winform application?
Mainly it behaves very like in a normal WPF Application.
But sometimes there are little unexpected behaviors. And it should use the hardware rendering if it would be using in normal wpf applications (depending on your configuration)
Sometimes i had problems with correctly recevien some keyboard keys in events. but there are a lot of artikels descriping the problems and solutions.
For example with some controls you could have problems catching keys. So you should have a look at System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop
-> also see this blogpost

Creating new MDI Window in hosting application

I have an interesting case to solve:
I have a native (winapi) application, which uses MDI. This application allows me to extend itself with a simple scripting language. The scripts are launched on different thread than UI thread (although I can get the UI thread ID with appropriate functions). The scripting language also allow me to launch any c++ code (through LoadLibrary).
What I would like to achieve is to create a new window inside this application, which could host WPF and "attach it" as a MDI child window to MDI client (mdi area). Also, I would like this window to properly "talk" to MDI area, for example update list of windows in mdi menu.
So far, my first guess was to just create a WinForms window, host WPF inside, and then try to make it mdi child window by setting MDI client as it's parent (because my hosting application is not written in c#, I only have a handle, so I did this with User32.SetParent() P/Invoke). This worked almost well, after I attached my script thread to GUI thread, but I had few problems with it: the MDI menu with active windows did not update, the window did not interact correctly (it stayed on stop when it shouldn't etc.).
Then, I tried to set flags (style, exStyles) with SetWindowLongPtr. It changed my window's behavior a bit, but that was still not it.
Now I'm considering using CreateMDIWindow function, or doing it by SendMessage, according to docs sending message should create a window, even if I send it from different thread. The problem is, that this way would give me a native handle only, and I could not find (yet) any way of hosting WinForms / WPF inside it.
I'm curious if anyone tried to do something similar and had any results with it? Which way would be the best - trying to create a WinForms window and add it to MDI client, or create a native window and try to host WinForms in it (any particular way of doing that)?
Most answers for this question I have found are dealing with situation where hosting application is managed, so you can just set .MDIParent property, which won't work in this case I'm afraid.

Opening winform from wpf application programmatically

How would I go about opening a winform from a wpf application? This needs to be done through code, but the normal function
Form.Show();
will not work. I need something that will allow me to change the visibility of the form, but I am not able to access any of the gui related functions within this form.
You can work with the Windows Form the same way in a WPF application as you would from a Windows Forms application, provided you have the proper references. This should work fine:
YourForm form = new YourForm();
form.Show();
However, I would typically recommend trying to rework this to include the Windows Forms content as a UserControl within a WPF Window using a WindowsFormsHost control. This tends to be a little cleaner, since the window parenting works a bit more cleanly (since they're all WPF windows).

integration of wpf into window application

I am new in wpf application..
I am working on window application.it has many module.its one of module is in wpf which is seperate from project.so I want to integrate wpf application into window application project.
Your question is unclear. However, if you are trying to host a WPF object inside of a WinForms application, then yes, that is possible.
A good tutorial can be found here.
Assuming you mean MDI Winform for "windows Application". Here are the steps:
Create a new child Winform.
Add "WPF Interoperability" Element
Add your WPF pages to the WPF interop element
These steps only work in .NET Framework 3.0 or above, where WPF is supported.
You need to add an interop layer between the MDI application and the WPF component that you want to host. This is non-trivial.
1) Determine where the border between your managed and native code will lie. Ensure that you clearly define, delineate, and respect this border, or you will weep tears of pain. This will require use of C++/CLI in any real-world scenario.
I suggest creating a C++/CLI ref class called something like "Launcher" to act as a springboard. It exposes a native API that your native application can consume. The native application provides a pointer to your MDI window and any other req'd information.
2) Use the MDI child pointer and an HwndSource on the WPF component to drop your WPF into the native window.
3) Supply appropriate manual forwarding of window messages from MDI-land to WPF-land via a MessageHook in the HwndSource. Note that you'll manually be handling everything from WM_WINDOWPOSCHANGED to ID_HELP.
Good luck!
You can use ElementHost to add your wpf controls to windows forms. Add ElementHost control to windows form and set your wpf control as ElementHost Child property.
You can find more details here
MSDN
ElementHost

How to host a WPF form in a MFC application

I'm looking for any resources on hosting a WPF form within an existing MFC application. Can anyone point me in the right direction on how to do this?
From what I understand (haven't tried myself), it's almost as simple as just giving the WPF control the parent's handle. Here's a Walkthrough: Hosting WPF Content in Win32.

Categories

Resources