Reference Article: 《BuilderCoder Revit Parent Window 》

When we use modal form, when focus is not on form, form will hide behind all layers. If encountering unfamiliar user, will click buttons continuously, creating multiple forms. And interaction for single form is also not friendly.

Searching in website, often tell everyone to set TopMost, but this operation sets form to top most layer. When we use other applications like browser, form still displays on top layer which does not conform to interaction logic.

By setting IWin32Window set Revit as parent window of form, but we only get its focus via

            var rvtPtr = Process.GetCurrentProcess().MainWindowHandle;

At this time we need to inherit IWin32Window to create a class, finally apply to Show() function

 public class WindowHandle:IWin32Window
        {
            private IntPtr _handle;

            public WindowHandle(IntPtr h)
            {
                Debug.Assert(IntPtr.Zero!=h,"expected non-null window handle");

                _handle = h;
            }
            public IntPtr Handle => _handle;
        }

Assignment:

            var rvtPtr = Process.GetCurrentProcess().MainWindowHandle;
            RegisterForm sign = new RegisterForm(new Point(rec.Left,rec.Top));
            sign.ShowDialog(new WindowHandle(rvtPtr));

Finally realized, when clicking Revit, form hidden behind will show up, and close automatically following Revit process