Jump to content

Don't take focus when executed compiled CUI script?


Recommended Posts

I'm trying make my compiled script as CUI to be shown on screen when ran (task scheduler or from explorer), but not taking focus.

My initial idea was to relaunch itself with Run() and use @SW_SHOWNOACTIVATE flag, but it didn't work, it still showed focused console window.

Any ideas how it can be achieved?


Thank you.

Link to comment
Share on other sites

before starting the CUI , check which window has the focus $hWndActive = WinGetHandle("[ACTIVE]"),
and after opening your CUI , return the focus to where it was WinActivate($hWndActive )

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

@ioa747 Thanks, it works, but very unreliable. Sometimes it switches to totally different application..

 

14 minutes ago, Nine said:

Show a runable script where we can actually see the issue.


Sure

#AutoIt3Wrapper_Change2CUI=y

sleep(60000)

 

Compile it and run from explorer. The opened console window will be in focus. The expected result: explorer window remains in focus, while console window remains visible on screen

Note, as I mentioned before, this may be run by task scheduler, so any application can be in focus when it's launched.

Edited by VAN0
Link to comment
Share on other sites

That would do it, but I'm trying find a way to do it within same script if possible. For example, I probably could do something if I could figure out how to get hwnd of the console window.

Also, launcher can just run the program with @SW_SHOWNOACTIVATE as one-liner:

Run("myCUIapp.exe", @WorkingDir, @SW_SHOWNOACTIVATE, 0x10000)

No need any WinActivate

Edited by VAN0
Link to comment
Share on other sites

Link to comment
Share on other sites

2 minutes ago, Nine said:

Not sure that this is what you want but it removes focus from the cmd window (when started from explorer) :

 

Hmm it doesn't for me on Win 10 x64

Console remains focused. This is because it's not the @AutoItExe window, but "Console Window Host" window, which is separate process:

image.thumb.png.b83662168300b843e1d44ad6a90b2fdc.png

 

Spoiler
#AutoIt3Wrapper_Change2CUI=y
#include <SendMessage.au3>
#include <WindowsConstants.au3>

Local $hWnd = WinGetHandle(@AutoItExe)
_SendMessage($hWnd, $WM_KILLFOCUS)

ConsoleWrite("hWnd: " & $hWnd & " pid: " & WinGetProcess(@AutoItExe) & @CRLF)

Sleep(60000)

 

 

Link to comment
Share on other sites

I have Win10 x64 and it works for me (based on the Windows definition of Focus).  So you must be confusing the signification of "focused".  Also, by your own screen shot, the console has the title "E:\dev\1.exe" which is the exact value of @AutoItExe.  As I do not understand what is your goal, I will let others continue...

Link to comment
Share on other sites

@Nine it didn't work for me either.
and I don't understand why
and in both cases  WinGetHandle(@AutoItExe) or WinGetHandle(@ScriptFullPath)
the title comes out normally

Windows 10 Pro x64, Version    22H2
script Compile X86

#AutoIt3Wrapper_Change2CUI=y
#include <SendMessage.au3>
#include <WindowsConstants.au3>

Local $hWnd = WinGetHandle(@AutoItExe)
;~ Local $hWnd = WinGetHandle(@ScriptFullPath)
ConsoleWrite(WinGetTitle($hWnd) & @CRLF)
_SendMessage($hWnd, $WM_KILLFOCUS)

Sleep(6000)

 

I know that I know nothing

Link to comment
Share on other sites

I'm not sure what other meaning of "focused" there could be, other than the window that is being interacted with, aka currently activated.

Look at the screenshot of the task manager. See the 2 applications under 1.exe (2) - these are two separate processes launched when 1.exe is executed.

On my system when a window is focused its title bar is blue, otherwise it's gray. On the screenshot it's blue = focused.

So, with your code on my system the title bar of the console window remains blue, and not the explorer window I launched it from.

 

@ioa747 Your code almost works for me, the console window loses focus, but no other window gets focused either (at least visibly), I have to add WinSetState($hNextWindow, "", @SW_SHOW) - then it worked but with 0.5sec delay:

#AutoIt3Wrapper_Change2CUI=y
#include <WinAPI.au3>

Local $hWnd = WinWaitActive(@ScriptFullPath)
ConsoleWrite("$hWnd=" & $hWnd & @CRLF)

; Get the handle of the next window in Z order
Local $hNextWindow = _WinAPI_GetWindow($hWnd, $GW_HWNDNEXT)

WinActivate($hNextWindow)
WinSetState($hNextWindow, "", @SW_SHOW)

Sleep(6000)

 

Link to comment
Share on other sites

safer since _WinAPI_GetWindow can return a hidden window (which you don't want to make visible, since it's hidden for some reason)

#AutoIt3Wrapper_Change2CUI=y
#include <WinAPI.au3>

Local $hWnd = WinWaitActive(@ScriptFullPath)
ConsoleWrite("$hWnd=" & $hWnd & @CRLF)

; Initialize a variable to hold the next window handle
Local $hNextWindow = $hWnd

; Loop to find the next visible window in Z order
While True
    $hNextWindow = _WinAPI_GetWindow($hNextWindow, $GW_HWNDNEXT)

    If $hNextWindow = Null Then
         ConsoleWrite("No next visible window found in Z order.")
        ExitLoop
    EndIf

    ; Check if window is visible
    If _WinAPI_IsWindowVisible($hNextWindow) Then
        WinActivate($hNextWindow)
        ExitLoop
    EndIf
WEnd

ConsoleWrite("out of loop" & @CRLF)

Sleep(60000)

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

@ioa747 does it work for you? (it doesn't for me)

For example, when you double click on compiled .exe and without clicking anywhere press up/down cursor keys - does it change to next/previous file in the explorer window? If not = it doesn't work.

Link to comment
Share on other sites

maybe it's the hidden thing that you make visible
above you said
I have to add WinSetState($hNextWindow, "", @SW_SHOW) 

since we switched off the hidden

Edited by ioa747

I know that I know nothing

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...