Anybody using Menu, Tray, Icon, HICON:%hIcon% ? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Anybody using Menu, Tray, Icon, HICON:%hIcon% ?

03 Sep 2017, 17:56

I don't get desirable results with following script.

Alt-tab icon is okay
Tray icon is blank
Title-bar icon is default (Green-H)

AutoHotkey v1.1.26.00 on Windows 7 x64

What Am I missing? :roll:

Code: Select all

#NoEnv
#SingleInstance, Force
#NoTrayIcon

hIcon := LoadPicture( "user32.dll", "Icon5 w16 h16", VType )
Menu, Tray, Icon, HICON:%hIcon%
Menu, Tray, Icon

Gui, Show, w200 h200, Testing
Return
PS:
Actually I am trying to generate and use Icon with in-script base64 PNG data.
Ref: Base64 to HICON : Native PNG Decompression (requires WIN VISTA and later)


Solution:

Menu, Tray, Icon, HICON:*%hICON%

Code: Select all

; Ask for help topic: https://autohotkey.com/boards/viewtopic.php?t=36640

#NoEnv
#SingleInstance, Force
#NoTrayIcon
Menu, Tray, UseErrorLevel
hICON := Base64toHICON()               ; Create a HICON
Menu, Tray, Icon, HICON:*%hICON%       ; AHK makes a copy of HICON when * is used
Menu, Tray, Icon
DllCall( "DestroyIcon", "Ptr",hICON )  ; Destroy original HICON

Gui, Show, w400 h200, TrayIcon with in-script Base64 PNG image raw data
Return 

Base64toHICON() { ; 16x16 PNG image (236 bytes), requires WIN Vista and later
Local B64 := "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAs0lEQVQ4jbXTO27CUBCF4e9a7rIJECugZ"
. "QlZQhoUSmihg8rpqKEOTZbAEihhBUjsxBSWEZYY2Qrwl/fM49zRTDoWYIwVBrpxRoFdjik2HRNrBvjFR455/TpclreI0"
. "09qZATaPEPvUYu/Q9z+TutlSHFoKyl7IhnkkbDel5HU4H0OFp/N0USOwgLwNWp38PQXXlKg27gfU+a4oE+1otEGBtolH"
. "Yt/HVPNLMMWE9WJduWMb2yvxO4l4nkelNAAAAAASUVORK5CYII=",       Bin, Blen, nBytes:=236, hICON:=0                     
  
  VarSetCapacity( Bin,nBytes,0 ), BLen := StrLen(B64)
  If DllCall( "Crypt32.dll\CryptStringToBinary", "Str",B64, "UInt",BLen, "UInt",0x1
            , "Ptr",&Bin, "UIntP",nBytes, "Int",0, "Int",0 )
     hICON := DllCall( "CreateIconFromResourceEx", "Ptr",&Bin, "UInt",nBytes, "Int",True
                     , "UInt",0x30000, "Int",16, "Int",16, "UInt",0, "UPtr" )            
Return hICON
}
My Scripts and Functions: V1  V2
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: Anybody using Menu, Tray, Icon, HICON:%hIcon% ?

04 Sep 2017, 02:45

Helgef wrote:try "hicon:*" hicon
Wouldn't that be % "hicon:" *hicon if you want to dereference it or is that not what you are suggesting ?
Please excuse my spelling I am dyslexic.
just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Anybody using Menu, Tray, Icon, HICON:%hIcon% ?

04 Sep 2017, 03:11

By default, AutoHotkey treats the handle as though it loaded the image from file - for example, a bitmap used on a Picture control is deleted when the GUI is destroyed, and an image will generally be deleted immediately if it needs to be resized. To avoid this, put an asterisk between the colon and handle. For example: hbitmap:*%handle% (or "hbitmap:*" handle in an expression). With the exception of ImageSearch, this forces the command to take a copy of the image.

Image Handles
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Anybody using Menu, Tray, Icon, HICON:%hIcon% ?

04 Sep 2017, 07:29

Helgef wrote:try "hicon:*" hicon
Thank you very much! :D

I have included a demo script in title post.

@ just me:
Thanks! Yet again I've to read RTFM.
FYI, I am rewriting IconEx to support display of PNG icons. I am using your version of IconEx as reference.
Very helpful and many thanks!
My Scripts and Functions: V1  V2
just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Anybody using Menu, Tray, Icon, HICON:%hIcon% ?

04 Sep 2017, 08:40

Hi SKAN,

I quoted for Capn Odin, not for you. I still was thinking about trying the * option when Helgef posted the solution. At last, I think it should be mentioned in the docs that you need this option for the tray icon to work properly.

Best wishes,
just me
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Anybody using Menu, Tray, Icon, HICON:%hIcon% ?

04 Sep 2017, 08:57

just me wrote:I quoted for Capn Odin, not for you. I still was thinking about trying the * option when Helgef posted the solution. At last, I think it should be mentioned in the docs that you need this option for the tray icon to work properly.
Dear just me,

Your quote from docs was helpful to me.
I was looking at the wrong place (It was the right place, intuitively)
https://autohotkey.com/docs/commands/Me ... st_be_TRAY

