Jump to content

[solved] Set icon on hidden window


argumentum
 Share

Recommended Posts

image.png.5018762a8ea63bf152425e85f6e590c6.png

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=MyIcon_or_yours.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

AutoItWinSetTitle(StringTrimRight(@ScriptName, 4) & "|PID:" & @AutoItPID)
WinSetState(StringTrimRight(@ScriptName, 4) & "|PID:" & @AutoItPID, "", @SW_SHOW)
TraySetIcon(StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", 0, -1)) & "Icons\au3.ico")
ControlSetText(StringTrimRight(@ScriptName, 4) & "|PID:" & @AutoItPID, "", "Edit1", '@Compiled = ' & (@Compiled = 1))
While 1
    Sleep(50)
WEnd

- So I would like to set the icon on a script that is not compiled, to show the icon as when it is compiled.
- Why ?.
- Well, while I'm coding, it's easier to identify the window. 
- Why don't you just create a GUI instead of un-hiding the one that is not meant to be shown anyway ?.
- I know.  I just want to go this way.  Can you show me how ?

PS: the above chatter, is to avoid the common responses :)

 

Edited by argumentum
SOLVED

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

The following works:

https://www.autoitscript.com/forum/topic/168698-changing-a-windows-icon/?do=findComment&comment=1233266

#include <WinAPI.au3>

