Opening winform from wpf application programmatically - c#

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).

Related

Close all WPF windows from winforms application

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!

C# Windows Forms and XAML

I would like to know if it is possible to combine XAML with Window Form or reverse. Also I would like to know a good place to learn how to work with both of them.
For example I want to make a XAML window on which I want to add a button with an event action wrote in C#.
Yes this is possible (both ways)
WinForms in WPF
WPF in WinForms
In both cases you add a special control that acts as a host for the other environment.
WindowsFormsHost allows you to host a Windows Forms control on a WPF page.

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.

how to set wpf form as windows form application mainform in c#

I am very new in WPF.
I have created a form style in Wpf application.
I want set this WPF application formstyle as my windows form application form style.
How can i do it.
There would be a great appreciation if someone could help me.
The form image is
you can override the forms onpaint event and drawing the region there. This allows to use GDI+ with antialiasing and make it look much cleaner.
you can use this solution also creating custom borders in winforms
The examples are found at here:
http://netcode.ru/dotnet/?lang=&katID=30&skatID=283&artID=7833
http://www.xtremevbtalk.com/showthread.php?t=288178

Is there a way to prevent WinForms controls in a WPF window from being unstyled?

I have some WinForms controls that I need to use in a WPF window. I'm able to get the controls to show up just fine and everything works as I would expect, but I'm experiencing one issue: all the WinForms controls are unstyled.
I'd like for the WinForms controls to at least use the default OS style (like I would see in a WinForms application). Is there any way to control this, or do I have to live with the controls the way they are?
The WinForms controls can't use the WPF styles, because Windows Forms doesn't understand the WPF styling and templating system.
To get them to use the "OS style" (the OS visual theme), try calling System.Windows.Forms.Application.EnableVisualStyles in your Main method. (I thought WPF handled this automatically, but I guess that's not what you're seeing.) This must be called before any controls get created!

Categories

Resources