[Gambas-user] Gambas in background

Caveat Gambas at ...1950...
Wed Apr 21 16:21:57 CEST 2010


Thanks for the tips Benoit.  I never realised I could avoid all those
ugly Attaches. :-)

But that comment... Do not use QUIT in GUI programs. Just close your
form.

I was going to argue about this, as the point is that we are creating an
application that runs in background and kind of "officially has no
forms".  But then I tested it and found, of course, you're absolutely
right... the second I close MyDummyForm the app closes (even though we
never Load or Show that form).

It's always nice to help someone learn something new, and in the process
learn something new yourself. :-)

Regards,
Caveat

On Wed, 2010-04-21 at 16:07 +0200, Benoît Minisini wrote:
> > 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.  
> 
> No need to use Object.Attach(), as there is a syntax for that. Look at the 
> documentation of the NEW instruction.
> 
> > 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??) 
> 
> You need a window to create a menu, that's true.
> 
> > 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
> >
> > [...]
> >
> > 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
> 
> -----------------------------> AS "MyTrayIcon"
> 
> >   MyTrayIcon.Tooltip = "Hi from Gambas"
> >   Object.Attach(MyTrayIcon, ME, "MyTrayIcon")
> 
> ...and you can remove the previous line.
> 
> You can do that for all instanciations below.
> 
> >   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
> 
> Do not use QUIT in GUI programs. Just close your form.
> 
> > 
> > END
> > 
> 
> Regards,
> 






More information about the User mailing list