Jump to content

How to find last active window ?


kcvinu
 Share

Go to solution Solved by jdelaney,

Recommended Posts

Hi there,

I am planning to customize ALT TAB key combination. This is my pseudo code

 Get window list with WinList()

 If Mywindow1 and Mywindow2 is Exits Then

        Toggle that two windows.

 Else

        Find last active window and activate it.

So i need to know how to find the last active window.?

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

You would do something like so:

Local $myLastWin = WinActive("[ACTIVE]")
; your code goes after
Local $winList = WinList()
Local $Mywindow1 = "handle for window 1 here", $MyWindow2 = "handle for window 2 here"
If WinExists($Mywindow1) And WinExists($Mywindow2) Then
    ; toggle the windows
Else
    WinActivate($myLastWin)
EndIf

Does that help? :)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

just send alt+tab as pressing alt+tab once takes you to the last active window.  But it would take more effort to find your script and run it than it would to just press alt+tab.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Hi MikahS, 

I didn't get the first line of your code. What is "[Active]" ?

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

Hi 

boththose, Thanks but Send is not working
Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

Hi MikahS, 

I didn't get the first line of your code. What is "[Active]" ?

[ACTIVE] is the window that is active right now. Storing the returned result (which is the handle) in a variable will give you the ability to use WinActivate to re-activate the window that you want.

EDIT: see the title special definiton

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Hi 

MikahS

Thanks. Let me try

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

Hi MikahS, 

It is partially working now. See my code. (Attached image). Code in line number 21 is not working. Rest is ok. 

post-89644-0-89174300-1421864011_thumb.p

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

The reason is probably due to if $Nuendo_hnd and $Word_hnd are both existing (or they are open windows), line 21 will never run. The only way you'll be able to get it to do line 21 is if Nuendo or MS word did not exist.

If you only had one window that exists like MS word then it would run, but not with both open.

EDIT: does that help clear it up?

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

send works fine

If not winexists("Microsoft Word") AND not winexists("nuendo") Then send("!{TAB}")

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

It is not working even when both "Nuendo" and "Word" is not running..

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

on line 10 use

WinGetTitle("[ACTIVE]")

instead of WinActive. I think it was a problem trying to use the handle instead of a title.

EDIT: Try that and post results. :)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

It is not working even when both "Nuendo" and "Word" is not running..

 

 

Makes me think we are troubleshooting your If statement, not the line suggested.

Did you try running just the line I provided by itself?

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Can you try this ?

$hPreviousWindow = _WinGetNextWindow("[ACTIVE]" , "", 2)
ConsoleWrite("Previous Window : " & @CRLF & "HWND = " & $hPreviousWindow &  @CRLF & "Title = " &  WinGetTitle($hPreviousWindow) & @CRLF)

 
 
; #FUNCTION# ====================================================================================================================
; Name ..........: _WinGetNextWindow
; Description ...: Retrieves a handle to the next or previous visible window in the Z-Order.
; Syntax ........: _WinGetNextWindow($vWindow[, $sText = ""[, $iCmd = 0]])
; Parameters ....: $vWindow             - The title/hWnd/class of the window. See Title special definition.
;                  $sText               - [optional] The text of the window to check. Default is an empty string. See Text special definition.
;                  $iOrder              - [optional] Indicates whether the function returns a handle to the next window or the previous window.
;                                         This parameter can be either of the following values :
;                                          $GW_HWNDNEXT (2) : Returns a handle to the window below the given window.
;                                          $GW_HWNDPREV (3) : Returns a handle to the window above the given window.
; Return values .: Success - Returns a window handle
;                  Failure - Returns 0  and sets @error flag to non-zero on error
; Author ........: jguinch
; Remarks .......: If the specified window is a topmost window, the function searches for a topmost window.
;                  If the specified window is a top-level window, the function searches for a top-level window.
;                  If the specified window is a child window, the function searches for a child window.
; Link ..........:
; ===============================================================================================================================
Func _WinGetNextWindow($vWindow, $sText = "", $iCmd = 2)
    Local $hWnd = $vWindow, $aGetWindow
    If NOT IsHWnd($vWindow) Then $hWnd = WinGetHandle($vWindow, $sText)
    If $hWnd = 0 Then Return SetError(2, 0, 0)
 
    If NOT IsDeclared("GW_HWNDNEXT") Then Local Const $GW_HWNDNEXT = 2
    If NOT IsDeclared("GW_HWNDPREV") Then Local Const $GW_HWNDPREV = 3
 
    If $iCmd <> $GW_HWNDNEXT AND $iCmd <> $GW_HWNDPREV Then Return SetError(3, 0, 0)
 
    While 1
        $aGetWindow = DllCall("User32.dll", "hwnd", "GetWindow", "hwnd", $hWnd, "uint", $iCmd)
        If @error Then Return SetError(4, 0, 0)
        If $aGetWindow[0] = 0 Then ExitLoop
 
        $hWnd = $aGetWindow[0]
 
        $aGetWindowLong = DllCall("User32.dll", "long", "GetWindowLong", "hwnd", $hWnd, "int", -16)
        If @error Then ExitLoop
 
        If NOT BitAND(WinGetState($hWnd), 2) OR $aGetWindowLong[0] < 0 Then ContinueLoop
 
        Return $hWnd
    WEnd
 
    Return SetError(1, 0, 0)
EndFunc
Edited by jguinch
Link to comment
Share on other sites

  • Moderators

Are you running your script from SciTe?  I imagine when you hit F5 from SciTe to test, that's the last active window?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Solution

Simplified with the Winapi UDF:

#include <WinAPI.au3>
 $hPriorActiveWindow = _WinAPI_GetWindow(WinGetHandle("[ACTIVE]"),$GW_HWNDNEXT)
 WinActivate($hPriorActiveWindow)
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Makes me think we are troubleshooting your If statement, not the line suggested.

Did you try running just the line I provided by itself?

No, The code provided by you is not working. 

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

 

Simplified with the Winapi UDF:

#include <WinAPI.au3>
 $hPriorActiveWindow = _WinAPI_GetWindow(WinGetHandle("[ACTIVE]"),$GW_HWNDNEXT)
 WinActivate($hPriorActiveWindow)

Thank you, let me try

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

Did you try post #12?

Glad you found a solution! ;)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

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

×
×
  • Create New...