首页 文章

从VSTO Addin获取Outlook窗口

提问于
浏览
1

我有 Outlook 2013 VSTO addin . 我想将我创建的saveFileDialog居中 . 要执行此操作,您需要将其传递给父级的 Window 对象 . 我不确定 IWin32WindowWindow 是否相同,但这就是我所拥有的 .

public IWin32Window getWindowHandle()
{
    dynamic activeWindow = Globals.ThisAddIn.Application.ActiveWindow();
    IntPtr outlookHwnd = new OfficeWin32Window(activeWindow).Handle;
    IWin32Window win = Control.FromHandle(outlookHwnd);

    return win;
}

SaveFileDialog.ShowDialog(Window) 方法占用一个窗口 . 我能把它传给 IWin32Window 吗?有没有办法从 Control.FromHandle 以外的处理程序中获取 Window 对象?

任何批评都会受到欢迎 .

谢谢

编辑:

哦,该函数的助手类:

public class OfficeWin32Window : IWin32Window
{
   [DllImport("user32")]
   public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

   IntPtr _windowHandle = IntPtr.Zero;

   public IntPtr Handle
   {
      get { return _windowHandle; }
   }

   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);
   }

}

我得到了这个代码,所以我确实有一个关于它的附带问题:
What does FindWindow("rctrl_renwnd32\0", caption) do? I don't get it at all. Especially the "rctrl_renwnd32\0" part.

编辑:

IOleWindow类的新功能:

public IOleWindow getWindowHandle()
    {
        dynamic activeWindow = Globals.ThisAddIn.Application.ActiveWindow();
        IOleWindow win = activeWindow as IOleWindow;
        window = win.GetWindow();
        //IntPtr outlookHwnd = new OfficeWin32Window(activeWindow).Handle;
        //IWin32Window wind = Control.FromHandle(outlookHwnd);

        return window;
    }

EDIT2:

我的更新逻辑,我删除了该功能,并将其添加到我需要的位置:

Inspector currentObject = Globals.ThisAddIn.Application.ActiveInspector();
var winh = currentObject as IOleWindow;
IntPtr win = winh.GetWindow();

EDIT3:

重组:

Inspector currentObject = Globals.ThisAddIn.Application.ActiveInspector();
var winh = currentObject as IOleWindow;
IntPtr win;
winh.GetWindow(out win);

1 回答

  • 4

    FindWindow方法检索顶级窗口的句柄,该窗口的类名和窗口名与指定的字符串匹配 . 第一个参数指定窗口类名称 . 第二个参数是窗口名称(窗口的 Headers ) . 如果此参数为NULL,则所有窗口名称都匹配 .

    您需要将Explorer或Inspector窗口强制转换为IOleWindow接口,并使用GetWindow方法获取窗口句柄,而不是使用FindWindow函数 .

    /// <summary>
     /// Implemented and used by containers and objects to obtain window handles 
     /// and manage context-sensitive help.
     /// </summary>
     /// <remarks>
     /// The IOleWindow interface provides methods that allow an application to obtain  
     /// the handle to the various windows that participate in in-place activation, 
     /// and also to enter and exit context-sensitive help mode.
     /// </remarks>
     [ComImport]
     [Guid("00000114-0000-0000-C000-000000000046")]
     [InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
     public interface IOleWindow
     {
         /// <summary>
         /// Returns the window handle to one of the windows participating in in-place activation 
         /// (frame, document, parent, or in-place object window).
         /// </summary>
         /// <param name="phwnd">Pointer to where to return the window handle.</param>
         void GetWindow (out IntPtr phwnd) ;
    
         /// <summary>
         /// Determines whether context-sensitive help mode should be entered during an 
         /// in-place activation session.
         /// </summary>
         /// <param name="fEnterMode"><c>true</c> if help mode should be entered; 
         /// <c>false</c> if it should be exited.</param>
         void ContextSensitiveHelp ([In, MarshalAs(UnmanagedType.Bool)] bool fEnterMode) ;
     }
    

    IWin32Window接口的实例用于指定Show或ShowDialog方法的父窗口句柄 .

相关问题