Jump to content

Window Hide/Show


Simucal
 Share

Recommended Posts

Alright, I've created this script just now as a standalone example (even though this is part of a much larger project).

It shows a window with a listview that lists all windows with "Untitled - Notepad", their hwnd, and their visibility. You can then click Hide/Show and it will change the visibility of the selected window in the listview.

However, my working version still seems a little clumsy to me. Any of the heavy hitters want to help me improve my coding by showing me a way to simplify/clean up this code? (Specifically the _PopulateWindowList() function)

Here is what I have:

#Include <GUIConstants.au3>
#Include <GuiListView.au3>

$hWindowList = GUICreate("Hide/Show", 265, 140, 596, 604)
$oWindowList = GUICtrlCreateListView("Instance|hWnd|State", 8, 8, 249, 89)
GUICtrlSendMsg(-1, 0x101E, 0, 100)
GUICtrlSendMsg(-1, 0x101E, 1, 70)
GUICtrlSendMsg(-1, 0x101E, 2, 70)
$oHideShow = GUICtrlCreateButton("Hide/Show", 8, 104, 113, 25, 0)
$oHideShowCancel = GUICtrlCreateButton("Cancel", 128, 104, 113, 25, 0)
GUISetState(@SW_SHOW, $hWindowList)

Global $aOldWinList, $aNewWinList

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $oHideShow
            _HideShow()
    EndSwitch
    _PopulateWindowList()
    Sleep(20)
WEnd

Func _HideShow()
    Local $iSelectedIndex
    Local $aTemp
    
    ConsoleWrite("Selected: " & _GuiCtrlListView_GetNextItem($oWindowList, -1, 0, 8) & @CR)
    $iSelectedIndex = _GuiCtrlListView_GetNextItem($oWindowList, -1, 0, 8)
    If $iSelectedIndex <> -1 Then
        $aTemp = StringSplit(_GuiCtrlListView_GetItemTextString($oWindowList, $iSelectedIndex), "|")
        If IsArray($aTemp) And $aTemp[0]=3 Then
            If $aTemp[3] = "Visible"  Then
                ConsoleWrite("WinSetState("&$aTemp[2]&")"&@CR)
                WinSetState(HWnd($aTemp[2]), "", @SW_HIDE)
            Else
                WinSetState(HWnd($aTemp[2]), "", @SW_SHOW)
            EndIf
        EndIf
    Else
        MsgBox(0, "Error", "You must select a window to hide/show!")
    EndIf
EndFunc   ;==>_HideShow

Func _PopulateWindowList()
    Local $aWinList
    $aWinList = WinList("Untitled - Notepad")

    If IsArray($aWinList) And UBound($aWinList) > 1 Then ; Convert the current window list to my array format [hwnd][visible/hidden]
        Dim $aNewWinList[UBound($aWinList, 1)][2]
        For $i = 1 To UBound($aWinList) - 1
            $aNewWinList[$i][0] = $aWinList[$i][1]
            $aNewWinList[$i][1] = _ReturnVisibility($aWinList[$i][1])
        Next
        If Not _Array2DimCompare($aNewWinList, $aOldWinList) Then ;Compare new window list to the old window list previously applied to the listview
            If _GUICtrlListView_GetItemCount($oWindowList) > 0 Then _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($oWindowList)) ; Clear the listview if it isnt already
            For $i = 1 To UBound($aWinList, 1) - 1 ; Fill the listview with new updated window list/states
                GUICtrlCreateListViewItem("Notepad #" & $i & "|" & $aWinList[$i][1] & "|" & _ReturnVisibility($aWinList[$i][1]), $oWindowList)
            Next
            $aOldWinList = $aNewWinList ; Make the new list now the old one.
        EndIf
    Else
        If _GUICtrlListView_GetItemCount($oWindowList) > 0 Then _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($oWindowList)) ; Clear the listview if it is full and we found no windows
    EndIf
EndFunc   ;==>_PopulateWindowList

Func _ReturnVisibility($hWND)
    If WinExists($hWND) Then
        If BitAND(WinGetState($hWND), 2) Then
            Return "Visible"
        Else
            Return "Hidden"
        EndIf
    EndIf
EndFunc   ;==>_ReturnVisibility

Func _Array2DimCompare($Array1, $Array2, $iBase1 = 1, $iBase2 = 0, $iCase = 1)
    If UBound($Array1, 1) <> UBound($Array2, 1) Then Return SetError(1, 0, 0)
    If UBound($Array1, 2) <> UBound($Array2, 2) Then Return SetError(2, 0, 0)
    For $iCC = $iBase1 To UBound($Array1, 1) - 1
        For $xCC = $iBase2 To UBound($Array1, 2) - 1
            If $iCase Then
                If Not ($Array1[$iCC][$xCC] == $Array2[$iCC][$xCC]) Then _
                        Return SetError(3, 0, 0)
            Else
                If Not ($Array1[$iCC][$xCC] = $Array2[$iCC][$xCC]) Then _
                        Return SetError(3, 0, 0)
            EndIf
        Next
    Next
    Return 1
EndFunc   ;==>_Array2DimCompare

So again, I just want to see if anyone has any ideas how to streamline this even more.

Note: To test this, just open up a few instances of Notepad.

AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)
Link to comment
Share on other sites

Really, I just feel like a person reaches a certain level of proficiency in scripting/programming to where they can get most of the programs they write to do what they want.. but I'm always sure there are better ways to do things. I feel like I stagnate since I have no peer review of my code and no one to say, "Hey.. you could simplify this or do this more efficently, etc, etc, etc"

AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)
Link to comment
Share on other sites

I like your script logic quite well.

Just little optimization:

...
Global $aOldWinList, $aNewWinList
Global $start = TimerInit()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $oHideShow
            _HideShow()
    EndSwitch

;~     _PopulateWindowList()
;~     Sleep(20)
    If TimerDiff($start) > 1000 Then
        _PopulateWindowList()
        $start = TimerInit()
    EndIf
WEnd
...
Edited by Zedna
Link to comment
Share on other sites

I like your script logic quite well.

Just little optimization:

...
Global $aOldWinList, $aNewWinList
Global $start = TimerInit()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $oHideShow
            _HideShow()
    EndSwitch

;~     _PopulateWindowList()
;~     Sleep(20)
    If TimerDiff($start) > 1000 Then
        _PopulateWindowList()
        $start = TimerInit()
    EndIf
WEnd
...

Thanks for your input zedna. I was thinking of doing the same thing, adding the timer to prevent the constant hammering. Also, that arraycompare function is Smoke_n's... I had it labled as such in the comments but I think "Tidy" stripped it. So, major credit to you smoke.

Edited by Simucal
AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)
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...