Revit Application hides in background after closing Add-in (modelles form)

c_hanschen
Advocate
Advocate

Revit Application hides in background after closing Add-in (modelles form)

c_hanschen
Advocate
Advocate

Hello,

 

I have more than 1 add-in all showing this behavior:

 

The modelles Form is shown by the Idling Event, doing it's thing, but after closing the Fom the Revit Application is gone, hide in the background, lost it's focus.

 

I found this:

https://thebuildingcoder.typepad.com/blog/2017/10/modeless-form-keep-revit-focus-and-on-top.html 

 

but is says:

This was written about 5 years ago.

As of the next major release of Revit, the method described there will no longer work.

An easy way to fix this will be provided. The Revit API will include new API calls providing an official way to get the application window handle:

  • Get the handle of the Revit main window.
  • Return the main window handle of the Revit application. This handle should be used when displaying modal dialogs and message windows to ensure that they are properly parented. This property replaces the System.Diagnostics.Process.GetCurrentProcess() MainWindowHandle property.

I use the MainWindowHandle from the UIApplication when calling the Show event of the Form, so the Form knows it's owner/parent. It's stays on top of Revit, so it is called correct, but still this behavior.

 

Because of the post from Jerremy ages ago, I thought I could find the solution on the internet, but can't find it.

 

Who can help me fix this annoying issue?

 

Thanks!!

 

Chris Hanschen

LKSVDD Architects, The Netherlands

 

 

 

 

0 Likes
Reply
1,096 Views
10 Replies
Replies (10)

Sean_Page
Collaborator
Collaborator

I am not entirely sure that is what you are after (or if this is even the correct approach) but this is what I have been using and it works well for Modal or Modless dialogs. It was was a combination of @jeremytammik and other sources.

 

public class JtWindowHandle : IWin32Window
	{
		IntPtr _hwnd;
		public JtWindowHandle(IntPtr h)
		{
			Debug.Assert(IntPtr.Zero != h,
			  "expected non-null window handle");
			_hwnd = h;
		}
		public IntPtr Handle
		{
			get
			{
				return _hwnd;
			}
		}
	}

 

 

WPF.AbbreviationsWPF form = new WPF.AbbreviationsWPF(Abbreviations);
JtWindowHandle rvtwin = new JtWindowHandle(commandData.Application.MainWindowHandle);
System.Windows.Interop.WindowInteropHelper helper = new System.Windows.Interop.WindowInteropHelper(form)
{
	Owner = rvtwin.Handle
};
form.Show();

 

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect

c_hanschen
Advocate
Advocate

@Sean_Page 

You Are using WPF, my tools are build with Windows Forms.

(Rewriting to WPF is still on the to-do-list 😃 )

 

I will try to rewrite this for use with Windows Forms, when succeeded, I will share my code on this topic.

 

Thanks for your reply!!

 

Greetings, Chris, The Netherlands

 

0 Likes

ricaun
Advisor
Advisor

You could take a look at the RevitLookup project, now the Form is modeless.

 

Something in here:

See yaa.

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

Sean_Page
Collaborator
Collaborator

I didn't realize you were looking for WinForms.

 

Here is how I do that as well.

 

using ComponentManager = Autodesk.Windows.ComponentManager;

 

 

IWin32Window revit_window = new JtWindowHandle(ComponentManager.ApplicationWindow);
			Forms.WarningsForm frm = new Forms.WarningsForm(uiapp);
			frm.Show(revit_window);

 

 

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect

c_hanschen
Advocate
Advocate

@Sean_Page ,

 

Thanks again for your reply!

 

So you are calling the Show Event with argument 'revit_window' which you acquire from  Autodesk.Windows.ComponentManager.ApplicationWindow.

 

I thought, from the posts of Jemmery, that you should use: uiapp.MainWindowHandle.

 

In earlier versions of Revit I used: (VB.Net)

Dim process As Process = Process.GetCurrentProcess()
Dim MyIntPtr As IntPtr = process.MainWindowHandle 

 

So know I am confused ?

 

