Jump to content

How to find last active window ?


kcvinu
 Share

Go to solution Solved by jdelaney,

Recommended Posts

 

Simplified with the Winapi UDF:

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

Hi jdelaney, Thanks alot. That worked.. Thank you all for helping me.

Edited by kcvinu
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 

jguinch,

However, i am going to try your function. 

Edited by kcvinu
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

My function is a bit different, because it returns only a visible Window.
You can try this code, it should return different values for the two functions :
 

#include <WinAPI.au3>
$hPriorActiveWindow = _WinAPI_GetWindow(WinGetHandle("[ACTIVE]"),$GW_HWNDNEXT)
$aPos = WinGetPos($hPriorActiveWindow)
ConsoleWrite("# Previous Window with _WinAPI_GetWindow: " & @CRLF & "HWND = " & $hPriorActiveWindow &  @CRLF & "Title = " &  WinGetTitle($hPriorActiveWindow) & @CRLF)
ConsoleWrite("Size : x=" & $aPos[0] & "; y=" & $aPos[1] & "; w=" & $aPos[2] & "; h=" & $aPos[3] & @CRLF & @CRLF)


$hPreviousWindow = _WinGetNextWindow("[ACTIVE]" , "", 2)
$aPos = WinGetPos($hPreviousWindow)
ConsoleWrite("# Previous Window with _WinGetNextWindow: " & @CRLF & "HWND = " & $hPreviousWindow &  @CRLF & "Title = " &  WinGetTitle($hPreviousWindow) & @CRLF)
ConsoleWrite("Size : x=" & $aPos[0] & "; y=" & $aPos[1] & "; w=" & $aPos[2] & "; h=" & $aPos[3] & @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
Link to comment
Share on other sites

 

My function is a bit different, because it returns only a visible Window.

You can try this code, it should return different values for the two functions :

 

#include <WinAPI.au3>
$hPriorActiveWindow = _WinAPI_GetWindow(WinGetHandle("[ACTIVE]"),$GW_HWNDNEXT)
$aPos = WinGetPos($hPriorActiveWindow)
ConsoleWrite("# Previous Window with _WinAPI_GetWindow: " & @CRLF & "HWND = " & $hPriorActiveWindow &  @CRLF & "Title = " &  WinGetTitle($hPriorActiveWindow) & @CRLF)
ConsoleWrite("Size : x=" & $aPos[0] & "; y=" & $aPos[1] & "; w=" & $aPos[2] & "; h=" & $aPos[3] & @CRLF & @CRLF)


$hPreviousWindow = _WinGetNextWindow("[ACTIVE]" , "", 2)
$aPos = WinGetPos($hPreviousWindow)
ConsoleWrite("# Previous Window with _WinGetNextWindow: " & @CRLF & "HWND = " & $hPreviousWindow &  @CRLF & "Title = " &  WinGetTitle($hPreviousWindow) & @CRLF)
ConsoleWrite("Size : x=" & $aPos[0] & "; y=" & $aPos[1] & "; w=" & $aPos[2] & "; h=" & $aPos[3] & @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

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

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