Jump to content
Sign in to follow this  
Hypertrophy

Get Active Window

Recommended Posts

Hypertrophy

Would the following code get the currently active windows handle?

$state = WinGetState("", "")

If BitAnd($state, 8) Then
    $handle=WinGetHandle($state)
EndIf


            
        

        
            
                
                    

        
        
            
                
                    
                
            
            
            
        
    


                
            
            
                
                    
                    
                    
                
                
                    
                
            
        

        
    

    
        
            Share this post
            
            Link to post
            
            
            

            
                Share on other sites
                

    
        
            

    

        
            

    

        
            

    

        
            

    

        
            

    

        
    

            
        
    

    

                    
                        
                            


     (adsbygoogle = window.adsbygoogle || []).push({});

                        
                    
                
                    
                    
                    






    

    
        
            
Richard Robertson
              

    
        
    
             186
    
        
    

        
        
            

    
        
    

        
    
    
        
Richard Robertson   

    
        
    
             186
    
        
    

        
        
            
                Universalist
            
            

            
                

    
        
    

            
            Active Members
            
            
                

    
        
    
             186
    
        
    

                10,165 posts
                
                

            
        
    
    
        


    

    
        
            
                
                
                
                    
                
                
            
        

        
            Posted 
            
                
                
            
        
    

    


    
        
        
            WinGetHandle("") will return the active window's handle.


            
        

        
            
                
                    

        
        
            
                
                    
                        


    
    
        
        
            
                
            
                    
                        
                    
                    
                        1
                    
            
                
            
        
    
    


    
    

                    
                
            
            
            
        
    


                
            
            
                
                    
                    
                    
                
                
                    
                
            
        

        
    

    
        
            Share this post
            
            Link to post
            
            
            

            
                Share on other sites
                

    
        
            

    

        
            

    

        
            

    

        
            

    

        
            

    

        
    

            
        
    

    

                    
                        
                    
                
                    
                    
                    






    

    
        
            
Hypertrophy
              

    
        
    
             0
    
        
    

        
        
            

    
        
    

        
    
    
        
Hypertrophy   

    
        
    
             0
    
        
    

        
        
            
                Universalist
            
            

            
                

    
        
    

            
            Active Members
            
            
                

    
        
    
             0
    
        
    

                330 posts
                
                

            
        
    
    
        


    

    
        
            
                
                
                
                    
                
                
            
        

        
            Posted 
            
                
                    (edited)
                
                
            
        
    

    


    
        
        
            I've been trying to tweak this code so it will put a red border around whatever window is active. Credits to MrCreator, martin; and Melba for his further tweakage.#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Opt("GuiOnEventMode", 1)
HotKeySet("{ESC}", "QuitApp")

If Not IsDeclared("WM_WINDOWPOSCHANGED") Then Assign("WM_WINDOWPOSCHANGED",0x0047)

Global $ahGUI[4]
Global $Frame_Color = 0xFF0000
Global $Frame_Width = 2

$Main_GUI = GuiCreate("Highlight Window Demo")
GUISetOnEvent($GUI_EVENT_CLOSE, "QuitApp")
GUIRegisterMsg($WM_WINDOWPOSCHANGED, "WM_WINDOWPOSCHANGED")

GUISetState()

Global $fSquare = False

While 1
    Sleep(100)
    $handle = WinGetHandle("")
    If BitAND(WinGetState($handle), 8) Then
        If $fSquare = False Then
    ; Get window data
            $aWin = WinGetPos($handle)  ; X Y
            $iBorder  = _WinAPI_GetSystemMetrics(8); Border width
            $iBar    = _WinAPI_GetSystemMetrics(4); Title bar height
    ; Get Client area size
            $aCA = WinGetClientSize($handle); Width, height
            GUICreateSquare($aWin[0] + $iBorder, $aWin[1] + $iBar + $iBorder, $aCA[0], $aCA[1])
            WinActivate($handle)
        EndIf
    Else
        If $fSquare = True Then GUISquareDelete()
    EndIf
