title: AutoHotKey - How to add a click handler to a displayed program's icon in a GUI?
body: <p>The examples that I've found that display a program's icon on a GUI form all seem to use a text control to indicate where the icon is to be displayed, but then send the STM_SETICON messages to actually display the image.  However, the text control's event subroutine that handles mouse clicks are never get called when I click on the displayed icon.  I've considered displaying a transparent graphic area over the icon with its own event subroutine, but seems like I should be able to attach the event handler directly to the icon's display area or load the icon into a graphic control with its own event handler.</p>
  
  <p>Anyone know how to do either of these?</p>
  
  <p>I am using AutoHotKey L (v 1.1.09.04) 64 bit on Windows 7 Pro.</p>
  
  <p>Here is the code I found while searching for a way to display the icon, which I am now using.</p>
  
  <pre><code>FileName := "C:\XXXX\XXXX.exe" ; Get the icon from a program file.
  
  Ignored := 0
  
  ;
  ; CopyImage into memory and scale it to size.
  ;
  IMAGE_ICON          := 1
  LR_COPYFROMRESOURCE := 4
  LR_COPYDELETEORG    := 8
  
  IconSize      := 16 ; Icon size: X by X
  ptr           := ( ( A_PtrSize = 8 ) ? "ptr" : "uint" )
  sfi_size      := A_PtrSize + 8 + ( ( A_IsUnicode ) ? 680 : 340 )
  VarSetCapacity( sfi, sfi_size )
  DllCall( "Shell32\SHGetFileInfo" . ( ( A_IsUnicode ) ? "W" : "A" )
              , "str",  FileName
              , "uint", Ignored
              , ptr,    &amp;sfi
              , "uint", sfi_size
              , "uint", SHGFI_ICON )
  hIcon         := NumGet( sfi, 0 )
  hicon_resized := DllCall( "CopyImage"
                            , ptr,    hicon
                            , "uint", IMAGE_ICON
                            , "int",  IconSize
                            , "int",  IconSize
                            , "uint", LR_COPYFROMRESOURCE | LR_COPYDELETEORG
                            , ptr )
  DllCall( "DestroyIcon", ptr, hicon )
  
  STM_SETICON := 0x0170
  Gui, Add, Text, x5 y8 w%IconSize% h%IconSize% hwndMyPic %SS_ICON% gClicked
  SendMessage, STM_SETICON, hicon_resized, 0,, Ahk_ID %MyPic%
  </code></pre>
  
  <p>I want to change the icon to be embedded in my script and to also use is a the taskbar and tray icon, and I want this to all work without having to compile the script.</p>