public static System.Drawing.Rectangle GetWindowRect(this IVisio.Window window) { // MSDN: http://msdn.microsoft.com/en-us/library/office/ms367542(v=office.14).aspx int left, top, height, width; window.GetWindowRect(out left, out top, out width, out height); var r = new System.Drawing.Rectangle(left, top, width, height); return(r); }
/// <summary> /// Allows a windows form to be used as the UI for an anchor window /// </summary> public static void AttachWindowsForm( IVisio.Window anchor_window, System.Windows.Forms.Form the_form) { if (anchor_window == null) { throw new System.ArgumentNullException(nameof(anchor_window)); } if (the_form == null) { throw new System.ArgumentNullException(nameof(the_form)); } // Show the form as a modeless dialog. the_form.Show(); // Get the window handle of the form. int hwnd = the_form.Handle.ToInt32(); var hwnd_as_intptr = new System.IntPtr(hwnd); // Set the window properties to make it a visible child window. const int window_prop_index = VisioAutomation.Internal.NativeMethods.GWL_STYLE; const int window_prop_value = VisioAutomation.Internal.NativeMethods.WS_CHILD | VisioAutomation.Internal.NativeMethods.WS_VISIBLE; VisioAutomation.Internal.NativeMethods.SetWindowLong(hwnd_as_intptr, window_prop_index, window_prop_value); // Set the anchor bar window as the parent of the form. VisioAutomation.Internal.NativeMethods.SetParent(hwnd, anchor_window.WindowHandle32); // Force a resize of the anchor bar so it will refresh. int left, top, width, height; anchor_window.GetWindowRect(out left, out top, out width, out height); anchor_window.SetWindowRect(left, top, width - 1, height - 1); anchor_window.SetWindowRect(left, top, width, height); // Set the dock property of the form to fill, so that the form // automatically resizes to the size of the anchor bar. the_form.Dock = System.Windows.Forms.DockStyle.Fill; // had to set to false to prevent a resizing problem (it was originally set to true) the_form.AutoSize = true; }
public static bool AddFormToAnchorWindow(Visio.Window anchorWindow, Form form) { int left, top, width, height; int windowHandle; bool bSuccess = false; try { // Show the form as a modeless dialog. form.Show(); // Get the window handle of the form. windowHandle = form.Handle.ToInt32(); // Set the form as a visible child window. if (NativeMethods.SetWindowLongW(windowHandle, NativeMethods.GWL_STYLE, NativeMethods.WS_CHILD | NativeMethods.WS_VISIBLE) != 0) { // La forme est maintenant une fenêtre enfant visible // Affectation de la anchor window en tant que parent de la forme if (NativeMethods.SetParent(windowHandle, anchorWindow.WindowHandle32) != 0) { // Le parent de la forme est maintenant la anchor window // Set the dock property of the form to fill, so that the form // automatically resizes to the size of the anchor bar. form.Dock = System.Windows.Forms.DockStyle.Fill; // Resize the anchor bar so it will refresh. anchorWindow.GetWindowRect(out left, out top, out width, out height); anchorWindow.SetWindowRect(left, top, width, height + 1); anchorWindow.SetWindowRect(left, top, width, height); bSuccess = true; } } } catch (Exception err) { } return(bSuccess); }