WEnd

Func GUICreateSquare($X, $Y, $W, $H)
    $X -= $Frame_Width
    $Y -= $Frame_Width
    $W += $Frame_Width
    $H += $Frame_Width
    $ahGUI[0] = GUICreate("", $W, $Frame_Width, $X, $Y, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    $ahGUI[1] = GUICreate("", $Frame_Width, $H, $X, $Y, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    $ahGUI[2] = GUICreate("", $Frame_Width, $H, $X+$W, $Y, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    $ahGUI[3] = GUICreate("", $W+$Frame_Width, $Frame_Width, $X, $Y+$H, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    For $i = 0 To 3
        GUISetState(@SW_SHOW, $ahGUI[$i])
    Next
    $fSquare = True
EndFunc

Func GUISquareDelete()
    For $i = 0 To 3
        If IsHWnd($ahGUI[$i]) And $ahGUI[$i] <> $Main_GUI Then GUIDelete($ahGUI[$i])
    Next
    $ahGUI = ""
    Dim $ahGUI[4]
    $fSquare = False
EndFunc

Func WM_WINDOWPOSCHANGED($hWnd, $MsgID, $WParam, $LParam)
    If $hWnd = $Main_GUI Then
        GUISquareDelete()
    Else
        Return $GUI_RUNDEFMSG
    EndIf
EndFunc

Func QuitApp()
    Exit
EndFunc
Edited by coder09

Share this post


Link to post
Share on other sites
Richard Robertson

$handle = WinGetHandle("")

If BitAND(WinGetState($handle), 8) Then

This is pointless. WinGetHandle("") always returns the active window's handle. There's no need to waste time getting the state of the window.

Also, the way you have it set up, it's going to create and destroy those windows 10 times a second. That's a huge waste of resources. What you need to do is resize/relocate the windows already used to match the new window.

Share this post


Link to post
Share on other sites
Hypertrophy

Thanks brah for not helping me on my actual problem and telling me to change something that I didn't write.

Edited by coder09

Share this post


Link to post
Share on other sites
Richard Robertson

I did help. I told you what needed to be changed. Help does not mean "rewrite it for me".

Share this post


Link to post
Share on other sites
Hypertrophy

I don't want you to re-write it for me. Whether you think its wasting resources or not it works fine therefore should be left alone.

Share this post


Link to post
Share on other sites
Richard Robertson

If it works, what are you asking for?

Share this post


Link to post
Share on other sites
Hypertrophy

No it doesn't work with what I'm trying to do. It works in its original form.

Share this post


Link to post
Share on other sites
MDCT

No it doesn't work with what I'm trying to do. It works in its original form.

I moded your code, try this:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Opt("GuiOnEventMode", 1)
HotKeySet("{ESC}", "QuitApp")

Global $ahGUI[4]
Global $Frame_Color = 0xFF0000
Global $Frame_Width = 2

$Main_GUI = GuiCreate("Highlight Window Demo")
GUISetOnEvent($GUI_EVENT_CLOSE, "QuitApp")

GUISetState()

Global $fSquare = False
Global $handle = 0
While 1
    Sleep(100)
    if WinGetHandle("") <> $handle Then
        if $fSquare = True then GUISquareDelete()
        $handle = WinGetHandle("")
        $aWin = WinGetPos($handle)   ; X Y
        $iBorder  = _WinAPI_GetSystemMetrics(8); Border width
        $iBar    = _WinAPI_GetSystemMetrics(4); Title bar height
   ; Get Client area size
        $aCA = WinGetClientSize($handle); Width, height
        GUICreateSquare($aWin[0] + $iBorder, $aWin[1] + $iBar + $iBorder, $aCA[0], $aCA[1])
        WinActivate($handle)
    Else
        $aWin2 = WinGetPos($handle)
        if $aWin[0] <> $aWin2[0] and $aWin[1] <> $aWin2[1] Then
            GUISquareDelete()
            $fSquare = True
            $aWin = WinGetPos($handle)   ; X Y
            $iBorder  = _WinAPI_GetSystemMetrics(8); Border width
            $iBar    = _WinAPI_GetSystemMetrics(4); Title bar height
    ; Get Client area size
            $aCA = WinGetClientSize($handle); Width, height
            GUICreateSquare($aWin[0] + $iBorder, $aWin[1] + $iBar + $iBorder, $aCA[0], $aCA[1])
            WinActivate($handle)
        EndIf
        Sleep(150)
    EndIf
    
WEnd    

exit
    If BitAND(WinGetState($handle), 8) Then
        tooltip(_WinAPI_GetClassName($handle),0,0)
        If $fSquare = False Then
   ; Get window data
            $aWin = WinGetPos($handle)   ; X Y
            $iBorder  = _WinAPI_GetSystemMetrics(8); Border width
            $iBar    = _WinAPI_GetSystemMetrics(4); Title bar height
   ; Get Client area size
            $aCA = WinGetClientSize($handle); Width, height
            GUICreateSquare($aWin[0] + $iBorder, $aWin[1] + $iBar + $iBorder, $aCA[0], $aCA[1])
            WinActivate($handle)
        EndIf
    Else
        If $fSquare = True Then GUISquareDelete()
    EndIf

Func GUICreateSquare($X, $Y, $W, $H)
    $X -= $Frame_Width
    $Y -= $Frame_Width
    $W += $Frame_Width
    $H += $Frame_Width
    $ahGUI[0] = GUICreate("", $W, $Frame_Width, $X, $Y, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    $ahGUI[1] = GUICreate("", $Frame_Width, $H, $X, $Y, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    $ahGUI[2] = GUICreate("", $Frame_Width, $H, $X+$W, $Y, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    $ahGUI[3] = GUICreate("", $W+$Frame_Width, $Frame_Width, $X, $Y+$H, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    For $i = 0 To 3
        GUISetState(@SW_SHOW, $ahGUI[$i])
    Next
    $fSquare = True
EndFunc

Func GUISquareDelete()
    For $i = 0 To 3
        If IsHWnd($ahGUI[$i]) And $ahGUI[$i] <> $Main_GUI Then GUIDelete($ahGUI[$i])
    Next
    $ahGUI = ""
    Dim $ahGUI[4]
    $fSquare = False
EndFunc

Func QuitApp()
    Exit
EndFunc

It works by checking if the active window is changed. I believe your code won't work because it cannot detect if there's change in active window. And it is true that "If BitAND(WinGetState($handle), 8) Then" is not necessary as the active window will give the same result. And one more thing, WM_WINDOWPOSCHANGED here cannot be used on all windows.

Hope this helps.

Share this post


Link to post
Share on other sites
Hypertrophy

MDCT: your code works well except when i try to move the currently active window that has the red border around it, I can't move the window constantly. it like freezes at certain points and i have to release it and move it again. (this doesn't happen with the main gui)

Share this post


Link to post
Share on other sites
MDCT

MDCT: your code works well except when i try to move the currently active window that has the red border around it, I can't move the window constantly. it like freezes at certain points and i have to release it and move it again. (this doesn't happen with the main gui)

I set the condition of the mousepos with AND, instead OR. Otherwise, you would have problem with moving window as it always recreates the border and reactivate the window. Here, I've changed some in the code, it should work with no problem.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Opt("GuiOnEventMode", 1)
HotKeySet("{ESC}", "QuitApp")

Global $ahGUI[4]
Global $Frame_Color = 0xFF0000
Global $Frame_Width = 2

$Main_GUI = GuiCreate("Highlight Window Demo")
GUISetOnEvent($GUI_EVENT_CLOSE, "QuitApp")

GUISetState()

Global $fSquare = False
Global $handle = 0
While 1
    Sleep(50)
    if WinGetHandle("") <> $handle Then
        if $fSquare = True then GUISquareDelete()
        Sleep(24)
        $handle = WinGetHandle("")
        $aWin = WinGetPos($handle)  ; X Y
        $iBorder  = _WinAPI_GetSystemMetrics(8); Border width
        $iBar    = _WinAPI_GetSystemMetrics(4); Title bar height
  ; Get Client area size
        $aCA = WinGetClientSize($handle); Width, height
        GUICreateSquare($aWin[0] + $iBorder, $aWin[1] + $iBar + $iBorder, $aCA[0], $aCA[1])
        Sleep(24)
    Else
        Sleep(50)
        $aWin2 = WinGetPos($handle)
        if $aWin[0] <> $aWin2[0] or $aWin[1] <> $aWin2[1] Then
            GUISquareDelete()
            Sleep(24)
            $fSquare = True
            $aWin = WinGetPos($handle)  ; X Y
            $iBorder  = _WinAPI_GetSystemMetrics(8); Border width
            $iBar    = _WinAPI_GetSystemMetrics(4); Title bar height
   ; Get Client area size
            $aCA = WinGetClientSize($handle); Width, height
            GUICreateSquare($aWin[0] + $iBorder, $aWin[1] + $iBar + $iBorder, $aCA[0], $aCA[1])
            Sleep(24)
        EndIf
        Sleep(50)
    EndIf
    Sleep(50)
WEnd    

exit
    If BitAND(WinGetState($handle), 8) Then
        tooltip(_WinAPI_GetClassName($handle),0,0)
        If $fSquare = False Then
  ; Get window data
            $aWin = WinGetPos($handle)  ; X Y
            $iBorder  = _WinAPI_GetSystemMetrics(8); Border width
            $iBar    = _WinAPI_GetSystemMetrics(4); Title bar height
  ; Get Client area size
            $aCA = WinGetClientSize($handle); Width, height
            GUICreateSquare($aWin[0] + $iBorder, $aWin[1] + $iBar + $iBorder, $aCA[0], $aCA[1])
            WinActivate($handle)
        EndIf
    Else
        If $fSquare = True Then GUISquareDelete()
    EndIf

Func GUICreateSquare($X, $Y, $W, $H)
    $X -= $Frame_Width
    $Y -= $Frame_Width
    $W += $Frame_Width
    $H += $Frame_Width
    $ahGUI[0] = GUICreate("", $W, $Frame_Width, $X, $Y, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    $ahGUI[1] = GUICreate("", $Frame_Width, $H, $X, $Y, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    $ahGUI[2] = GUICreate("", $Frame_Width, $H, $X+$W, $Y, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    $ahGUI[3] = GUICreate("", $W+$Frame_Width, $Frame_Width, $X, $Y+$H, $WS_POPUP, $WS_EX_TOPMOST+$WS_EX_TOOLWINDOW)
    GUISetBkColor($Frame_Color)

    For $i = 0 To 3
        GUISetState(@SW_SHOWNOACTIVATE, $ahGUI[$i])
    Next
    $fSquare = True
EndFunc

Func GUISquareDelete()
    For $i = 0 To 3
        If IsHWnd($ahGUI[$i]) And $ahGUI[$i] <> $Main_GUI Then GUIDelete($ahGUI[$i])
    Next
    $ahGUI = ""
    Dim $ahGUI[4]
    $fSquare = False
EndFunc

Func QuitApp()
    Exit
EndFunc

Share this post


Link to post
Share on other sites
Hypertrophy

Thank you this is perfect.

Share this post


Link to post
Share on other sites
MDCT

Thank you this is perfect.

I think you need to add some sleep commands here and there. It might lower the responsiveness, but it helps your CPU.

Share this post


Link to post
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
Sign in to follow this  

×