AutoItWinSetTitle(StringTrimRight(@ScriptName, 4) & "|PID:" & @AutoItPID)
WinSetState(StringTrimRight(@ScriptName, 4) & "|PID:" & @AutoItPID, "", @SW_SHOW)
TraySetIcon(StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", 0, -1)) & "Icons\au3.ico")
ControlSetText(StringTrimRight(@ScriptName, 4) & "|PID:" & @AutoItPID, "", "Edit1", '@Compiled = ' & (@Compiled = 1))
Local $hGui = WinGetHandle(AutoItWinGetTitle())
_WinSetIcon($hGui, "C:\Windows\Notepad.exe")
While 1
    Sleep(50)
WEnd

Func _WinSetIcon($hWnd, $sFile, $iIndex = 0)
    Local $tIcons = DllStructCreate("ptr Data")
    DllCall("shell32.dll", "uint", "ExtractIconExW", "wstr", $sFile, "int", $iIndex, "struct*", 0, "struct*", $tIcons, "uint", 1)
    If @error Then Return SetError(1, 0, 0)
    Local $hIcon = DllStructGetData($tIcons, "Data")
   _SendMessage($hWnd, 0x0080, 1, $hIcon ) ;$WM_SETICON = 0x0080
   _WinAPI_DestroyIcon($hIcon)
   Return 1
EndFunc

 

Link to comment
Share on other sites

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=MyIcon_or_yours.ico
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Opt("MustDeclareVars", 1) ; removes the chance for misspelled variables causing bugs.

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
;~ #include <Array.au3>; _ArrayDisplay()


If WinExists(StringTrimRight(@ScriptName, 4) & " or meaningful - slowdown") Then Exit 4
AutoItWinSetTitle(StringTrimRight(@ScriptName, 4) & " or meaningful - slowdown")

ConsoleWriteLog() ; init. something before any tweaking

Global $g_sBaseTitle = StringTrimRight(@ScriptName, 4) & " or meaningful - PID "
Global $g_sIconFullpath = @ScriptDir & "\MyIcon_or_yours.ico"

SingleInstance()
Func SingleInstance()
    If WinExists($g_sBaseTitle) Then
        WinClose($g_sBaseTitle)
        If Not WinWaitClose($g_sBaseTitle, "", 10) Then Exit 3
    Else
        WinCloseOfChildOfParent($g_sBaseTitle)
        If @error Then Exit 3
    EndIf
EndFunc   ;==>SingleInstance

If Not @Compiled Then
    AutoItWinSetTitle($g_sBaseTitle & @AutoItPID)
    _WinSetIconDebug($g_sIconFullpath)
Else
    AutoItWinSetTitle($g_sBaseTitle & @AutoItPID)
    WinSetState(AutoItWinGetTitle(), "", @SW_SHOW)
    WinSetOnTop(AutoItWinGetTitle(), "", 1)
EndIf

OnAutoItExitRegister("on_exit")
; cleanup here as "WinCloseOfChildOfParent()" will close it
Func on_exit()
    Local $t = TimerInit()
    Do
        ConsoleWriteLog("..bye." & @CRLF)
        Sleep(300) ; to fake doing something
    Until TimerDiff($t) > 2000
EndFunc   ;==>on_exit

For $n = 1 To 10 ; to show off ConsoleWriteLog()
    ConsoleWriteLog('@Compiled = ' & (@Compiled = 1) & @CRLF) ; write something before any tweaking
    ConsoleWriteLog("look ma', I'm a desktop thing w/o taskbar" & @CRLF)
    ConsoleWriteLog("( press ""Win + D"" to find it )" & @CRLF)
Next

While 1
    Sleep(50)
WEnd

Func _WinSetIconDebug($sFile, $iIndex = 0, $hWnd = 0)
    If Not $hWnd Then $hWnd = WinGetHandle(AutoItWinGetTitle())
    TraySetIcon($sFile, $iIndex)

    WinMove($hWnd, "", 50, 50, 500, 300)

    ;Use BiXOR to Remove Styles & BitOR to add styles
    Local $Style = _WinAPI_GetWindowLong($hWnd, $GWL_STYLE) ;Capture Existing Style
    If BitAND($Style, $WS_MINIMIZEBOX) = $WS_MINIMIZEBOX Then ;if $WS_MINIMIZEBOX IS Set
        _WinAPI_SetWindowLong($hWnd, $GWL_STYLE, BitXOR($Style, $WS_MINIMIZEBOX)) ; REMOVE $WS_MINIMIZEBOX
    EndIf

    Local $tIcons = DllStructCreate("ptr Data")
    DllCall("shell32.dll", "uint", "ExtractIconExW", "wstr", $sFile, "int", $iIndex, "struct*", 0, "struct*", $tIcons, "uint", 1)
    If @error Then Return SetError(1, 0, 0)
    Local $hIcon = DllStructGetData($tIcons, "Data")
    _SendMessage($hWnd, 0x0080, 1, $hIcon) ;$WM_SETICON = 0x0080
    _WinAPI_DestroyIcon($hIcon)

    _WinAPI_SetParent($hWnd, WinGetHandle('[CLASS:Progman;TITLE:Program Manager]'))

    WinSetState($hWnd, "", @SW_SHOW)

    Return 1
EndFunc   ;==>_WinSetIconDebug

Func ConsoleWriteLog($s = "")
    Local Static $hWinSelf = WinGetHandle(AutoItWinGetTitle()), $iTrim = 1024 ; or bigger but not too much
    If $s = "" Then Return
    ControlSetText($hWinSelf, "", "Edit1", @HOUR & ":" & @MIN & ":" & @SEC & "." & @MSEC & " - " & $s & StringLeft(ControlGetText($hWinSelf, "", "Edit1"), $iTrim))
EndFunc   ;==>ConsoleWriteLog

Func WinCloseOfChildOfParent($sWinChild, $hWndParent = "", $iTimeout = 10)
    Local $h = WinExistsOfChildOfParent($sWinChild, $hWndParent)
    If Not $h Then Return SetError(0, 0, 0)
    WinClose($h)
    If Not WinWaitClose($h, "", $iTimeout) Then Return SetError(1, 0, 0)
    Return SetError(0, 0, 1)
EndFunc   ;==>WinCloseOfChildOfParent

Func WinExistsOfChildOfParent($sWinChild, $hWndParent = "")
    If $hWndParent = "" Then $hWndParent = WinGetHandle('[CLASS:Progman;TITLE:Program Manager]')
    Local $aWindows = _WinAPI_EnumChildWindows($hWndParent)
    Local $aResult[$aWindows[0][0]][5]
    For $i = 1 To $aWindows[0][0]
        $aResult[$i - 1][0] = $aWindows[$i][0]
        $aResult[$i - 1][1] = $aWindows[$i][1]
        $aResult[$i - 1][2] = WinGetTitle($aWindows[$i][0])
        $aResult[$i - 1][3] = WinGetText($aWindows[$i][0])
        $aResult[$i - 1][4] = WinGetProcess($aWindows[$i][0])
        If StringInStr($aResult[$i - 1][2], $sWinChild) Then
            $aResult[$i - 1][2] = ">" & $aResult[$i - 1][2] & "<"
            Return $aResult[$i - 1][0]
        EndIf
    Next
    Return 0
;~  _ArrayDisplay($aResult, "WinExistsOfChildOfParent()", Default, Default, Default, Default, "#|Handle|Class|Title|Text|Process")
EndFunc   ;==>WinExistsOfChildOfParent

This is what I wanted it for.  Thanks @Subz

Edited by argumentum
better code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

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