From e31b3def251bd46f83e3f26305f5af7c47f3e5b3 Mon Sep 17 00:00:00 2001 From: Chuong Ho <31106432+chuongmep@users.noreply.github.com> Date: Mon, 27 Jun 2022 13:39:00 +0700 Subject: [PATCH] fix owner window show --- RevitPythonShell/Helpers/ProcessManager.cs | 54 +++++++++++++++++++ .../RevitCommands/ConfigureCommand.cs | 1 + .../RevitCommands/IronPythonConsoleCommand.cs | 4 ++ .../RevitCommands/NonModalConsoleCommand.cs | 5 +- RevitPythonShell/RevitPythonShell.csproj | 1 + 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 RevitPythonShell/Helpers/ProcessManager.cs diff --git a/RevitPythonShell/Helpers/ProcessManager.cs b/RevitPythonShell/Helpers/ProcessManager.cs new file mode 100644 index 0000000..e3fc8c8 --- /dev/null +++ b/RevitPythonShell/Helpers/ProcessManager.cs @@ -0,0 +1,54 @@ +using System; +using System.ComponentModel; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Windows; +using System.Windows.Interop; + +namespace RevitPythonShell.Helpers +{ + public static class ProcessManager + { + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool SetForegroundWindow(IntPtr hWnd); + + public static void SetRevitAsWindowOwner(this Window window) + { + if (null == window) { return; } + window.WindowStartupLocation = WindowStartupLocation.CenterScreen; + WindowInteropHelper helper = new WindowInteropHelper(window); + helper.Owner = GetActivateWindow(); + window.Closing += SetActivateWindow; + } + + private static void SetActivateWindow(object sender, CancelEventArgs e) + { + SetActivateWindow(); + } + + /// + /// Set process revert use revit + /// + /// + public static bool SetActivateWindow() + { + IntPtr ptr = GetActivateWindow(); + if (ptr != IntPtr.Zero) + { + return SetForegroundWindow(ptr); + } + return false; + } + + /// + /// return active windows is active + /// + /// + public static IntPtr GetActivateWindow() + { + return Process.GetCurrentProcess().MainWindowHandle; + } + } +} \ No newline at end of file diff --git a/RevitPythonShell/RevitCommands/ConfigureCommand.cs b/RevitPythonShell/RevitCommands/ConfigureCommand.cs index ec9ba3e..5ebb07b 100644 --- a/RevitPythonShell/RevitCommands/ConfigureCommand.cs +++ b/RevitPythonShell/RevitCommands/ConfigureCommand.cs @@ -16,6 +16,7 @@ class ConfigureCommand: IExternalCommand public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { var dialog = new ConfigureCommandsForm(); + dialog.StartPosition = FormStartPosition.CenterScreen; dialog.ShowDialog(); MessageBox.Show("Restart Revit to see changes to the commands in the Ribbon", "Configure RevitPythonShell", MessageBoxButtons.OK, MessageBoxIcon.Information); diff --git a/RevitPythonShell/RevitCommands/IronPythonConsoleCommand.cs b/RevitPythonShell/RevitCommands/IronPythonConsoleCommand.cs index 22826cf..4a2fff0 100644 --- a/RevitPythonShell/RevitCommands/IronPythonConsoleCommand.cs +++ b/RevitPythonShell/RevitCommands/IronPythonConsoleCommand.cs @@ -1,10 +1,12 @@ using System; using System.Threading; +using System.Windows; using System.Windows.Threading; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Microsoft.Scripting; +using RevitPythonShell.Helpers; using RevitPythonShell.Views; using RpsRuntime; @@ -75,6 +77,8 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme } }); }); + gui.WindowStartupLocation = WindowStartupLocation.CenterScreen; + gui.SetRevitAsWindowOwner(); gui.ShowDialog(); return Result.Succeeded; } diff --git a/RevitPythonShell/RevitCommands/NonModalConsoleCommand.cs b/RevitPythonShell/RevitCommands/NonModalConsoleCommand.cs index c12d972..26e682c 100644 --- a/RevitPythonShell/RevitCommands/NonModalConsoleCommand.cs +++ b/RevitPythonShell/RevitCommands/NonModalConsoleCommand.cs @@ -2,11 +2,13 @@ using System.Collections.Generic; using System.Diagnostics; using System.Threading; +using System.Windows; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Microsoft.Scripting; using Microsoft.Scripting.Hosting; +using RevitPythonShell.Helpers; using RevitPythonShell.Views; using RpsRuntime; @@ -67,8 +69,9 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme commandCompletedEvent.WaitOne(); }); }); - gui.Topmost = true; gui.Title = gui.Title.Replace("RevitPythonShell", "RevitPythonShell (non-modal)"); + gui.WindowStartupLocation = WindowStartupLocation.CenterScreen; + gui.SetRevitAsWindowOwner(); gui.Show(); return Result.Succeeded; } diff --git a/RevitPythonShell/RevitPythonShell.csproj b/RevitPythonShell/RevitPythonShell.csproj index b83459e..020a1af 100644 --- a/RevitPythonShell/RevitPythonShell.csproj +++ b/RevitPythonShell/RevitPythonShell.csproj @@ -81,6 +81,7 @@ +