Jump to content

Recommended Posts

Posted

Alright so testing under the circumstances of hidden desktop icons (eg. SysListView32 is hidden), the follow both work for me 100% of the time:

ControlSend($hSHELLDLL_DefView, "", "", "^z")
ControlSend($hSysListView32, "", "", "^z")

Here is my current testing script which also restores the original window that was visible at the time, whether it's Files Au3 or VS Code or SciTE.

#include <WinAPISysWin.au3>

#cs prev
Local $hProgman = WinGetHandle("[CLASS:Progman]")
Local $hCurrent = WinGetHandle("[ACTIVE]")
WinActivate($hProgman)
ControlSend($hProgman, "", "", "^z")
WinActivate($hCurrent)
#ce

#cs
Local $hProgman = WinGetHandle("[CLASS:Progman]")
Local $hCurrent = WinGetHandle("[ACTIVE]")
ConsoleWrite("progman: " & $hProgman & @CRLF)
WinActivate($hProgman)
WinWaitActive($hProgman)
Send("^a")
WinActivate($hCurrent)
#ce

;#cs
Local $hPrevious = _WinAPI_GetForegroundWindow()

Local $hProgman = WinGetHandle("[CLASS:Progman]")
ConsoleWrite("Progman hWnd: " & $hProgman & @CRLF)

Local $hSHELLDLL_DefView = _WinAPI_FindWindowEx($hProgman, 0, "SHELLDLL_DefView", "")
ConsoleWrite("SHELLDLL_DefView hWnd: " & $hSHELLDLL_DefView & @CRLF)

Local $aEnumWindows = _WinAPI_EnumWindows(False)
For $n = 1 To UBound($aEnumWindows) - 1
    If $aEnumWindows[$n][1] <> "SysListView32" Then ContinueLoop
    If _WinAPI_GetParent($aEnumWindows[$n][0]) = $hSHELLDLL_DefView Then
        $hSysListView32 = $aEnumWindows[$n][0]
        ExitLoop
    EndIf
Next

ConsoleWrite("Desktop SysListView32 hWnd: " & $hSysListView32 & @CRLF)

_WinAPI_SetForegroundWindow($hSysListView32)
_WinAPI_SetFocus($hSysListView32)

ControlSend($hSHELLDLL_DefView, "", "", "^z")
;ControlSend($hSysListView32, "", "", "^z")

;_WinAPI_SetForegroundWindow($hPrevious)
_WinAPI_SwitchToThisWindow($hPrevious, False)
_WinAPI_SetFocus($hPrevious)


Func _WinAPI_FindWindowEx($hParent, $hAfter, $sClass, $sTitle = "")
    Local $ret = DllCall('user32.dll', "hwnd", "FindWindowExW", "hwnd", $hParent, "hwnd", $hAfter, "wstr", $sClass, "wstr", $sTitle)
    If @error Or Not IsArray($ret) Then Return 0
    Return $ret[0]
EndFunc   ;==>_WinAPI_FindWindowEx
;#ce

@SOLVE-SMART If that fails, try to uncomment the one ControlSend and comment the other. See if one works better. Both work for me and work 100% of the time as undo for rename, copy, move, etc.

If it's not 100% then I will have to write my own Undo stack.

Posted

It seems to be the WinActivate failing on @Nine's script when the desktop icons are hidden.

I made a smaller script that contains some elements of both scripts and works on mine while the desktop icons are hidden.

#include <WinAPISysWin.au3>

Local $hProgman = WinGetHandle("[CLASS:Progman]")
Local $hCurrent = WinGetHandle("[ACTIVE]")
ConsoleWrite("Progman hWnd: " & $hProgman & @CRLF)

Local $hSHELLDLL_DefView = _WinAPI_FindWindowEx($hProgman, 0, "SHELLDLL_DefView", "")
ConsoleWrite("SHELLDLL_DefView hWnd: " & $hSHELLDLL_DefView & @CRLF)

