I have an Outlook 2013 VSTO addin. I want to center my saveFileDialog that I create. To do this you need to pass it a Window
object of the parent. I'm not sure if IWin32Window
and Window
are the same, but here's what I have.
public IWin32Window getWindowHandle()
{
dynamic activeWindow = Globals.ThisAddIn.Application.ActiveWindow();
IntPtr outlookHwnd = new OfficeWin32Window(activeWindow).Handle;
IWin32Window win = Control.FromHandle(outlookHwnd);
return win;
}
The SaveFileDialog.ShowDialog(Window)
method takes a window. Can I pass it the IWin32Window
? Is there a way to get the Window
object from the handler other than Control.FromHandle
?
Any criticisms would be welcome.
Thanks
EDIT:
Oh and the helper class for that function:
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);
}
}
I was given this code, so I do have a side question about it:
What does FindWindow("rctrl_renwnd32\0", caption)
do? I don't get it at all. Especially the "rctrl_renwnd32\0" part.
EDIT:
New function with 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;
}
EDIT2:
My updated logic, I removed the function and added this in the place I need tthe handle:
Inspector currentObject = Globals.ThisAddIn.Application.ActiveInspector();
var winh = currentObject as IOleWindow;
IntPtr win = winh.GetWindow();
EDIT3:
Restructured:
Inspector currentObject = Globals.ThisAddIn.Application.ActiveInspector();
var winh = currentObject as IOleWindow;
IntPtr win;
winh.GetWindow(out win);