I will try your lines of code, but can't call Autodesk.Windows.ComponentManager, After 'Autodesk.' I only get 'Revit'.

How do I get 'Autodesk.Windows' ?

 

Thanks again.

 

Chris

 

0 Likes

c_hanschen
Advocate
Advocate

Please Look at the screenshot, I used 2 ways of getting JtWindowHandle:

Revit 2018 and older: Using Process.GetCurrentProcess

Revit 2019 and later: Using uiapp.MainWindowHandle

 

It says:

chanschen_0-1637173463133.png

So, I did exactly what it says, but using uiapp.MainWindowHandle let's me down (Revit hides in the back), Process.GetCurrentProcess seems to work fine (not hidden in the background).

 

So there are 3 Ways:

- Using Process.GetCurrentProcess

- Using uiapp.MainWindowHandle

- Using Autodesk.Windows.ComponentManager.ApplicationWindow

 

@Sean_Page , can you clear this out for me?

 

Thanks,

 

Chris

0 Likes

Sean_Page
Collaborator
Collaborator

You have to add the AdWindows reference to use the Component Manager.

 

C:\Program Files\Autodesk\Revit 2022\AdWindows.dll

 

However, from what I can tell, I am seeing the same behavior in all three cases.

1. Window is Modless

2. If I Minimize Revit the Window also goes

3. If I Restore Revit the Window comes back

4. If I select the Window anywhere then Revit is now Active

5. If I close "X" the Window, Revit is not activated and will stay "under" any othe windows that are currently above it.

 

Are you seeing some other behavior or are you trying to do something completely different? 

 

IWin32Window revit_window = new JtWindowHandle(uiapp.MainWindowHandle);
//IWin32Window revit_window = new JtWindowHandle(ComponentManager.ApplicationWindow);
Forms.WarningsForm frm = new Forms.WarningsForm(uiapp);
frm.Show(revit_window);
Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect

ricaun
Advisor
Advisor

To add the References I usually use this Revit_All_Main_Versions_API_x64 PackageReference.

 

Includes these assemblies:

  • RevitAPI.dll
  • RevitAPIUI.dll
  • AdWindows.dll
  • UIFramework.dll

 

Something like this on the csproj.

 

<ItemGroup>
  <PackageReference Include="Revit_All_Main_Versions_API_x64" Version="2022.*" IncludeAssets="build; compile" />
</ItemGroup>

 

And inside the Wpf/Window something like this.

public SimpleView()
{
    InitializeComponent();
    new System.Windows.Interop.WindowInteropHelper(this) { Owner = Autodesk.Windows.ComponentManager.ApplicationWindow };
}

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

c_hanschen
Advocate
Advocate

the third case (Autodesk.Windows.ComponentManager.ApplicationWindow) is not tested by me, I will give this a try within days and report back to you.
for now: Process.GetCurrentProcess works better than uiapp.MainWindowHandle (for me, but you should not use Process.GetCurrentProcess if you do what Autodesk says 🙂)

0 Likes

c_hanschen
Advocate
Advocate

Still struggling 😞

 

All 3 methods described before (and listed in bold in this topic) are working fine for calling the Modelles WinForm, The behavior is correct as long as the WinForm is not closed.

 

Process.GetCurrentProcess

Work for me, After Closing the Winform Revit does Not Hide in the background, but Revit API says it is not reliable, so you should not use this method.

 

uiapp.MainWindowHandle

After Closing the Winform, Revit Hides behind an other application. (which was visible before setting focus to Revit)

 

ComponentManager.ApplicationWindow

Exactly the same behavior as uiapp.MainWindowHandle

 

So it seems like, you have to use the RevitMainWindowHandle when Calling the Show Event for the proper behavior of the Form (WinForm and WPF), but also have to set the .Owner of the Form after InitializeComponent() as well.

The example to do that works for WPF, but I can't get it to work for a WinForm, because the .Owner of the WinForm only accepts an other WinForm.

 

I am stuck, who can help me out?

 

Thanks,

 

Chris Hanschen

LKSVDD architects

The Netherlands

 

 

0 Likes