Skip to content

Commit

Permalink
fix owner window show
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Jun 27, 2022
1 parent 6635d05 commit e31b3de
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
54 changes: 54 additions & 0 deletions RevitPythonShell/Helpers/ProcessManager.cs
Original file line number Diff line number Diff line change
@@ -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();
}

/// <summary>
/// Set process revert use revit
/// </summary>
/// <returns></returns>
public static bool SetActivateWindow()
{
IntPtr ptr = GetActivateWindow();
if (ptr != IntPtr.Zero)
{
return SetForegroundWindow(ptr);
}
return false;
}

/// <summary>
/// return active windows is active
/// </summary>
/// <returns></returns>
public static IntPtr GetActivateWindow()
{
return Process.GetCurrentProcess().MainWindowHandle;
}
}
}
1 change: 1 addition & 0 deletions RevitPythonShell/RevitCommands/ConfigureCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions RevitPythonShell/RevitCommands/IronPythonConsoleCommand.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -75,6 +77,8 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
}
});
});
gui.WindowStartupLocation = WindowStartupLocation.CenterScreen;
gui.SetRevitAsWindowOwner();
gui.ShowDialog();
return Result.Succeeded;
}
Expand Down
5 changes: 4 additions & 1 deletion RevitPythonShell/RevitCommands/NonModalConsoleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions RevitPythonShell/RevitPythonShell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers\ProcessManager.cs" />
<Compile Include="RevitCommands\CommandLoaderBase.cs" />
<Compile Include="Views\CompletionToolTip.cs" />
<Compile Include="RevitCommands\ConfigureCommand.cs" />
Expand Down

0 comments on commit e31b3de

Please sign in to comment.