如何将Window.Owner设置为Outlook窗口

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

有没有办法将WPF的Window.Owner属性设置为Outlook?

感谢@reedcopsey让我们走上正轨……

检索Outlook句柄的技巧是使用reflection获取活动窗口的标题( Caption )和FindWindow Win32 API以获取活动窗口IntPtr句柄( 检查器,资源管理器等 )。 灵感来自这篇MSDN论坛post 。 拥有活动窗口句柄后,您可以利用WindowInteropHelper来管理所有者关系。

检索Outlook句柄( 通过ActiveWindow

 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( 助手类

 /// /// This class retrieves the IWin32Window from the current active Office window. /// This could be used to set the parent for Windows Forms and MessageBoxes. /// /// /// OfficeWin32Window parentWindow = new OfficeWin32Window (ThisAddIn.OutlookApplication.ActiveWindow ()); /// MessageBox.Show (parentWindow, "This MessageBox doesn't go behind Outlook !!!", "Attention !", MessageBoxButtons.Ok , MessageBoxIcon.Question ); /// public class OfficeWin32Window : IWin32Window { /// /// The FindWindow method finds a window by it's classname and caption. /// ///The classname of the window (use Spy++) ///The Caption of the window. ///Returns a valid window handle or 0. [DllImport("user32")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); #region IWin32Window Members /// /// This holds the window handle for the found Window. /// IntPtr _windowHandle = IntPtr.Zero; /// /// The Handle of the Outlook WindowObject. /// public IntPtr Handle { get { return _windowHandle; } } #endregion /// /// The OfficeWin32Window class could be used to get the parent IWin32Window for Windows.Forms and MessageBoxes. /// ///The current WindowObject. 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); } } 

这可以通过WindowInteropHelper完成:

 WindowInteropHelper wih = new WindowInteropHelper(yourWindow); wih.Owner = outlookHwnd; yourWindow.Show();