Sign in to follow this  
Followers 0
supersonic

Trouble assigning icons to tray items

1 post in this topic

#1 ·  Posted (edited)

Hi,

I was looking for a way to assign icons to tray items - I found an UDF here: http://www.autoit.de/index.php?page=Thread&postID=163013 (german site).

The UDF works so far... But I have some trouble if using it under MS-Windows XP or MS-Windows Vista/7 (only if the 'Themes' service is disabled): There's no space between tray icons and text (Untitled2.jpg); furthermore, the tray icons are inverted if you point to them with the mouse cursor (Untitled3.jpg).

Only under MS-Windows Vista/7 with 'Themes' service started the tray icons look as they should (Untitled1.jpg). Maybe this is a OS limitation?

Here's a reproducer:

#include <Array.au3>
#include <Constants.au3>
#include <GUIMenu.au3>
#include <WinAPI.au3>

AutoItSetOption("TrayMenuMode", 1) ; Opt()

Local $___TRAYITEMSETICON[1]
Local $hTRAY_Item0 = TrayCreateItem("Exit")
Local $tMsg = 0

_TrayItemSetIcon($hTRAY_Item0, @AutoItExe, 0, $___TRAYITEMSETICON)

While 1
$tMsg = TrayGetMsg()
Select
  Case $tMsg = $hTRAY_Item0
   TrayItemSetState($tMsg, $TRAY_UNCHECKED)
   ExitLoop
EndSelect
Sleep(10)
WEnd

_TrayItemSetIcon_DeleteObject($___TRAYITEMSETICON)

; [http://www.autoit.de/index.php?page=Thread&postID=163013].
Func _TrayItemSetIcon($hWnd, $sIconFile, $sIconIndex, ByRef $aHwd) ; _WinAPI_DeleteObject($aHwd)
Local $iReturn = 0
Local $hTrayItem = TrayItemGetHandle(ControlGetHandle("", "", $hWnd))
If $hTrayItem <> 0 Then
  Local $hIcon = DllStructCreate("ptr")
  If Not @error Then
   Local $iIcon = _WinAPI_ExtractIconEx($sIconFile, $sIconIndex, 0, DllStructGetPtr($hIcon), 1)
   If $iIcon <> 0 Then
    $hIcon = DllStructGetData($hIcon, 1)
    If Not @error Then
     Local $hDC1 = _WinAPI_GetDC(_WinAPI_GetDesktopWindow())
     If $hDC1 <> 0 Then
      Local $iArrayIndex = 0
      Switch (UBound($aHwd, 0) <> 1)
       Case True
        $aHwd = _ArrayCreate(0)
        ContinueCase
       Case Else
        ReDim $aHwd[UBound($aHwd) + 1]
        $aHwd[0] = UBound($aHwd) - 1
      EndSwitch
      $iArrayIndex = $aHwd[0]
      ; ----------------------------------------------------------------------------------------------------
      $aHwd[$iArrayIndex] = _WinAPI_CreateCompatibleBitmap($hDC1, 16, 16)
      If $aHwd[$iArrayIndex] <> 0 Then
       Local $hDC2 = _WinAPI_CreateCompatibleDC($hDC1)
       If $hDC2 <> 0 Then
        $iReturn = 1
        _WinAPI_ReleaseDC(_WinAPI_GetDesktopWindow(), $hDC1)
        _WinAPI_SelectObject($hDC2, $aHwd[$iArrayIndex])
        _WinAPI_DrawIconEx($hDC2, 0, 0, $hIcon, 16, 16, 0, _WinAPI_GetSysColorBrush(4), 3) ; Default tray background color.
        _WinAPI_DeleteDC($hDC2)
        _WinAPI_DestroyIcon($hIcon)
        _GUICtrlMenu_SetItemBmp($hTrayItem, $hWnd, $aHwd[$iArrayIndex], False)
       Else ; Error.
        SetError(7, 0)
       EndIf
      Else ; Error.
       SetError(6, 0)
      EndIf
     Else ; Error.
      SetError(5, 0)
     EndIf
    Else ; Error.
     SetError(4, 0)
    EndIf
   Else ; Error.
    SetError(3, 0)
   EndIf
  Else ; Error.
   SetError(2, 0)
  EndIf
Else ; Error.
  SetError(1, 0)
EndIf
Return $iReturn
EndFunc   ;==>_TrayItemSetIcon

Func _TrayItemSetIcon_DeleteObject(ByRef $aHwd)
Local $iReturn = 1
If UBound($aHwd, 0) = 1 Then
  Local $iDeleteObject = 0
  For $i = 1 To UBound($aHwd) - 1 Step 1
   $iDeleteObject += _WinAPI_DeleteObject($aHwd[$i])
   If Not $iDeleteObject Then
    $iReturn = 0
   EndIf
   ConsoleWrite("_TrayItemSetIcon_DeleteObject(" & $aHwd[$i] & ") -> [" & $iDeleteObject & "][" & $iReturn & "]" & @CRLF)
  Next
  SetExtended($iDeleteObject)
Else ; Error.
  $iReturn = 0
  SetError(1, 0)
EndIf
Return $iReturn
EndFunc   ;==>_TrayItemSetIcon_DeleteObject

Any help is welcome.

Since other programs show good looking tray icons there should be a solution to that issue!?

Greets,

-supersonic.

post-34380-0-04671800-1326135775_thumb.j

post-34380-0-57321100-1326135786_thumb.j

post-34380-0-49684900-1326135796_thumb.j

Edited by supersonic

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0