我有一个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;
}
Run Code Online (Sandbox Code Playgroud)
该SaveFileDialog.ShowDialog(Window)方法需要一个窗口。我可以通过吗IWin32Window?除了 之外,还有其他方法可以Window从处理程序获取对象吗Control.FromHandle?
任何批评都将受到欢迎。
谢谢
编辑:
哦,还有该函数的辅助类:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了这段代码,所以我确实有一个关于它的附带问题:
它有什么FindWindow("rctrl_renwnd32\0", caption)作用?我根本不明白。特别是“rctrl_renwnd32\0”部分。
编辑:
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;
}
Run Code Online (Sandbox Code Playgroud)
编辑2:
我更新了逻辑,我删除了该函数并将其添加到我需要句柄的位置:
Inspector currentObject = Globals.ThisAddIn.Application.ActiveInspector();
var winh = currentObject as IOleWindow;
IntPtr win = winh.GetWindow();
Run Code Online (Sandbox Code Playgroud)
编辑3:
重组:
Inspector currentObject = Globals.ThisAddIn.Application.ActiveInspector();
var winh = currentObject as IOleWindow;
IntPtr win;
winh.GetWindow(out win);
Run Code Online (Sandbox Code Playgroud)
FindWindow方法检索类名和窗口名与指定字符串匹配的顶级窗口的句柄。第一个参数指定窗口类名称。第二个参数是窗口名称(窗口的标题)。如果此参数为 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) ;
}
Run Code Online (Sandbox Code Playgroud)
IWin32Window 接口的实例用于指定 Show 或 ShowDialog 方法的父窗口句柄。