Excel & WPF modal form - wpf

I've got this situation. (.net 3.5)
A Winform application that with OleAutomation and Office Interop create an excel, a toolbar and handlers for buttons in the toolbar.
Now we've got some functionality that shows the user modal windows (winform 2.0) with the method:
form.ShowDialog(new ExcelHwndWrapper(objExcelApplication.HWND))
And the "owner" of the modal form is set to the "excel" window.
Now I would like to create some WPF Window instead of Winforms due to layout requirements for new functionalities.
Is there any way to Show a WPF ModalDialog "over" excel Window ?
I found something for showing a WPF ModalDialog "over" a winform, but nothing over excel.
Thanks.

When using VSTO and Office 2007, I've had trouble using WPF Windows as modal dialogues. What I found that worked much better was creating a Winforms Form and putting an ElementHost control inside it, then putting my WPF content inside that.
When I used WPF Windows without the Winforms wrapper I ran into trouble with properly capturing keyboard input, among other things.
As long as you make the ElementHost stretch to fill the entire dialogue it'll look like you have a WPF Window.

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.

WPF Interoperability: How to get Reference of Host Windows Form in WPF User Control

I've developed an Outlook Addin and UI of the addin is created in WPF. The WPF User controls are hosted in the Windows Forms. All my functionality resides inside the WPF user controls and I've provided a Close form functionality. Can somebody tell me how to get the reference of the Windows Form in the code behind of WPF user control which is hosted in the elementhost available in the Windows Form, so that I can put the frmHost.Hide() code in the WPF user control.
Thanks
Nitin
I'm not familiar with WPF Outlook addin, but in the control hierarchy, I suppose your WPF control is ultimately the (grand) child of an ElementHost control. Once you have a reference to it, you can get to the Winform host (Parent property, etc.).

WPF Component in Windows Application

i have created one component in WPF. its working fine with WPF but when i tried to use it in windows application i am getting one issue.
i have one textbox in that control so in when the control hosted in windows applicaion and i try to type something in that textbox its not working.
i tried to put some messageboxes in "KeyPreview", "KeyDown" and "Textxchanged" events any of these events have not been fired.
so is there any limitation for WPF usercontrols usage in windows application??
one more thing i have used Dispatcher timer in the usercontrol.
This MSDN walkthrough shows how to use a WPF user control in Windows Forms. It may help you in your query.
http://msdn.microsoft.com/en-us/library/ms745781.aspx

Is it possible to have a project containing both Winforms and WPF?

Is it possible to have a project containing both Winforms and WPF?
Say a WinForm project that is transformed step by step(form by form) in a WPF one, will be possible to have a Winform opening on a button, and a WPF one opening on a other button?
Yes. You have to pick one technology to display each physical window and control in your app, but there's no reason why you can't mix and match.
For example:
A WinForms window can show a WPF window.
A WPF window can show a WinForms window.
A WinForms window can contain WPF content (see the ElementHost control).
A WPF window can contain WinForms controls (see the WindowsFormsHost control).
This works great.
One can have WPF windows in Windows Forms and Windows Forms windows in WPF
http://msdn.microsoft.com/en-us/library/ms745781.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.aspx
Adding Winforms to WPF projects can be done smoothly (directly from the "Add new item" menu), but there is not straight option to add a WPF window to a Winforms project. Still, I handled to do it following these steps:
Add a WPF User Control (this option is available on the "Add new
item" menu) and then convert it into a WPF Window. Modify the XAML
changing the UserControl parent tag to Window, and remove the
inheritance from UserControl (all of this is explained in this link).
Add a reference to System.Xaml.dll. See this link.
Add a reference to System.Windows.dll (I found it on my computer on this path: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5. Be aware it might be different in yours). See this link.
What you might be looking for is the ElementHost control. What it lets you do is take WPF content and host it in a Windows Forms window. More details are here:
http://msdn.microsoft.com/en-us/library/ms745781.aspx
There is also a control that lets you do the reverse: host Windows Forms content from within WPF:
http://nayyeri.net/host-windows-forms-controls-in-wpf
Between the two, you can move the 'dividing line' between WPF and Windows Forms with some degree of flexibility.
There is at one caveat you'll need to keep in mind. Windows Forms works internally in terms of HWND's... a window managed by the legacy Windows window manager (which handles the z-order). WPF doesn't do this... A WPF tree is typically rendered into a single HWND', and it's WPF that manages things like z-order. What this means to you is that z-order doesn't always work the way you expect it to, and there are things you can't do with hosted Windows Forms controls that you can do with traditional WPF elements. (There is actually a way to solve this, but it involves periodically rendering the HWND into a memory bitmap, rendering that bitmap into a WPF surface, and then redirecting events directed to the WPF surface to the underlying HWND. This is powerful, but tricky and difficult to get right.)
I see no objection to do that.(I have in WinForms Application WPF windows)
Many of the examples used MessageBox.Show which is part of the Windows.Forms.
Of course you must rewrite all windows, not only controls.

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.

Resources