从 VSTO 插件获取 Outlook Window
Getting Outlook Window from VSTO Addin
我有一个 Outlook 2013 VSTO 插件。我想将我创建的 saveFileDialog 居中。为此,您需要向它传递父级的 Window
对象。我不确定 IWin32Window
和 Window
是否相同,但这是我所拥有的。
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)
方法采用 window。我可以将它传递给 IWin32Window
吗?有没有办法从 Control.FromHandle
以外的处理程序获取 Window
对象?
欢迎大家批评指正。
谢谢
编辑:
哦,还有那个函数的助手 class:
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[=12=]", caption);
}
}
我得到了这段代码,所以我有一个关于它的附带问题:
FindWindow("rctrl_renwnd32[=23=]", caption)
有什么作用?我完全不明白。特别是“rctrl_renwnd32[=57=]”部分。
编辑:
IOleWindow 的新功能class:
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;
}
编辑 2:
我更新的逻辑,我删除了函数并将其添加到我需要句柄的地方:
Inspector currentObject = Globals.ThisAddIn.Application.ActiveInspector();
var winh = currentObject as IOleWindow;
IntPtr win = winh.GetWindow();
编辑 3:
重组:
Inspector currentObject = Globals.ThisAddIn.Application.ActiveInspector();
var winh = currentObject as IOleWindow;
IntPtr win;
winh.GetWindow(out win);
FindWindow 方法检索 top-level window 的句柄,其 class 名称和 window 名称与指定的字符串匹配。第一个参数指定 window class 名称。第二个参数是 window 名称(window 的标题)。如果此参数为 NULL,则所有 window 个名称匹配。
您需要将 Explorer 或 Inspector window 转换为 IOleWindow interface and use the GetWindow 方法来获取 window 句柄,而不是使用 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 方法的 parent window 句柄。
我有一个 Outlook 2013 VSTO 插件。我想将我创建的 saveFileDialog 居中。为此,您需要向它传递父级的 Window
对象。我不确定 IWin32Window
和 Window
是否相同,但这是我所拥有的。
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)
方法采用 window。我可以将它传递给 IWin32Window
吗?有没有办法从 Control.FromHandle
以外的处理程序获取 Window
对象?
欢迎大家批评指正。
谢谢
编辑:
哦,还有那个函数的助手 class:
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[=12=]", caption);
}
}
我得到了这段代码,所以我有一个关于它的附带问题:FindWindow("rctrl_renwnd32[=23=]", caption)
有什么作用?我完全不明白。特别是“rctrl_renwnd32[=57=]”部分。
编辑:
IOleWindow 的新功能class:
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;
}
编辑 2:
我更新的逻辑,我删除了函数并将其添加到我需要句柄的地方:
Inspector currentObject = Globals.ThisAddIn.Application.ActiveInspector();
var winh = currentObject as IOleWindow;
IntPtr win = winh.GetWindow();
编辑 3:
重组:
Inspector currentObject = Globals.ThisAddIn.Application.ActiveInspector();
var winh = currentObject as IOleWindow;
IntPtr win;
winh.GetWindow(out win);
FindWindow 方法检索 top-level window 的句柄,其 class 名称和 window 名称与指定的字符串匹配。第一个参数指定 window class 名称。第二个参数是 window 名称(window 的标题)。如果此参数为 NULL,则所有 window 个名称匹配。
您需要将 Explorer 或 Inspector window 转换为 IOleWindow interface and use the GetWindow 方法来获取 window 句柄,而不是使用 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 方法的 parent window 句柄。