Sign in to follow this  
Followers 0
power1power1

Writing Text in Systray

9 posts in this topic

Hello, I am trying to write a piece of text in systray. Looking at various web pages across the internet, I came up with the following code to create a bitmap, write the text in that bitmap and set the bitmap as a systray icon. Here are my questions:

- The code doesn't do it and I only get a black icon. Can you see what is missing?

- It seems too many code lines for such a task. Maybe you guys have a better solution.

#include <WinAPIGdi.au3>
#include <FontConstants.au3>
#include <WinAPIShellEx.au3>

$Text = "100"
$hDC = _WinAPI_GetDC(0)
$hMemDC = _WinAPI_CreateCompatibleDC($hDC)
$hBitmap = _WinAPI_CreateCompatibleBitmapEx($hDC, 32, 32, 0)
$hBitmapMask = _WinAPI_CreateCompatibleBitmapEX($hDC, 32, 32, 0)
$hOldBitmap = _WinAPI_SelectObject($hMemDC, $hBitmap)
$hFont = _WinAPI_CreateFont(10, 0)
;$hOldFont = _WinAPI_SelectObject($hDC, $hFont)
_WinAPI_SetTextColor($hOldBitmap, 0xFF0000)
_WinAPI_TextOut( $hOldBitmap, 0, 0, $Text)

$hIcon = _WinAPI_CreateIconIndirect($hBitmap, $hBitmapMask)

$tNOTIFYICONDATA = DllStructCreate($tagNOTIFYICONDATA)
DllStructSetData($tNOTIFYICONDATA, 'Size', DllStructGetSize($tNOTIFYICONDATA))
DllStructSetData($tNOTIFYICONDATA, 'hWnd', WinGetHandle(AutoItWinGetTitle()))
DllStructSetData($tNOTIFYICONDATA, 'Flags', $NIF_ICON)
DllStructSetData($tNOTIFYICONDATA, 'ID', 2)
DllStructSetData($tNOTIFYICONDATA, 'hIcon', $hIcon)
_WinAPI_ShellNotifyIcon($NIM_ADD, $tNOTIFYICONDATA)

sleep(3000)

_WinAPI_DeleteObject($hFont)
_WinAPI_DeleteObject($hBitmapMask)
_WinAPI_DeleteObject($hBitmap)
_WinAPI_DeleteDC($hMemDC)
_WinAPI_ReleaseDC(0, $hDC)
DllStructSetData($tNOTIFYICONDATA, 'ID', 2)
_WinAPI_ShellNotifyIcon($NIM_DELETE, $tNOTIFYICONDATA)

Share this post


Link to post
Share on other sites



Thanks for the reply. It seems "Toast.au3" creates a pop up window above systray. I am trying to write text in the systray, where the icons are ...

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks Frank. Well, I made some progress: Now the script works and I have a timer (text in red, background in black) in the systray. Still, these are things to do, if someone could help:

- I can't get the background color of the bitmap to be transparent and I can't set it to another color, say green.

- The text is at top-left corner of the bitmap and I can't move the text to the middle of the bitmap.

- The code really seems to be too big for such a purpose.

#include <WindowsConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPIShellEx.au3>
OnAutoItExitRegister("_OnAutoItExit")
$Text = 0

While 1
    _CreateSmallIcon($Text)
    Sleep(1000)
    $Text += 1
WEnd

