Mixing MFC and WPF: Modal Dialogs - wpf

I'm adding C# WPF dialogs to an existing C++ MFC app, using a C++/CLI interface layer. I've got things working, except I'm having a problem with modality. For example:
MFC app shows a WPF dialog using ShowDialog. Works as expected.
That WPF dialog shows a MFC dialog using DoModal. The WPF dialog is hidden behind the base C++ app, and is not disabled unless I manually change IsEnabled. Not ideal, but it works.
Now, that MFC dialog is closed. Now for some reason the base MFC app is enabled, when it should still be disabled due to the WPF dialog not having been closed. That's bad, as it now allows the user to do crazy things while the WPF dialog is still open.
I have a feeling that it would work better if I could set parent dialogs correctly. But so far I havent been able to set an MFC dialog's parent as a WPF dialog, or vice versa. And, I don't even know if that'd fix it.
Any ideas?

When opening a CDialog, the trick is to use a WindowsInteropHelper to get the parent WPF dialog's HWND. Then, you can use CWnd::Attach to wrap that HWND in a CWnd class to pass to the CDialog's constructor.
The problem I had was that I already had the CDialog constructed., but not yet displayed. The various versions of SetParent can only be used if your target child window already has a valid handle. I had to write a new function in my CDialog class to set m_wndParent, which is what it uses as the parent when it finally creates the dialog. Then everything works great!
Somehow creating WPF dialogs from MFC dialogs "just works". It's magic.

When showing the WPF dialog, are you using the HwndSource class to wrap the WPF window? If so, you may be able to ::SetParent the WPF window as well as use the HwndSource.Handle property to set the sub-child's parent.

Related

What should I implement for a small popup control in a VSTO project?

In my VSTO projects (Office 2007 / 2010) I would like to use a small popup control (think Tooltip like features; fade animation and mouse interaction).
I would prefer to use WPF. If I were to do this in WPF I would create a custom Popup Control.
In VSTO, as far as I understand it, I must use a WPF window and then have 2 options, either I put this window in a Windows Form Container or I get the Hwnd of my office app and I add this Hwnd to the Owner property of a WPF Window. Am I correct here?
Creating a whole window, animating it on and off the screen etc to look like a ToolTip seems to be overkill.
So my question is how should I do this. I would prefer WPF.
I don't know if this would work but my first thought is to build a very tiny WPF window which is transparent and start it with the Office App. I can then build a Popup Control (which is a child of this tiny window) which I show and not show at a mouse position. Pretty sure a popup can appear outside the bounds of its parent window.
Just wanted to tidy up my own question with what I did. In the VSTO addin project I added a reference to WindowsBase, PresentationFramework and presentation core.
And then I just follow any tutorial or example that adds a WPF Popup. By popup I mean a System.Windows.Controls.Primitives.Popup. For the Placement I used PlacementMode.Absolute and then used a window point to set its position.
This pretty much answers my question. Of course this popup is literally floating above your office window so you will need to make sure that you control it for example if the office window moves, is minimized and the like.

Are native Windows Controls still used in .NET GUIs?

Is the stuff that is displayed when you create a GUI with WinForms or WPF still based on the native controls like Common Controls or any of the system provided Window Classes or is everything that is displayed "custom" drawn by the framework?
Note: I'm not talking about stuff like a file dialog, but GUI that was actually implemented with WinForms or WPF.
And yes, this is purely out of interest.
User HighCore commented:
WPF Uses an "HWND" for the Window objects, but then all other objects
(Controls) inside the Window are WPF objects not related to Win32 in
any way, wheareas AFAIK, winforms uses a separate HWND for each UI
element.
And indeed, using Spy++, we can observe the following when we create a test app with a simple dialog/window and a button on it:
MFC/native:
The app window is a Window (HWND) with the Window Class of #32770 (Dialog) (I used a "Dialog based" app.)
The Button is a separate Window (Class: Button)
Windows Forms:
The app window has the Window Class WindowsForms10.Window.8.app.0.2bf8098_r20_ad1 (oh my)
The Button is a separate Window (Class: WindowsForms10.BUTTON.app.0.2bf8098_r20_ad1)
WPF
There's only one top level Window, although for good measure I added a ComboBox, a ListBox, and a Menu to this window in the UI designer.
The Class of the only Window is: HwndWrapper[WpfApplication1.exe;;9b1aec0f-1b88-419c-8730-858906314cd9]
The Window Class names are actually quite interesting: With the MFC/native one you get the classes known for years and documented on MSDN. With Windows Forms, it does appear that it always uses the same class names. And with WPF it seems the name of a Class of a Window also incorporates the executable/process name.
So apparently MS thinks that using more than one Window per window isn't necessary anymore. I think I need to open a second question for that.
Windows Forms uses native controls for some UI elements. WPF draws everything on its own.

