Jump to content

Gui TopMost and Focus issue


Go to solution Solved by SOLVE-SMART,

Recommended Posts

Hello,

I have a GUI as in the attached image.

When I click the button, the text should be inserted into the active cell as copy-paste (Ctrl+C & Ctrl+V) , but it is not, since it will switch focus from Excel to GUI when the button is clicked.

The excel is just an example, it could be any other application.

Is there a way to keep the GUI on top and also "lose" it's focus at the same time so that the text can be submitted to other window on button click?

ย 

Thank you.

IMG_20241001_093032353.jpg

Link to comment
Share on other sites

  • Solution
Posted (edited)

Hi @JohnyXย ๐Ÿ‘‹ ,

please: next time let us see an example script of yours. Show a bit more effort, this would make our live and yours more easy.
Fortunately I had a similar script laying around and only had to adjust some things.

That's a example which does not contain error handling etc., but your attention should be going to the $iGuiTopMostFlag (*\AutoIt3\Include\WindowsConstants.au3) and the WinActivate() function.

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y

Global $hGui

_Main()

Func _Main()
    _OpenNotepad()
    _ShowGui()
    _Teardown()
EndFunc

Func _OpenNotepad()
    Run('notepad.exe')
EndFunc

Func _ShowGui()
    Local Const $iGuiTopMostFlag = 0x00000008

    GUICreate('Form1', 300, 200, Default, Default, Default, $iGuiTopMostFlag)
    Local Const $cIdInput  = GUICtrlCreateInput('Text to insert', 15, 15, 150)
    Local Const $cIdButton = GUICtrlCreateButton( 'Insert', 15, 45, 150)
    GUISetState(@SW_SHOW)

    Local Const $iGuiCloseFlag = -3

    While True
        Switch GUIGetMsg()
            Case $iGuiCloseFlag
                ExitLoop
            Case $cIdButton
                _Insert(GUICtrlRead($cIdInput))
        EndSwitch
    WEnd
EndFunc

Func _Insert($sText)
    ClipPut($sText)

    Opt('WinTitleMatchMode', 2)
    WinActivate('Editor')
    Opt('WinTitleMatchMode', 1) ; reset to default

    Send('^v')
EndFunc

Func _Teardown()
    ProcessClose('notepad.exe')
    GUIDelete($hGui)
EndFunc

I guess this should be helpful, right?

Best regards
Sven

Edited by SOLVE-SMART

Stay innovative!

Spoiler

๐ŸŒย Au3Forums

๐ŸŽฒ AutoIt (en) Cheat Sheet

๐Ÿ“Š AutoIt limits/defaults

๐Ÿ’Ž Code Katas: [...] (comming soon)

๐ŸŽญ Collection of GitHub users with AutoIt projects

๐Ÿžย False-Positives

๐Ÿ”ฎย Me on GitHub

๐Ÿ’ฌย Opinion about new forum sub category

๐Ÿ“‘ย UDF wiki list

โœ‚ย VSCode-AutoItSnippets

๐Ÿ“‘ย WebDriver FAQs

๐Ÿ‘จโ€๐Ÿซย WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...