Sign in to follow this  
Followers 0
Hashbong

ON-OFF tray icon

10 posts in this topic

Hello,

I would like to code a tray icon in AutoIt which changes colours when a VPN connexion is turned ON or OFF.

- When my VPN connexion is off, the tray icon should be gray.

- When my VPN connexion is on, the tray icon should be blue.

- On a click on gray tray icon, the connexion is switched on... and the icon becomes blue

- On a click on blue tray icon, the connexion is switched off... and the icon becomes gray

Has somebody any clue, how to achieve that? (icon change)

Thanls in advance!

Hashbong

P.-S. target OS: Windows 7

Share this post


Link to post
Share on other sites



You could probably use a combination of TraySetIcon and TraySetState in your script to handle that. Changing the icon itself would be done by TraySetIcon, having it flash/hide/show etc. would be done by TraySetState. You'd have to have more than one icon if you're changing the colors because one icon can't be recolored as far as I am aware.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I'm almost certain that one icon can't be changed (recolored), and two different icons would have to be used. I do this in a script, using TraySetIcon.


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

I'm almost certain that one icon can't be changed (recolored), and two different icons would have to be used. I do this in a script, using TraySetIcon.

That's what I thought, but wasn't 100% sure so I left myself an out. :mellow:

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Thank you very much for your suggestions, BrewManNH.

It helped me to write (hmm, mostly copy and paste from the help)...

#include <Constants.au3>
#NoTrayIcon
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.
;$exit = TrayCreateItem("Exit")
;TrayItemSetOnEvent(-1,"ExitEvent")
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "toggleVPN")
TraySetOnEvent($TRAY_EVENT_SECONDARYUP, "ExitEvent")
TraySetState()
Global $vpnState = "OFF"
TraySetIcon("icon-off.ico")
While 1
Sleep(10) ; Idle loop
WEnd
Exit

Func toggleVPN()
if $vpnState = "ON" Then
  ShellExecuteWait ("RASDIAL.EXE", " /d",  @SystemDir, "open", @SW_MAXIMIZE)
  $vpnState = "OFF"
  TraySetIcon("icon-off.ico")
Else
  ShellExecuteWait ("RASDIAL.EXE", "MYCONNECTION1",  @SystemDir, "open", @SW_MAXIMIZE)
  $vpnState = "ON"
  TraySetIcon("icon-on.ico")
EndIf
EndFunc
Func ExitEvent()
    Exit
EndFunc

...which is a good start,

Share this post


Link to post
Share on other sites

I'm almost certain that one icon can't be changed (recolored), and two different icons would have to be used. I do this in a script, using TraySetIcon.

I'm glad you said you were "almost" certain

#include <GDIPlus.au3>
global $nottranscolour="0x000066"

While 1
$nottranscolour="0x000066"
PicSetGraphics(32, 32)
sleep(1000) ; check every 5 minutes
$nottranscolour="0x990000"
PicSetGraphics(32, 32)
sleep(1000) ; check every 5 minutes
WEnd
Func PicSetGraphics($iW, $iH)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $IMAGE_BITMAP = 0
    Local $hWnd, $hBitmap, $hImage, $hGraphic, $hBrush, $hBrush1, $hbmp, $aBmp
    _GDIPlus_Startup()
    ;Buffer
    $hBitmap = _WinAPI_CreateSolidBitmap(0, $nottranscolour, $iW, $iH)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    $sText = @MDAY
    $hBrush1 = _GDIPlus_BrushCreateSolid(_InvertColor($nottranscolour,0))
    $hFormat = _GDIPlus_StringFormatCreate(0)
    $hFamily = _GDIPlus_FontFamilyCreate("Verdana")
    $hFont = _GDIPlus_FontCreate($hFamily, 14, 0, 3)
    $tLayout = _GDIPlus_RectFCreate(0, 4, 32,32) ; increase 300 to 380 for one line of text
    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sText, $hFont, $tLayout, $hFormat)
   _GDIPlus_GraphicsDrawStringEx($hGraphic, $sText, $hFont, $tLayout, $hFormat, $hBrush1)
$hIcon = _GDIPlus_BitmapCreateHICONFromBitmap($hImage)
; Create an ico file from the image
_CreateIconFileFromHICON($hIcon, @ScriptDir & "\trayicon.ico")
$_mday=@MDAY
; Destroy the HICON now I've finished with it.
_WinAPI_DestroyIcon($hIcon)
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TraySetIcon(@ScriptDir & "\trayicon.ico")
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_BrushDispose($hBrush1)
;    _GDIPlus_BrushDispose($hBrushLin)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_DeleteObject($hbmp)
    _WinAPI_DeleteObject($hBitmap)
    _GDIPlus_Shutdown()
EndFunc   ;==>PicSetGraphics
Func _GDIPlus_BitmapCreateHICONFromBitmap($hBitmap)
    Local $hIcon
    $hIcon = DllCall($ghGDIPDll, "int", "GdipCreateHICONFromBitmap", "hwnd", $hBitmap, "int*", 0)
    If @error Then Return SetError(@error, 0, -1)
    Return SetError($hIcon[0], 0, $hIcon[2])
