如何设置Window.Owner到Outlook窗口 [英] How to set the Window.Owner to Outlook window

查看:517
本文介绍了如何设置Window.Owner到Outlook窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Outlook插件,弹出一个WPF窗口

I have an outlook plugin which pops up a WPF window

有没有一种方法来设置WPF的 Window.Owner 属性到Outlook?

Is there a way to set the WPF's Window.Owner property to Outlook?

推荐答案

荣誉给@reedcopsey为把我们在正确的轨道......

Kudos to @reedcopsey for putting us on the right track...

的技巧用于检索展望手柄使用反射来获得活动窗口的标题(标题的)和的FindWindow 的Win32 API来获取活动窗口的IntPtr 手柄(检查,探险等。的)。从<一个启发href="http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/6ec291f3-6f0f-4d25-aa4c-1f5779486881/">this MSDN论坛帖子。一旦你的活动窗口的句柄,您可以利用<一个href="http://msdn.microsoft.com/en-us/library/system.windows.interop.windowinterophelper.aspx"><$c$c>WindowInteropHelper管理主人的关系。

The trick for retrieving the Outlook Handle is using reflection to obtain the active window's title (Caption) and the FindWindow Win32 API to obtain the active window IntPtr handle (inspector, explorer, etc.). Inspired from this MSDN forum post. Once you have the active window handle, you can leverage WindowInteropHelper for managing the owner relationship.

Window yourWPFWindow = new Window();
dynamic activeWindow = Globals.ThisAddIn.Application.ActiveWindow();
IntPtr outlookHwnd = new OfficeWin32Window(activeWindow).Handle;
WindowInteropHelper wih = new WindowInteropHelper(yourWPFWindow);
wih.Owner = outlookHwnd;
yourWPFWindow.Show();

OfficeWin32Window(辅助​​类的)

OfficeWin32Window (Helper Class)

///<summary>
/// This class retrieves the IWin32Window from the current active Office window.
/// This could be used to set the parent for Windows Forms and MessageBoxes.
///</summary>
///<example>
/// OfficeWin32Window parentWindow = new OfficeWin32Window (ThisAddIn.OutlookApplication.ActiveWindow ());   
/// MessageBox.Show (parentWindow, "This MessageBox doesn't go behind Outlook !!!", "Attention !", MessageBoxButtons.Ok , MessageBoxIcon.Question );
///</example>
public class OfficeWin32Window : IWin32Window
{

    ///<summary>
    /// The <b>FindWindow</b> method finds a window by it's classname and caption.
    ///</summary>
    ///<param name="lpClassName">The classname of the window (use Spy++)</param>
    ///<param name="lpWindowName">The Caption of the window.</param>
    ///<returns>Returns a valid window handle or 0.</returns>
    [DllImport("user32")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    #region IWin32Window Members

    ///<summary>
    /// This holds the window handle for the found Window.
    ///</summary>
    IntPtr _windowHandle = IntPtr.Zero;

    ///<summary>
    /// The <b>Handle</b> of the Outlook WindowObject.
    ///</summary>
    public IntPtr Handle
    {
        get { return _windowHandle; }
    }

    #endregion

    ///<summary>
    /// The <b>OfficeWin32Window</b> class could be used to get the parent IWin32Window for Windows.Forms and MessageBoxes.
    ///</summary>
    ///<param name="windowObject">The current WindowObject.</param>
    public OfficeWin32Window(object windowObject)
    {
        string caption = windowObject.GetType().InvokeMember("Caption", System.Reflection.BindingFlags.GetProperty, null, windowObject, null).ToString();

        // try to get the HWND ptr from the windowObject / could be an Inspector window or an explorer window
        _windowHandle = FindWindow("rctrl_renwnd32\0", caption);
    }
}

这篇关于如何设置Window.Owner到Outlook窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