[英]How to set the Window.Owner to Outlook window
我有一個outlook插件,彈出一個WPF窗口
有沒有辦法將WPF的Window.Owner
屬性設置為Outlook?
感謝@reedcopsey讓我們走上正軌......
檢索Outlook句柄的技巧是使用反射獲取活動窗口的標題( Caption )和FindWindow
Win32 API以獲取活動窗口IntPtr
句柄( 檢查器,資源管理器等 )。 靈感來自這篇MSDN論壇帖子 。 擁有活動窗口句柄后,您可以利用WindowInteropHelper
來管理所有者關系。
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();
///<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);
}
}
這可以通過WindowInteropHelper完成:
WindowInteropHelper wih = new WindowInteropHelper(yourWindow);
wih.Owner = outlookHwnd;
yourWindow.Show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.