EndFunc   ;==>_GDIPlus_BitmapCreateHICONFromBitmap
Func _CreateIconFileFromHICON($hIcon, $sOutIcon)
    Local $aInfo, $sIco, $sBmp, $hCDC, $tBI, $tBits, $iSz, $sBD, $FO
    $sIco = "0x000001000100"
    $sBmp = "28000000"
    $aInfo = _WinAPI_GetIconInfo($hIcon)
    $hCDC = _WinAPI_CreateCompatibleDC(0)
     $tBI = DllStructCreate($tagBITMAPINFO)
    DllStructSetData($tBI, "Size", DllStructGetSize($tBI))
    _WinAPI_GetDIBits($hCDC, $aInfo[5], 0, 0, 0, DllStructGetPtr($tBI), 0)
     $sIco &= Hex(DllStructGetData($tBI, "Width"), 2) & Hex(DllStructGetData($tBI, "Height"), 2) & "00000100" & _RB(Hex(DllStructGetData($tBI, "BitCount"), 4))
    $sBmp &= _RB(Hex(DllStructGetData($tBI, "Width"))) & _RB(Hex(DllStructGetData($tBI, "Height") * 2)) & "0100" & _RB(Hex(DllStructGetData($tBI, "BitCount"), 4)) & "00000000"
     $iSz = DllStructGetData($tBI, "SizeImage")
    $tBits = DllStructCreate("byte[" & DllStructGetData($tBI, "SizeImage") & "]")
    DllCall('gdi32.dll', 'int', 'GetBitmapBits', 'ptr', $aInfo[5], 'int', $iSz, 'ptr', DllStructGetPtr($tBits))
    For $i = DllStructGetData($tBI, "SizeImage") + 1 To 0 Step -(DllStructGetData($tBI, "SizeImage") / DllStructGetData($tBI, "Height"))
        $sBD &= StringTrimLeft(BinaryMid(DllStructGetData($tBits, 1), $i, (DllStructGetData($tBI, "SizeImage") / DllStructGetData($tBI, "Height"))), 2)
    Next
    $tBits = 0
    $tBI = 0
    $tBI = DllStructCreate($tagBITMAPINFO)
    DllStructSetData($tBI, "Size", DllStructGetSize($tBI))
    _WinAPI_GetDIBits($hCDC, $aInfo[4], 0, 0, 0, DllStructGetPtr($tBI), 0)
     _WinAPI_DeleteDC($hCDC)
      $iSz += DllStructGetData($tBI, "SizeImage")
     $sBmp &= _RB(Hex($iSz)) & "00000000000000000000000000000000"
     $sIco &= _RB(Hex($iSz + 40)) & _RB(Hex("22")) & $sBmp & $sBD
     $tBits = DllStructCreate("byte[" & DllStructGetData($tBI, "SizeImage") & "]")
     DllCall('gdi32.dll', 'int', 'GetBitmapBits', 'ptr', $aInfo[4], 'int', DllStructGetData($tBI, "SizeImage"), 'ptr', DllStructGetPtr($tBits))
     For $i = DllStructGetData($tBI, "SizeImage") + 1 To 0 Step -(DllStructGetData($tBI, "SizeImage") / DllStructGetData($tBI, "Height"))
        $sIco &= StringTrimLeft(BinaryMid(DllStructGetData($tBits, 1), $i, (DllStructGetData($tBI, "SizeImage") / DllStructGetData($tBI, "Height"))), 2)
    Next
    ; Write the icon to a file.
    $FO = FileOpen($sOutIcon, 18)
    FileWrite($sOutIcon, Binary($sIco))
    FileClose($FO)
    ; Clear the structs
    $tBits = 0
    $tBI = 0
EndFunc   ;==>_CreateIconFileFromHICON
; Reverse Byte String
Func _RB($sByte)
    Local $aX = StringRegExp($sByte, "(.{2})", 3), $sX = ''
    For $i = UBound($aX) - 1 To 0 Step -1
        $sX &= $aX[$i]
    Next
    Return $sX
EndFunc   ;==>_RB
Func _InvertColor($iColor, $mode = 1)
    Switch $mode
        Case 0
            Return "0xFF" & Hex(0xFFFFFF - $iColor, 6)
        Case 1
            Return "0xFF" & StringRegExpReplace(Hex($iColor, 6), "(.{2})(.{2})(.{2})", "\3\2\1")
        Case 2
            Return "0xFF" & StringRegExpReplace(Hex(0xFFFFFF - $iColor, 6), "(.{2})(.{2})(.{2})", "\3\2\1")
    EndSwitch
EndFunc ;==>_InvertColor

Share this post


Link to post
Share on other sites

Seems like a lot of work to replace having 2 icon files :mellow:


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

agreed....

I did it with a pic...it can be done with an extracted icon using a script

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Seems like a lot of work to replace having 2 icon files :mellow:

You are looking the Kitchen.. go, sit and wait in the diner room until He finsih :) :) :party:

EDIT: emmm i was to meant he, not we I do not cook this time :)

Edited by monoscout999

Share this post


Link to post
Share on other sites

I'm glad you said you were "almost" certain

I was leaving myself an out too.


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

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