Func _CreateSmallIcon($Text)
   $hDC = _WinAPI_GetDC(0)
   $hDCMem = _WinAPI_CreateCompatibleDC($hDC)
   $hBitmap = _WinAPI_CreateCompatibleBitmapEx($hDC, 32, 32, 0)
   $hBitmapMask = _WinAPI_CreateCompatibleBitmapEx($hDC, 32, 32, 0)
   _WinAPI_ReleaseDC(0, $hDC)
   $hOldBitmap = _WinAPI_SelectObject($hDCMem, $hBitmap)
   ;_WinAPI_PatBlt($hDCMem, 0, 0, 32, 32, $WHITENESS)
   _WinAPI_PatBlt($hDCMem, 0, 0, 32, 32, $BLACKNESS)

   $hFont = _WinAPI_CreateFont(22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Arial")
   $hFont = _WinAPI_SelectObject($hDCMem, $hFont)
   _WinAPI_SetTextColor($hDCMem, 0x0000FF)
   ;_WinAPI_SetBkColor($hDCMem, 0x00FF00)
   _WinAPI_SetBkMode($hDCMem, $TRANSPARENT)
   _WinAPI_TextOut($hDCMem, 0, 0, $Text)

   _WinAPI_SelectObject($hDC, $hOldBitmap)
   $hOldBitmap = 0

   $hIcon = _WinAPI_CreateIconIndirect($hBitmap, $hBitmapMask)

   _WinAPI_DeleteObject(_WinAPI_SelectObject($hDCMem, $hFont))
   _WinAPI_DeleteDC($hDCMem)
   _WinAPI_DeleteDC($hDC)
   _WinAPI_DeleteObject($hBitmap)
   _WinAPI_DeleteObject($hBitmapMask)

   Global $tNOTIFYICONDATA = DllStructCreate($tagNOTIFYICONDATA)
   DllStructSetData($tNOTIFYICONDATA, 'Size', DllStructGetSize($tNOTIFYICONDATA))
   DllStructSetData($tNOTIFYICONDATA, 'hWnd', WinGetHandle(AutoItWinGetTitle()))
   DllStructSetData($tNOTIFYICONDATA, 'Flags', $NIF_ICON)
   DllStructSetData($tNOTIFYICONDATA, 'ID', 1)
   DllStructSetData($tNOTIFYICONDATA, 'hIcon', $hIcon)
   _WinAPI_ShellNotifyIcon($NIM_MODIFY, $tNOTIFYICONDATA)
EndFunc

Func _OnAutoItExit()
    DllStructSetData($tNOTIFYICONDATA, 'ID', 1)
    _WinAPI_ShellNotifyIcon($NIM_DELETE, $tNOTIFYICONDATA)
EndFunc
Edited by power1power1

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I have an updated code ... It works now ... May be there is a more efficient way of doing this.

#include <WinAPIGdi.au3>
#include <WinAPIShellEx.au3>
#include <WindowsConstants.au3>
OnAutoItExitRegister("_RemoveSysTrayIcon")
$Text = 0
$TextColor = 0x0000FF
$BackColor = 0x00FF00

While 1
    _CreateSysTrayIcon($Text, $TextColor, $BackColor)
    Sleep(1000)
    $Text += 1
WEnd

Func _CreateSysTrayIcon($Text, $TextColor, $BackColor)
   $hDC = _WinAPI_GetDC(0)
   $hDCMem = _WinAPI_CreateCompatibleDC($hDC)
   $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, 32, 32)
   _WinAPI_ReleaseDC(0, $hDC)

   $hOldBitmap = _WinAPI_SelectObject($hDCMem, $hBitmap)
   _WinAPI_PatBlt($hDCMem, 0, 0, 32, 32, $PATCOPY)

   $hFont = _WinAPI_CreateFont(32, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Calibri")
   $hFont = _WinAPI_SelectObject($hDCMem, $hFont)
   _WinAPI_DeleteObject($hFont)

   _WinAPI_SetTextColor($hDCMem, $TextColor)
   _WinAPI_SetBkColor($hDCMem, $BackColor)
   _WinAPI_SetBkMode($hDCMem, $TRANSPARENT)
   _WinAPI_TextOut($hDCMem, 0, 0, $Text)

   $hBitmapMask = _WinAPI_SelectObject($hDCMem, $hOldBitmap)
   _WinAPI_DeleteDC($hDCMem)
   _WinAPI_DeleteObject($hOldBitmap)

   $hIcon = _WinAPI_CreateIconIndirect($hBitmap, $hBitmapMask)
   _WinAPI_DeleteObject($hBitmap)
   _WinAPI_DeleteObject($hBitmapMask)

   Global $tNOTIFYICONDATA = DllStructCreate($tagNOTIFYICONDATA)
   DllStructSetData($tNOTIFYICONDATA, 'Size', DllStructGetSize($tNOTIFYICONDATA))
   DllStructSetData($tNOTIFYICONDATA, 'hWnd', WinGetHandle(AutoItWinGetTitle()))
   DllStructSetData($tNOTIFYICONDATA, 'Flags', $NIF_ICON)
   DllStructSetData($tNOTIFYICONDATA, 'ID', 1)
   DllStructSetData($tNOTIFYICONDATA, 'hIcon', $hIcon)
   _WinAPI_ShellNotifyIcon($NIM_MODIFY, $tNOTIFYICONDATA)
EndFunc

Func _RemoveSysTrayIcon()
    DllStructSetData($tNOTIFYICONDATA, 'ID', 1)
    _WinAPI_ShellNotifyIcon($NIM_DELETE, $tNOTIFYICONDATA)
EndFunc
Edited by power1power1

Share this post


Link to post
Share on other sites

hi,

i get this error:

C:Program Files (x86)AutoIt3IncludeWinAPIGdi.au3 (25) : ==> Variable used without being declared.:
Global Const $tagDIBSECTION = $tagBITMAP & ';' & $tagBITMAPINFOHEADER & ';dword dsBitfields[3];ptr dshSection;dword dsOffset'
Global Const $tagDIBSECTION = $tagBITMAP & ';' & ^ ERROR

What could be the reason?

Thx!

Share this post


Link to post
Share on other sites

Well, I am not that expert in these stuff ... May be using the latest version of Autoit is needed.

Share this post


Link to post
Share on other sites

hi,

 

i get this error:

 

C:Program Files (x86)AutoIt3IncludeWinAPIGdi.au3 (25) : ==> Variable used without being declared.:

Global Const $tagDIBSECTION = $tagBITMAP & ';' & $tagBITMAPINFOHEADER & ';dword dsBitfields[3];ptr dshSection;dword dsOffset'

Global Const $tagDIBSECTION = $tagBITMAP & ';' & ^ ERROR

 

What could be the reason?

 

Thx!

The message is pretty clear or don't you understand English? You're using an older version of AutoIt.

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

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