views:

22

answers:

2

I'm building an assembly that runs as an "add-on" to a vendor's Outlook add-in. When it is time for me to execute my "action", I have to put up a simple window with a few simple controls. The vendor's add-in provides me with the parent window's integer handle. I am able to put up a form pretty easily with WinForms by adding are reference to System.Windows.Forms from my assembly and with the following code:

        FrmHistoryDisplay frm = new FrmHistoryDisplay();
        frm.ShowDialog(new ParentWindowWrapper(_parentWindowHandle));

where ParentWindowWrapper is a shim class around the window handle I'm given

        private class ParentWindowWrapper : IWin32Window {

        private int _parentWindowHandle;

        public ParentWindowWrapper(int parentWindowHandle) {
            _parentWindowHandle = parentWindowHandle;
        }

        public IntPtr Handle {
            get { return new IntPtr(_parentWindowHandle); }
        }
    }

The Form's ShowDialog method takes an IWin32Window implementor to wrap the parent's window handle.

This all works and seems simple enough. I was just wondering whether something similar can be done with a WPF window rather than a WinForm Form? Should I care?

+1  A: 

I will be use a method commented with:

"put a from pretty easly with WinForms"

Because this is supported then will be have more build-in functionality of your add-in in this way.

Svisstack
+1 I agree. Stick with winforms.
Chris Lively
@Chris: There may be very good reason to use WPF for portions of the UI, however. It is far more powerful in many cases. Without knowing the application in question, it's difficult to know whether it's a good idea or not.
Reed Copsey
@Reed: I think your answer below contradicted your comment here.
Chris Lively
@Chris: No - As I said in my answer, I'd potentially use WPF, but only for portions of the UI, not everything. I would not recommend using WinForms exclusively without more information.
Reed Copsey
+2  A: 

Yes, you can do similar with WPF to host a WPF window as owned or parented by an arbitrary Win32 window handle. Given a wHandle in the context of a WPF form method, you can do this:

var helper = new WindowInteropHelper(this);
helper.Owner = wHandle;

this.Show();

This will set up the WPF window as a standard top-level window that is owned by the given wHandle. Win32 window handle ownership is critical to modal dialog behavior, window activation and focus when clicking on any of the windows of a deactivated app, etc.

Whether you should use WinForms or WPF for your Outlook add-in is really up to you and your preferences. I'm pretty sure Outlook's UI uses neither WinForms nor WPF, so whatever you do will require that you make sure the right libraries are installed on the machine when your add-in is installed.

dthorpe