Specifically:
[v1.1.23+]: A bitmap or icon handle can be used instead of a filename. For example, HBITMAP:%handle%.
HBITMAP is not working for TrayIcon, or maybe I'm just tired and doing it incorrectly.
My Scripts and Functions: V1  V2
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Anybody using Menu, Tray, Icon, HICON:%hIcon% ?

04 Sep 2017, 09:54

SKAN wrote:HBITMAP is not working for TrayIcon
Brief testing suggests you are right.
stealzy
Posts: 91
Joined: 01 Nov 2015, 13:43

Re: Anybody using Menu, Tray, Icon, HICON:%hIcon% ?

21 Jan 2018, 07:37

Fixed in last versions:

Code: Select all

BattleCityPNG_base64 := "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAxQTFRF+Nh4/6BErHwA////Jm4jCgAAAAR0Uk5T////AEAqqfQAAACXSURBVHja7JZBDoAwCARb/P+f1caApQvBxIvCnKqFOXRNpW0u7cSvKEEJXhEMOhMQzB0lOATEmMXqHU2U4OpqEog657GSahGgGPMK9HkyHUArJVi/RPNRWcwYUwpQCiIAHT0QYzaBn4IpiMaYQ+DfB7L7KMZsAvQX8q8CP8YfCG4DdFCAZiSyiA5Z2QT+tI4wY/yqYBdgALy1GaKdVEwoAAAAAElFTkSuQmCC"
Menu Tray, Icon, % "HBITMAP:*" hICON := CreateBitMap(BattleCityPNG_base64)
Menu Tray, Icon
; MsgBox % DllCall("Gdiplus.dll\DeleteObject", "Ptr",hICON) ; ???
Gui Show, w400 h200, TrayIcon with in-script Base64 PNG image raw data
Return

CreateBitMap(ByRef strB64) {
	VarSetCapacity(B64, StrLen(strB64) << !!A_IsUnicode) ; Not need on short string
	B64 := strB64, strB64 := "" ; free memory
	If !DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &B64, "UInt", 0, "UInt", 0x01, "Ptr", 0, "UIntP", DecLen, "Ptr", 0, "Ptr", 0)
		Return False
	VarSetCapacity(Dec, DecLen, 0)
	If !DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &B64, "UInt", 0, "UInt", 0x01, "Ptr", &Dec, "UIntP", DecLen, "Ptr", 0, "Ptr", 0)
		Return False
	hData := DllCall("Kernel32.dll\GlobalAlloc", "UInt", 2, "UPtr", DecLen, "UPtr")
	pData := DllCall("Kernel32.dll\GlobalLock", "Ptr", hData, "UPtr")
	DllCall("Kernel32.dll\RtlMoveMemory", "Ptr", pData, "Ptr", &Dec, "UPtr", DecLen)
	DllCall("Kernel32.dll\GlobalUnlock", "Ptr", hData)
	DllCall("Ole32.dll\CreateStreamOnHGlobal", "Ptr", hData, "Int", True, "PtrP", pStream)
	hGdip := DllCall("Kernel32.dll\LoadLibrary", "Str", "Gdiplus.dll", "UPtr")
	VarSetCapacity(SI, 16, 0), NumPut(1, SI, 0, "UChar")
	DllCall("Gdiplus.dll\GdiplusStartup", "PtrP", pToken, "Ptr", &SI, "Ptr", 0)
	DllCall("Gdiplus.dll\GdipCreateBitmapFromStream",  "Ptr", pStream, "PtrP", pBitmap)
	DllCall("Gdiplus.dll\GdipCreateHBITMAPFromBitmap", "Ptr", pBitmap, "PtrP", hBitmap, "UInt", 0)
	DllCall("Gdiplus.dll\GdipDisposeImage", "Ptr", pBitmap)
	DllCall("Gdiplus.dll\GdiplusShutdown", "Ptr", pToken)
	DllCall("Kernel32.dll\FreeLibrary", "Ptr", hGdip)
	DllCall(NumGet(NumGet(pStream + 0, 0, "UPtr") + (A_PtrSize * 2), 0, "UPtr"), "Ptr", pStream)
	Return hBitmap
}

Esc::ExitApp
#NoEnv
#NoTrayIcon
#SingleInstance Force
Btw, the quality of bitmap is much better!
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Anybody using Menu, Tray, Icon, HICON:%hIcon% ?

21 Jan 2018, 09:52

stealzy, thanks for the note, indeed, it seems good. :thumbup:

Cheers.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Anybody using Menu, Tray, Icon, HICON:%hIcon% ?

11 Feb 2018, 00:21

The original script works with v1.1.28.00, but note:
  • Tray icons are not necessarily 16x16.
  • The icon you pass is also used in Alt-Tab and on the task bar in Windows 7 and later.
If you pass a 16x16 icon, it looks pretty bad, especially on systems with >100% DPI.

You can get the icon sizes with GetSystemMetrics:

Code: Select all

large_width := DllCall("GetSystemMetrics", "int", 11) ; SM_CXICON
small_width := DllCall("GetSystemMetrics", "int", 49) ; SM_CXSMICON
However, I don't think the large icon size is actually used in Alt-Tab or the task bar on Windows 10; the icons are slightly smaller than that.

If you don't want blurry icons, you can either pass a large icon for both, or pass a small icon and then set a different large icon for each GUI with WM_SETICON.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], hedehede81 and 275 guests