get image of menu icon/tray icon

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

get image of menu icon/tray icon

Post by jeeswg » 26 Apr 2017, 21:05

I am trying to get the image data from a menu icon (or tray icon). E.g. to compare it with another image, or to put it on the clipboard / save it as a file.

I appear to be retrieving the hBitmap of a menu icon correctly, but am having trouble retrieving its dimensions or checking if it's an icon, which are first steps before then retrieving the image data.

Code: Select all

q:: ;get menu icon hBitmap
;e.g. Explorer's New menu
SendMessage, 0x1E1, 0, 0,, ahk_class #32768 ;MN_GETHMENU
hMenu := ErrorLevel
vPos := 0
;MIIM_BITMAP := 0x80
vSize := A_PtrSize=8?80:48
VarSetCapacity(MENUITEMINFO, vSize, 0)
NumPut(vSize, MENUITEMINFO, 0, "UInt") ;cbSize
NumPut(0x80, MENUITEMINFO, 4, "UInt") ;fMask
DllCall("GetMenuItemInfo", Ptr,hMenu, UInt,vPos, Int,1, Ptr,&MENUITEMINFO)
hBitmap := NumGet(MENUITEMINFO, A_PtrSize=8?72:44, "Ptr") ;hBitmap

vSize := A_PtrSize=8?32:24
VarSetCapacity(BITMAP, vSize, 0)
DllCall("gdi32\GetObject", Ptr,hBitmap, Int,vSize, Ptr,&BITMAP)
vImgW := NumGet(BITMAP, 4, "Int") ;bmWidth
vImgH := NumGet(BITMAP, 8, "Int") ;bmHeight

hIcon := hBitmap
vSize := A_PtrSize=8?32:20
VarSetCapacity(ICONINFO, vSize, 0)
DllCall("user32\GetIconInfo", Ptr,hIcon, Ptr,&ICONINFO)
vIsIcon := NumGet(ICONINFO, 0, "Int") ;fIcon
MsgBox, % hBitmap "`r`n" vImgW " " vImgH " " vIsIcon ;all returning 0
return
Btw is there a good way to check what a handle, e.g. hIcon, hBitmap, hFont, hWnd, is a handle to, or if it's valid?

Btw I wanted to do some tests on internal GUI menus, but it appears you can't launch a hotkey or do a SetTimer action while a menu is being displayed.

Cheers.

==================================================

Some links that may be relevant:
[example of a menu with icons]
Menu
https://autohotkey.com/docs/commands/Menu.htm
[SOLVED]Extract programs icon in systray - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/7624 ... n-systray/
c++ - Copying a bitmap from another HBITMAP - Stack Overflow
http://stackoverflow.com/questions/5687 ... er-hbitmap

A link to an interesting related problem:
DllCall - Assign icon for “Copy/Cut/Paste/Delete” Windows default context menu items - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30377
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

william_ahk
Posts: 486
Joined: 03 Dec 2018, 20:02

Re: get image of menu icon/tray icon

Post by william_ahk » 29 Jan 2023, 00:14

I'm trying to do the same thing with tray icons. The feat I'm trying to accomplish is to check if a program's tray icon turns into a certain icon. I've managed to implement this by saving the hicon and comparing the files, but it's too wasteful. How can the hicons be compared in-memory? Can anyone help with some GDI blackmagic? :D

#include <TrayIcon>
#include <SaveHICONtoFile>
#include <FileCompare>

Code: Select all

info := TrayIcon_GetInfo("Program.exe")
SaveHICONtoFile(info[1].hIcon, "tmp.ico")
Msgbox % FileCompare("tmp.ico", "state.ico")

Post Reply

Return to “Ask for Help (v1)”