Local $aEnumWindows = _WinAPI_EnumWindows(False)
For $n = 1 To UBound($aEnumWindows) - 1
    If $aEnumWindows[$n][1] <> "SysListView32" Then ContinueLoop
    If _WinAPI_GetParent($aEnumWindows[$n][0]) = $hSHELLDLL_DefView Then
        $hSysListView32 = $aEnumWindows[$n][0]
        ExitLoop
    EndIf
Next

ConsoleWrite("Desktop SysListView32 hWnd: " & $hSysListView32 & @CRLF)

;WinActivate($hProgman)
;WinWaitActive($hProgman)
_WinAPI_SetForegroundWindow($hSysListView32)
_WinAPI_SetFocus($hSysListView32)
;Send("^z")
ControlSend($hSysListView32, "", "", "^z")
WinActivate($hCurrent)

 

Posted

@WildByDesign thanks for the function _WinAPI_FindWindowEx, it sure will be useful in other scripts.
One question : does this one-liner retrieve the same result on your OS :

;~ Local $aEnumWindows = _WinAPI_EnumWindows(False)
;~ For $n = 1 To UBound($aEnumWindows) - 1
;~     If $aEnumWindows[$n][1] <> "SysListView32" Then ContinueLoop
;~     If _WinAPI_GetParent($aEnumWindows[$n][0]) = $hSHELLDLL_DefView Then
;~         $hSysListView32 = $aEnumWindows[$n][0]
;~         ExitLoop
;~     EndIf
;~ Next

Local $hSysListView32 = _WinAPI_FindWindowEx($hSHELLDLL_DefView, 0, "SysListView32", "FolderView")

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted
8 minutes ago, pixelsearch said:

thanks for the function _WinAPI_FindWindowEx, it sure will be useful in other scripts.

You're welcome. I don't recall who it came from originally. It came out of that thread about placing a window behind the desktop (Progmon).

9 minutes ago, pixelsearch said:

One question : does this one-liner retrieve the same result on your OS :

Indeed, yes. That one-liner is much better. Thank you.

Posted (edited)

I tried to summarize your (@pixelsearch and @WildByDesign) code snippets.
The following script works perfectly on my sensitive 😅 system.

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y

#include-once
#include <WinAPISysWin.au3>

_Main()

Func _Main()
    Local Const $hProgman = WinGetHandle("[CLASS:Progman]")
    Local Const $hCurrent = WinGetHandle("[ACTIVE]")

    Local Const $hSHELLDLL_DefView = _WinAPI_FindWindowEx($hProgman, 0, "SHELLDLL_DefView", "")
    Local Const $hSysListView32    = _WinAPI_FindWindowEx($hSHELLDLL_DefView, 0, "SysListView32", "FolderView")

    _WinAPI_SetForegroundWindow($hSysListView32)
    _WinAPI_SetFocus($hSysListView32)

    ControlSend($hSysListView32, "", "", "^z")
    WinActivate($hCurrent)
EndFunc

Func _WinAPI_FindWindowEx($hParent, $hAfter, $sClass, $sTitle = "")
    Local $ret = DllCall('user32.dll', "hwnd", "FindWindowExW", "hwnd", $hParent, "hwnd", $hAfter, "wstr", $sClass, "wstr", $sTitle)
    If @error Or Not IsArray($ret) Then Return 0
    Return $ret[0]
EndFunc
  • I tested it with Desktop icons hidden or shown.
  • I tested it with multiple files and folder.
  • I tested it also on the Windows Server machine.

All good 😀 . One thing to consider: error handling for _WinAPI_FindWindowEx is missing. In the files-au3 code it should be handled @WildByDesign 😇 .

Best regards
Sven

Edited by SOLVE-SMART

==> AutoIt related: 🔗 Organization AutoIt Community🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet🔗 autoit-webdriver-boilerplate

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

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
×
×
  • Create New...