[Gambas-user] Gambas in background

Caveat Gambas at ...1950...
Wed Apr 21 15:31:56 CEST 2010


Hi Bill,

Yes, it is.

I have developed a little test app for you that shows this in action.

Module1 is set as the Startup Class, in place of the more normal Form.
As it's the Startup Class, main() gets run automatically when you start
the program.  First thing it does is instantiate the TrayIcon and give
it a tool tip so you know it's working.
Very important is the following Object.Attach line as this ensures your
module captures the events raised by the TrayIcon.  This is normally not
necessary when you stick a control on a Form, as the Attach is
automatic.  Then we make sure the TrayIcon gets Shown.

After this, we instantiate a dummy form (I couldn't see any way around
this... Benoit??) and then start to define our menus.  The top-level
menu has the dummy form set as its parent (also important that the dummy
form has been instantiated (using NEW Form)...but it doesn't need to be
Shown or Loaded).  The subsequent menus (MySubMenu 1,2,3) get the
top-level menu as a parent.
Very important again is to Attach all the sub menus to your module (ME
meaning literally me/myself... so when you write that code in Module1,
it means Module1).

When the Menu event from the TrayIcon gets fired, we call Popup on our
home-made menu MyMenu.  This pops up the 3 sub menu items in a little
box near the TrayIcon... Status, Show Main, Exit.

As each sub-menu has been Attached to Module1, when one of the sub-menus
gets a Click event, it can be trapped by Module1 in MyMenu1_Click(),
MyMenu2_Click()...

Note that the SUB to catch the event must be named according to the last
param of the Attach method... so MyMenu1, MyMenu2, MyMenu3 in this case
(you'll probably make much better names than this...)

My FMain is the same FMain as I used for Rolf's question.  There is
nothing special about my FMain, there are no secret menu widgets or
TrayIcons defined in FMain.  Including "Show Main" on MyMenu2 just
demonstrates that you *could* have a program running in background where
you might occasionally want to open up a form and see how it's
doing/respond to problems... 

Hope this helps, and above all... have fun! :-)

Regards,
Caveat

P.S.  The full code for Module1 is as follows:
' Gambas module file
PRIVATE MyTrayIcon AS TrayIcon
PRIVATE MyMenu AS Menu
PRIVATE MySubMenu1 AS Menu
PRIVATE MySubMenu2 AS Menu
PRIVATE MySubMenu3 AS Menu
PRIVATE MyDummyForm AS Form

PUBLIC SUB main()
  
  MyTrayIcon = NEW TrayIcon
  MyTrayIcon.Tooltip = "Hi from Gambas"
  Object.Attach(MyTrayIcon, ME, "MyTrayIcon")
  MyTrayIcon.Show
  
  MyDummyForm = NEW Form
  
  MyMenu = NEW Menu(MyDummyForm, FALSE)
  MyMenu.Caption = "TrayMenu"
  MyMenu.Tag = "TrayMenu"
  MyMenu.Name = "TrayMenu"
  
  MySubMenu1 = NEW Menu(MyMenu, FALSE)
  MySubMenu1.Caption = "Status"
  MySubMenu1.Tag = "Status"
  MySubMenu1.Name = "mnuStatus"
  Object.Attach(MySubMenu1, ME, "MyMenu1")
    
  MySubMenu2 = NEW Menu(MyMenu, FALSE)
  MySubMenu2.Caption = "Show Main"
  MySubMenu2.Tag = "ShowMain"
  MySubMenu2.Name = "mnuShowMain"
  Object.Attach(MySubMenu2, ME, "MyMenu2")
  
  MySubMenu3 = NEW Menu(MyMenu, FALSE)
  MySubMenu3.Caption = "Exit Program"
  MySubMenu3.Tag = "Exit"
  MySubMenu3.Name = "mnuExit"
  Object.Attach(MySubMenu3, ME, "MyMenu3")
  
END

PUBLIC SUB MyTrayIcon_Menu()

    MyMenu.Popup()

END

PUBLIC SUB MyMenu1_Click()
  
  Message.Info("Your program is: RUNNING")
  
END

PUBLIC SUB MyMenu2_Click()
  
  FMain.Show
  
END

PUBLIC SUB MyMenu3_Click()
  
  QUIT
  
END

On Wed, 2010-04-21 at 02:20 -0700, Bill-Lancaster wrote:
> Is it possible to run a Gambas programme with only a trayicon showing?
> 
> Bill Lancaster






More information about the User mailing list