Tray Icon broken?
Posted
#1
(In Topic #1580)
Regular

(gb.gui.trayIcon)
i tried to make a right click tray icon, and there is no code for it.
I can get the left click / middle mouse button, working just fine. but right doesn't seem to work / exist, every time i try, it just crashes outright.
is that intentional? or? bug?
Posted
Guru

Posted
Regular

sorry for not being clear about it.
i was trying to create a pop up menu to my app, using the right mouse button.
with the trayicon extention.
here is a small snippet.
(this works for left and middle mouse button, when i change it to the right mouse button, it just straight up crashes)
Code
Private $hPopup As Menu
Public Sub TrayIcon1_Click()
' Me.show
$hPopup = New Menu(Me, True)
Dim hMenu As Menu
' we only want right clicks
$hPopup.Children.Clear ' clear main popup menu parent items
' i just make some menus here. i guess for your needs the creation would be more dynamic/varied.
hMenu = New Menu($hPopup) As "POP" ' make a menu item in $hPopup and use event name POP
hMenu.Name = "M1"
hMenu.Text = "Show" ' make text
hMenu = New Menu($hPopup) As "POP" ' make another menu item in $hPopup and again use event name POP
hMenu.Name = "M2"
hMenu.Text = "Stop" ' make text
hMenu = New Menu($hPopup) ' make a menu item in $hPopup with no event as it's a spacer
' do not enter any text so it becomes a spacer
hMenu = New Menu($hPopup) As "POP" ' make another menu item in $hPopup and again use event name POP
hMenu.Name = "M3"
hMenu.Text = "Close" ' make text
$hPopup.Popup ' show the menu at mouse position
End
Public Sub POP_Click() ' all menus share event POP so in the event we detect the menu name to distinguish between them
Select Last.Name
Case "M1"
If Me.Visible = False Then Me.Visible = True
Case "M2"
btn_stop_Click
Case "M3"
TrayIcon1.Hide
TrayIcon1.Delete
Me.Close
End Select
End
Posted
Guru

I mean use the TrayIcon.PopupMenu property.
/comp/gb.gui.trayicon/trayicon/popupmenu - Gambas Documentation
Posted
Guru

Code (gambas)
- $hPopup.Name = "POPUP" ' Set the Name so it can be found.
- TrayIcon1.PopupMenu = "POPUP" ' this adds the menu named "POPUP" to the trayicon.
- ' Note, because it is the trayicon PopupMenu property the right click is automatically detected and does not require any code.
- ' Dynamically create the children for $hPopup before it shows...
- $hPopup.Children.Clear ' clear main popup menu parent items
- ' i just make some menus here. i guess for your needs the creation would be more dynamic/varied.
- hMenu.Name = "M1"
- hMenu.Text = "Show" ' make text
- hMenu.Name = "M2"
- hMenu.Text = "Stop" ' make text
- ' do not enter any text so it becomes a spacer
- hMenu.Name = "M3"
- hMenu.Text = "Close" ' make text
- ' $hPopup.Popup ' Do not need this as we are in the Menu_Show event.
- Case "M1"
- Case "M2"
- btn_stop_Click
- Case "M3"
- TrayIcon1.Hide
- TrayIcon1.Delete
PS.
You could also just have used the IDE if you do not need a "dynamic" menu…
1 Make a hidden menu using the IDE Menu editor.
2 Drag a TrayIcon object to the form.
3 Select the TrayIcon and use the properties editor to set the PopupMenu property.
Posted
Regular

b
Posted
Guru

thatbruce said
I really like your code there Bruce. So often we can forget the control naming trick!
b
Why thank you kind sir
I think more a necessity than a trick in this case though.
a statement like TrayIcon1.PopupMenu = "POPUP" makes the interpreter search the application menus for one specifically named POPUP and won't find it if not named.
Posted
Regular

thanks for showing me how to do it.
appreciate it
but i prefer to write it then going in the properties tab and such
because gambas3 has a clear button, right next to the blank space, and you wouldn't believe how many times, i hit the x, and wiped out all the data i put in.
so i kind of avoid it like the pest
Posted
Guru

JMathers said
ah, nice, so i was close,
thanks for showing me how to do it.
appreciate it![]()
but i prefer to write it then going in the properties tab and such
because gambas3 has a clear button, right next to the blank space, and you wouldn't believe how many times, i hit the x, and wiped out all the data i put in.
so i kind of avoid it like the pest
No worries.
If the menu is fixed and does not need to be dynamic you could always create it at program start..
Like this…
Code (gambas)
- $hPopup.Name = "POPUP" ' Set the Name so it can be found.
- ' create the children for $hPopup...
- ' i just make some menus here. i guess for your needs the creation would be more dynamic/varied.
- hMenu.Name = "M1"
- hMenu.Text = "Show" ' make text
- hMenu.Name = "M2"
- hMenu.Text = "Stop" ' make text
- ' do not enter any text so it becomes a spacer
- hMenu.Name = "M3"
- hMenu.Text = "Close" ' make text
- TrayIcon1.PopupMenu = "POPUP"
- Case "M1"
- Case "M2"
- btn_stop_Click
- Case "M3"
- TrayIcon1.Hide
- TrayIcon1.Delete
Posted
Regular

but i avoid adding things like that in the form_start section.
to avoid slowing down the app.
only absolute essentials for me go in the form_start
and the trayicon menu can wait until the app is loaded
but total side question for you.
can linux mint (22.01) do 2 wallpapers, 1 per screen, i see an option in the "Monitor Preferences" "same image in all monitors"
but i can't seem to find any documentation on how to assign 1 picture per monitor.
1 guest and 0 members have just viewed this.


