Changing SysTray icon

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

Anyone know how I can change the SysTray icon of my app to use my app's actual
executable icon? I looked at all the examples here but they don't work for me
because I am starting my app with the SysTray icon as #IDI_EXCLAMATION instead
of being my exe icon... so I need to use the ChangeSysTrayIcon command to change
it, but I can't get it to work...


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Don.

Hope I've understood what you're after .....

The first thing you must do is to set up your chosen icon in PB's "Compiler Options -> Use Icon", and then create an executable of your code. This executable then contains your icon as a resource.

Next you'll need something like this (I use this code to create an invisible main window, add the systray icon by extracting the icon from the executable, adding a tooltip and then checking for the right mouse button in the event loop):

Code: Select all

; Open the main window and make it invisible
;
hwn = OpenWindow(0,0,0,0,0,#WS_VISIBLE,"Test")
If hwn
  AddSysTrayIcon(0, hwn, ExtractIcon_(GetModuleHandle_(#NULL),"yourexe.exe",0))
  SysTrayIconToolTip0,"Your Appname")
Else
  MessageRequester("Your Appname","Cannot open window",#MB_ICONEXCLAMATION | #MB_SETFOREGROUND)
  End
EndIf


; ---------- Main Event Loop ----------------------
Repeat

  Select WaitWindowEvent()
    
    Case #PB_EventSysTray
      If EventMouseButton() = 2
        GetCursorPos_(Cursor)
        xPos = Cursor\x
        yPos = Cursor\y
        SetForegroundWindow_(hwn)  ; this line is needed for when you de-focus from the systray popup
        DisplayPopupMenu(0, hwn, xPos, yPos)
      EndIf

      etc. etc.
You'll need to adapt it for your own Window, Systray and Menu IDs.

Don
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.
SetForegroundWindow_(hwn) ; this line is needed for when you de-focus from the systray popup
That's what I was looking for ages ! I can't figure out why the PopupMenu would de-focus somethings.. Try with your line and it work well. I will fix the library. Thanks !

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Hope I've understood what you're after .....

Unfortunately not (sorry!). I need to do it with the ChangeSysTrayIcon command,
and not the AddSysTrayIcon command... because my app already uses a different
icon in the SysTray when it starts.


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

@PB: did you try...

ChangeSysTrayIcon(0, ExtractIcon_(GetModuleHandle_(#NULL),"yourexe.exe",0))

cya,
...Danilo


(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> PB: did you try...
> ChangeSysTrayIcon(0, ExtractIcon_(GetModuleHandle_(#NULL),"yourexe.exe",0))

Sure did, but the compiler reports Error: 'ImageID' is null for it...

It seems I need to load it as an image somehow, and there must be a way,
but I can't see it...

PB - Registered PureBasic Coder

Edited by - PB on 30 January 2002 10:07:37
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

If the debugger reports a NULL image, then the icon can't be extracted from your exec. Try to use the above code in the final compiler exec (and give the right exe name). I've used this trick and it worked well.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
If the debugger reports a NULL image, then the icon can't be extracted from your exec. Try to use the above code in the final compiler exec (and give the right exe name). I've used this trick and it worked well.
Still didn't work when compiled as an exe, in fact I got an Illegal Operation
error as a result...


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.
SetForegroundWindow_(hwn) ; this line is needed for when you de-focus from the systray popup
That's what I was looking for ages ! I can't figure out why the PopupMenu would de-focus somethings.. Try with your line and it work well. I will fix the library. Thanks !

Fred - AlphaSND
Well Fred, you haven't ever looked to my templates, have you? (on Paul's site)
There you will find a windowfunctions.zip including FrontWindow.pb...

It's there for ages


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.



Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Don.

Mmmm. This code worked OK for me:

Code: Select all

; Open the main window and make it invisible
;
hwn = OpenWindow(0,0,0,0,0,#WS_VISIBLE,"YourAppName")
If hwn
  AddSysTrayIcon(0, hwn, LoadIcon_(0, #IDI_EXCLAMATION))
  SysTrayIconToolTip(0,"yourappname")
Else
  MessageRequester("Yourapp","Cannot open window",#MB_ICONEXCLAMATION | #MB_SETFOREGROUND)
  End
EndIf
 
Delay(1000)
ChangeSysTrayIcon(0, ExtractIcon_(GetModuleHandle_(#NULL),"yourapp.exe",0))
 
; ---------- Main Event Loop ----------------------
     
      etc. etc.
Make sure you put the above code into your app AFTER you've got the icon resource into your executable. ie. you must compile your exe first to get the resource into it so that it can be extracted later, then add the above code & recompile.

Don
Post Reply