Reputation: 5865
I have a windows form [myForm] within a VSTO addin project - currently when I .Show() this form it is displayed outside the Excel application (on another monitor in my case), but I would like it to be displayed as an MdiChild of the Excel application hosting it, so within the main Excel application window.
myForm x = new myForm;
x.Show();
There is an overload of Show() that accepts an owner argument of type System.Windows.Forms.IWin32Window, but I'm not sure if that is how one might do this?
There is also an MdiParent property of the form, which is of type System.Windows.Forms.Form type, but in this case I want the parent to be the Excel application, not another windows form.
Upvotes: 1
Views: 1312
Reputation: 49395
The Hwnd property of the Window class returns an integer which stands for the window handle. But the Show method accepts an instance of the IWin32Window interface which exposes Win32 HWND handles. To get an instance of the interface you need to declare a class which implements that interface.
public class WindowImplementation : System.Windows.Forms.IWin32Window
{
public WindowImplementation(IntPtr handle)
{
_hwnd = handle;
}
public IntPtr Handle
{
get
{
return _hwnd;
}
}
private IntPtr _hwnd;
}
Then you can use the following code to get form shown specifying the parent Excel window handle.
form1.Show(new WindowImplementation(excelWindow.Hwnd));
Upvotes: 2