VAN0 Posted October 19 Share Posted October 19 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 More sharing options...
ioa747 Posted October 19 Share Posted October 19 (edited) 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 October 19 by ioa747 I know that I know nothing Link to comment Share on other sites More sharing options...
VAN0 Posted October 19 Author Share Posted October 19 CUI is being launched either by the task scheduler (aka system) or by explorer (aka user) Link to comment Share on other sites More sharing options...
ioa747 Posted October 19 Share Posted October 19 put Send("!{TAB}") on start I know that I know nothing Link to comment Share on other sites More sharing options...
Nine Posted October 19 Share Posted October 19 Show a runable script where we can actually see the issue. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
VAN0 Posted October 19 Author Share Posted October 19 (edited) @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 October 19 by VAN0 Link to comment Share on other sites More sharing options...
VAN0 Posted October 19 Author Share Posted October 19 Here is a somewhat working attempt relaunch itself as non-active window: #AutoIt3Wrapper_Change2CUI=y If $CmdLine[0] = 0 Then Run(@ScriptFullPath & ' 1', @WorkingDir, @SW_SHOWNOACTIVATE, 0x10000) Exit EndIf ConsoleWrite("Should start NOT focused") Sleep(60000) Unfortunately, it still steals focus for a moment Link to comment Share on other sites More sharing options...
ioa747 Posted October 19 Share Posted October 19 (edited) make a separate launcher script so it's not a CUI ;~ launcher.exe Local $hWndActive $hWndActive = WinGetHandle("[ACTIVE]") ShellExecute(@ScriptDir & "\MyCUI.exe") WinActivate($hWndActive) Edited October 19 by ioa747 I know that I know nothing Link to comment Share on other sites More sharing options...
VAN0 Posted October 19 Author Share Posted October 19 (edited) 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 October 19 by VAN0 Link to comment Share on other sites More sharing options...
Nine Posted October 19 Share Posted October 19 Not sure that this is what you want but it removes focus from the cmd window (when started from explorer) : #AutoIt3Wrapper_Change2CUI=y #include <SendMessage.au3> #include <WindowsConstants.au3> Local $hWnd = WinGetHandle(@AutoItExe) _SendMessage($hWnd, $WM_KILLFOCUS) Sleep(6000) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
VAN0 Posted October 19 Author Share Posted October 19 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: 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 More sharing options...
Nine Posted October 20 Share Posted October 20 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... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
ioa747 Posted October 20 Share Posted October 20 #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) Sleep(6000) I know that I know nothing Link to comment Share on other sites More sharing options...
ioa747 Posted October 20 Share Posted October 20 @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) pixelsearch 1 I know that I know nothing Link to comment Share on other sites More sharing options...
VAN0 Posted October 20 Author Share Posted October 20 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 More sharing options...
ioa747 Posted October 20 Share Posted October 20 (edited) 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 October 20 by ioa747 I know that I know nothing Link to comment Share on other sites More sharing options...
VAN0 Posted October 20 Author Share Posted October 20 @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 More sharing options...
ioa747 Posted October 20 Share Posted October 20 (edited) yes it change to next/previous file in the explorer Edit: I update the previous one in case I was running something else Edited October 20 by ioa747 I know that I know nothing Link to comment Share on other sites More sharing options...
VAN0 Posted October 20 Author Share Posted October 20 Strange. Your code works on my other computer, but not on this one: it focuses on some hidden "MSCTFIME UI" window. Link to comment Share on other sites More sharing options...
ioa747 Posted October 20 Share Posted October 20 (edited) 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 October 20 by ioa747 I know that I know nothing Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now