简体   繁体   中英

Which technology should I use to pop up a simple form in my add-in DLL?

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM