Re - Focus / Activate on modeless form

Hello,
I’m struggling with focusing/activating modeless form.
Could you give me some direction how should I code it or some reference?
Let’s talk about simple wpf xaml modeless form with a button. Button activates selection in revit or pick object, whatever. After selection (some selections need ‘finish’ button confirmation) or basically after calling method Revit or rather Windows loose focus. I want to activate wpf form once again. It’s still in foreground, but not active. Goal is to code keyboard shortcut for this button, it won’t work if form is not active - or maybe it’s possible, but form activation is still a big thing for me :slight_smile:
Thanks!

the pyTiba extension has a couple of examples, I haven’t tried to go down the modeless hole myself yet.

I would assume that after revit returns the elements from the selection, you’d just call Focus() on your main window. I haven’t specifically tested this though.
UIElement.Focus Method (System.Windows) | Microsoft Docs

Hi,
I found a solution :grinning:

Sample below:
Run the script:

if __name__ == '__main__':
    modeless_form = ModelessForm("UI.xaml")
    xaml_window = ctypes.windll.user32.GetForegroundWindow()

In

class ModelessForm(WPFWindow):
    def __init__(self, xaml_file_name):
            WPFWindow.__init__(self, xaml_file_name)
            self.Show()
    def SelectButtonClick(self, sender, args):
            # custom event @CyrilWaechter/pyRevitMEP
            customizable_event.raise_event(bt_manual_selection)

and somewhere outside the class I have a function for selection:

bt_manual_selection():
    try:
            picked_elements = uidoc.Selection.PickObjects(UI.Selection.ObjectType.Element,  'Pick elements')
        except OperationCanceledException:
                pass
    ctypes.windll.user32.SetForegroundWindow(xaml_window)

So basically:

import ctypes
ctypes.windll.user32.GetForegroundWindow()
ctypes.windll.user32.SetForegroundWindow(xaml_window)
1 Like