LoadPicture to use it as icon

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

LoadPicture to use it as icon

13 Dec 2017, 20:14

I have 16x16 pixels png-file in the same directory with ahk-script. Is there way to use it as icon? For some reason, this doesn't work:

Code: Select all

#Persistent
#SingleInstance, Force

handle := LoadPicture("icon.png")

Gui, Add, Pic, w100 h-1, % "HBITMAP:*" handle ; works
Gui, Show

Menu, Tray, Icon, % "HBITMAP:*" handle ; doesn't work

GuiClose:
GuiEscape:
ExitApp
Last edited by john_c on 13 Dec 2017, 21:17, edited 2 times in total.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: LoadPicture to use it as icon

13 Dec 2017, 20:17

Change "HBITMAP" to "HICON".
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: LoadPicture to use it as icon

13 Dec 2017, 20:20

Osprey - And what? Yes, I tried, it doesn't help. Also, as I understand, if I want use png-files, I must use HBITMAP, not HICON.
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: LoadPicture to use it as icon

14 Dec 2017, 00:28

Hi, john_c,
Icon [, FileName, IconNumber, 1]: Changes the script's icon to one of the ones from FileName. The following types of files are supported: ICO, CUR, ANI, EXE, DLL, CPL, SCR, and other types that contain icon resources.
So, png is not supported.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: LoadPicture to use it as icon

14 Dec 2017, 03:03

teadrinker wrote:So, png is not supported.
Hi, teadrinker! Well, probably you are correct, thank you. I was confused by this:
[v1.1.23+]: A bitmap or icon handle can be used instead of a filename. For example, HBITMAP:%handle%.
However, now I understand, that this probably works only for _inner_ menu items. (Which are shown when you click on tray icon with right mouse button).
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: LoadPicture to use it as icon

14 Dec 2017, 06:37

You can load a bmp as hIcon with LoadPicture, but not png it seems.

Code: Select all

hIcon  := LoadPicture("icon.bmp", "Icon1", io)
Note that the documentation says
Iconn: Indicates which icon to load from a file with multiple icons (generally an EXE or DLL file). If n is non-zero, the file must contain an icon. For example, "Icon2" loads the file's second icon
but I didn't manage to get non-zero handle returned in any other way, when I want an icon. Maybe someone can clarify. In my experience, the resulting icon using the above method is usually subpar.
Therefor, I have written a script for loading images to hBitmap, hIcon or hCursor, it works well with png to hIcon as long as the png doesn't contain any transparency, you can try

Code: Select all

#Persistent
#SingleInstance, Force

IMAGE_ICON := 1
bkColor:=0xffffff	; This color will be transparent, you might need to change this.
ico := new LoadPictureType("icon.png",, IMAGE_ICON, bkColor)

Gui, Add, Pic, w100 h-1, % "hIcon:*" ico.getHandle() ; works
Gui, Show

Menu, Tray, Icon, % "hIcon:*" ico.getHandle() ; works

return

GuiClose:
GuiEscape:
	ExitApp
#include LoadPictureType.ahk
you need LoadPictureType.ahk. If the png contains transparency, you can open it in paint and save it as iconb.png to get rid of the transparency, then set bkColor to which ever color the tranparency became, in one test it became white :D .

cheers.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: LoadPicture to use it as icon

14 Dec 2017, 15:16

Helgef wrote:You can load a bmp as hIcon with LoadPicture, but not png it seems.

Code: Select all

hIcon  := LoadPicture("icon.bmp", "Icon1", io)
Note that the documentation says
Iconn: Indicates which icon to load from a file with multiple icons (generally an EXE or DLL file). If n is non-zero, the file must contain an icon. For example, "Icon2" loads the file's second icon
but I didn't manage to get non-zero handle returned in any other way, when I want an icon. Maybe someone can clarify. In my experience, the resulting icon using the above method is usually subpar.
Therefor, I have written a script for loading images to hBitmap, hIcon or hCursor, it works well with png to hIcon as long as the png doesn't contain any transparency, you can try

Code: Select all

#Persistent
#SingleInstance, Force

IMAGE_ICON := 1
bkColor:=0xffffff	; This color will be transparent, you might need to change this.
ico := new LoadPictureType("icon.png",, IMAGE_ICON, bkColor)

Gui, Add, Pic, w100 h-1, % "hIcon:*" ico.getHandle() ; works
Gui, Show

Menu, Tray, Icon, % "hIcon:*" ico.getHandle() ; works

return

GuiClose:
GuiEscape:
	ExitApp
#include LoadPictureType.ahk
you need LoadPictureType.ahk. If the png contains transparency, you can open it in paint and save it as iconb.png to get rid of the transparency, then set bkColor to which ever color the tranparency became, in one test it became white :D .

cheers.
Hi Helgef! Thank you very much. Icons in AHK is a bit weird thing. "If you want to use bmp (bitmap image), you need to use hIcon, not hBitmap"... Hm! )))

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Rohwedder and 264 guests