我应该使用哪种技术在我的外接程序 DLL 中弹出一个简单的表单?

发布于 2024-09-05 14:04:34 字数 863 浏览 3 评论 0原文

我正在构建一个程序集,该程序集作为供应商的 Outlook 加载项的“加载项”运行。当我执行“操作”时,我必须建立一个带有一些简单控件的简单窗口。供应商的加载项为我提供了父窗口的整数句柄。我可以通过从程序集中添加对 System.Windows.Forms 的引用并使用以下代码,使用 WinForms 轻松地建立一个表单:

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

其中 ParentWindowWrapper 是窗口句柄周围的垫片类,我给出了

        private class ParentWindowWrapper : IWin32Window {

        private int _parentWindowHandle;

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

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

Form 的 ShowDialog 方法采用一个 IWin32Window 实现者来包装父窗口句柄。

这一切都有效并且看起来很简单。我只是想知道是否可以使用 WPF 窗口而不是 WinForm 窗体来完成类似的操作?我应该关心吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍暮颜 2024-09-12 14:04:34

是的,您可以对 WPF 执行类似操作,以托管由任意 Win32 窗口句柄拥有或作为父级的 WPF 窗口。给定 WPF 表单方法上下文中的 wHandle,您可以执行以下操作:

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

this.Show();

这会将 WPF 窗口设置为给定 wHandle 拥有的标准顶级窗口。 Win32 窗口句柄所有权对于模式对话框行为、窗口激活以及单击已停用应用程序的任何窗口时的焦点等至关重要。

您是否应该为 Outlook 加载项使用 WinForms 还是 WPF 实际上取决于您和您的偏好。我非常确定 Outlook 的 UI 既不使用 WinForms 也不使用 WPF,因此无论您做什么,都需要确保在安装加载项时在计算机上安装了正确的库。

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.

多情癖 2024-09-12 14:04:34

我将使用一种带有注释的方法:

“很容易用
WinForms”

因为这是受支持的,所以您的外接程序将以这种方式拥有更多内置功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文