New window in Silverlight out-of-browser application

In a Silverlight application that doesn't run in a browser, is it possible to create a new top-level window? Or at least a child window?
I found some solution using the ChildWindow class, but even though my project is configured for Silverlight 4, that class can not be found.
I have a UserControl (XAML file) that I want to show as a new window. Using a tab control is not really an option unfortunately as the user has to be able to arrange windows to see more than one at once.
Any suggestions?
ChildWindow is part of the SDK, you need to add the System.Windows.Controls.dll to access the ChildWindow type.
I'm not sure you can get the ChildWindow to do what you are expecting. A ChildWindow is designed to present a window in a modal manner. However this modal behaviour is really a function of the ChildWindow template. It is possible to re-template to remove the modal behaviour. However I've never tried to manipulate multiple child windows. You could give it a go, the big question would be what happens if you close Child windows in a different order in which they were created?
In order to use a ChildWindow to present your UserControl it would probably be best for you to derive from ChildWindow, instead of UserControl. You may even find it would ultimately be better for you create base class between your specific Xaml and ChildWindow where you would put code that is common to all your windows.
I might be worth you noting the Silverlight 5 will support multiple windows.

How to add a WPF window to a WinForms App

I'm creating a HUD window for inspecting biz entities in my WinForms application.
I wanted to have a completely different style of window (minimize the content area and showing only the TitleBar, no system buttons, etc) so I created a WPF application for this.
The problem is that I want this Window to 'live' inside my WinForms application. I can't just add the WPF as an OwnedForm or set the main Form as the Owner of the WPF window.
So, how can achive this?
EDIT: Thanks to pst I found the answer. Here is the snippet:
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(_inspector);
WindowInteropHelper inspectorHelper = new WindowInteropHelper(_inspector);
inspectorHelper.Owner = this.Handle;
_inspector.Show();
A WPF Window has a Win32-window handle/context.
See WindowInteropHelper. You can use this with Win32 (or perhaps there is WinForms support?) to set the owner window of the WPF Window. Be aware the handle does not exist until the "source initialized" (?) event.
However, using just WinForms, you may be able to customize the titlebar as much as you need (you can overwrite the drawing itself via Win32, and I think you lose all the control boxes without going this far).
There are lots of google results on this topic if you use the correct keywords.

Have a wpf window inside another wpf window

Is there a way to host a WPF window inside another WPF window. I have a couple of somewhat complex forms in place. But now to simplify things I am trying to merge a few of them as tabpages inside one 'Dashboard' form.
Please note that I am not trying to host a Windows Form, but another WPF window
If you want tabpages, why not use a TabControl with UserControls inside ? If you need to transform one of these tabs to a floating window, just put the UserControl in a new Window...
Can I answer this question with another question; why would you not create them as controls rather than other WPF windows, that you want to host in the main WPF window?
a bit late on this, but I guess with WindowsForms interop you can put in WPF a WinForms control host and in that host put a WinForms control that hosts the handle of a WPF window
I think what you're asking for is MDI, Multiple Document Interface. Something like this might help.
Do note, however, that the MDI paradigm is largely shunned these days. There are usually better ways to achieve the same functionality.
I think you want to hosting contents of WPF Window1.xaml (page1.xaml) inside within another WPF Window.
Well...you can use Navigation. Instead running window1.xaml contents inside tab then you can work with your data inside Navigation. Navigation can run within WPF Window Application. You just design your form / UI in page1.xaml.
one another..MDI old and rusty. We want clear of top window nowadays.

Resources