Set Script Icon in Alt+Tab Window

Get help with using AutoHotkey and its commands and hotkeys
JJohnston2
Posts: 193
Joined: 24 Jun 2015, 23:38

Set Script Icon in Alt+Tab Window

04 Jul 2016, 03:08

Under the Window Appearance section of the GUI help topic, there is some code to load an icon that will show up in the Alt+Tab Task Switcher...

Code: Select all

hIcon32 := DllCall("LoadImage", uint, 0
    , str, "My Icon.ico"  ; Icon filename (this file may contain multiple icons).
    , uint, 1  ; Type of image: IMAGE_ICON
    , int, 32, int, 32  ; Desired width and height of image (helps LoadImage decide which icon is best).
    , uint, 0x10)  ; Flags: LR_LOADFROMFILE
Gui +LastFound
SendMessage, 0x80, 1, hIcon32  ; 0x80 is WM_SETICON; and 1 means ICON_BIG (vs. 0 for ICON_SMALL).
Gui Show
This code (above) requires the use of an external icon file.

I use shell32.dll icons in many cases, in order to avoid the need for external icon files, and the limited selection of icons in shell32.dll suits the limited number of cases where I actually need to do this.

This works just fine for setting a tray icon, for example...

Code: Select all

    ;-----------------------------------------------------------------------------
    ; Create small icon for the tray
    ;-----------------------------------------------------------------------------
    Menu, Tray, Icon, Shell32.dll, 255
    ;-----------------------------------------------------------------------------
On the other hand, I have been unable to get the shell32.dll icons to show up as a large icon in the Alt+Tab Task Switcher.

This code below doesn't work--an attempt at adapting the help topic code above.

If anyone sees any mistakes in the DLL call arguments please let me know... this code is executed before the GUI is created via Gui, Show

Code: Select all

    ;-----------------------------------------------------------------------------
    ; Create large icon for Alt+Tab switch window
    ; https://msdn.microsoft.com/en-us/library/windows/desktop/ms648045(v=vs.85).aspx
    ;-----------------------------------------------------------------------------
    hIcon32 := DllCall("LoadImage", unit, hInst:=DllCall("GetModuleHandle", str, "Shell32.dll")
                                  , str,  "#255"         ; Icon resource # within DLL
                                  , uint, IMAGE_ICON:=1
                                  , int,  IcoWidth:=32, int, IcoHeight:=32       ; Desired width and height of image (helps LoadImage decide which icon is best).
                                  , uint, Flags:=0)
        
    SendMessage, WM_SETICON:=0x80, ICON_BIG:=1, hIcon32
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Set Script Icon in Alt+Tab Window

04 Jul 2016, 08:38

I'm not sure what icon you actually want. Resource ID 255 in shell32.dll on my Windows 10 system contains a green curved arrow pointing upwards. The Menu, Tray, Icon command you posted shows an icon of a window with two blue panes: one at the left and one at the bottom. Since you said that that command works fine and the latter icon looks better anyway, I'll assume that you meant the window pane icon, which is stored at resource ID 16742.

Code: Select all

#Persistent
 
;Menu, Tray, Icon, Shell32.dll, -16742
hIcon32 := DllCall("LoadImage", ptr, hInst:=DllCall("GetModuleHandle", str, "Shell32.dll", ptr)
                              , str,  "#16742"         ; Icon resource # within DLL
                              , uint, IMAGE_ICON:=1
                              , int,  IcoWidth:=32, int, IcoHeight:=32       ; Desired width and height of image (helps LoadImage decide which icon is best).
                              , uint, Flags:=0)
Gui +LastFound
SendMessage, % WM_SETICON:=0x80, % ICON_BIG:=1, hIcon32 ; https://autohotkey.com/docs/Variables.htm#Expressions
                                                        ; to a commmand like SendMessage, passing WM_SETICON:=0x80 directly
                                                        ; means you're passing "WM_SETICON:=0x80" instead of setting the variable WM_SETICON to 0x80 
                                                        ; and passing the result of that, 0x80, to SendMessage. Use the percent to force evaluation as an expression
                                                        
; You might be able to call DestroyIcon on the icon handle after SendMessage, but don't quote me on that
Gui Show
JJohnston2
Posts: 193
Joined: 24 Jun 2015, 23:38

Re: Set Script Icon in Alt+Tab Window

05 Mar 2017, 21:19

So the tray icon selector index (for lack of a better term) is not the same lookup number when used with the LoadImage DLL call, i.e., it does not reference the same icon. When the value 255 is used for the Tray Icon it yields one thing and when 255 is used for the Resource ID it yields another.

I can find the tray icons I want and their corresponding index relatively easily, but for this SendMessage code however, I don't know how to cross-reference the two, or perhaps to just manually find an appropriate ResourceID (if there is not a way to directly cross-reference the two), in order to generate the 'same' icon (i.e., an icon that looks the same but is larger).

In the post above, Resource ID 16742 was recommended (and indeed generates a similar icon)--where did that value come from?

i.e., What is a usable lookup process to determine the Resource ID for a given/desired icon?

EDIT

After looking around further, what I currently don't understand is why the icon indices in these two images are different (where they come from)
1) shell32_icons.jpg
2) shell32_icons.jpg
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Set Script Icon in Alt+Tab Window

03 Jun 2017, 15:34

Try NirSoft IconsExtract and Resource Hacker.

There is a difference between an icon and an icon group, and differences between different operating systems (try copying shell32.dll from PCs with different Windows versions and comparing in IconsExtract).

C:\Windows\System32\shell32.dll

Note also:
Menu
https://autohotkey.com/docs/commands/Menu.htm
To use an icon group other than the first one in the file, specify its number for IconNumber (if omitted, it defaults to 1). If IconNumber is negative, its absolute value is assumed to be the resource ID of an icon within an executable file.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot] and 37 guests