Alyce
Jun 18, 2007
- "Edited for spelling, grammar and clarity."
Running a LB program from the System Tray.
Author : Duncan Bushaw
Published June 17, 2007.
Introduction.
The example
For the purpose of this article a windowless control is any type of control that does not necessarily have to be assigned to a active window (ie; Liberty BASIC controls such as NOTICE or POPUPMENU;
Any standard approach to
The most common errors I see and hear about are the 'Invalid branch label', 'stack overflow', and 'popup menu already active' errors.
The errors have to do with the CALLBACK function TrayIcon() [This is the CALLBACK function that is
The program uses many Liberty BASIC commands and a few API controls, and it shows one technique that seems to encompass most controls and
The technique used in this program is not the only way of writing a program in Liberty BASIC which can run on the system
To do this I had to 'Think Outside Of The Box' when using function calls
Think of it this
If the 'Test Window' is active the program will call the Testwindow() function and then only refer to the code in this function.
The same idea happens
When the program closes the 'Test window' and the TrayIcon() CALLBACK function is
To
IFINSTR(CommandLine$,"-TestWindow")>0INSTR(CommandLine$,"-TestWindow")>0 then OP=1:null=TestWindow()
The above line of code is why the TKN file and the runtime engine must be in the same folder/directory as the BAS
If the CommandLine$ contains the string
All branch labels for the window are contained within the function code itself and when the close button on the title bar is clicked or (ALT-F4) is pressed the program looks for the branch lable [quit.WindowIcon] within the TestWindow() function and turns off the TIMER, closes the window, sets the TrayIconPointer to the value of Zero, and then checks the (OP) variable. If it is 0 then the program does not exit the function. Instead it waits and exits
TrayIcon.bas the program.
The Liberty BASIC runtime engine (run403.exe) copied and renamed as TrayIcon.exe and the TKN (tokenized BAS file) must be in the same dir./folder as TrayIcon.bas for all the program features to work without errors. A full working package can be downloaded from my domain
The program
Double clicking the left mouse button calls
The left button menu has 4
The right button menu has 4
The 'About'
The 'NOTICE'
The 'Help'
The 'Exit'
The 'Test window on Restart' starts another instance of the program, which will show the Test window before putting the icon on the system tray, and then ends the first instance of the program.
The 'Test window' will open the Test window which is of a window_nf type, and the tray icon will have a different popup menu available with the right mouse button. The left mouse button is disabled while the Test window is open.
The Test window.
Is of a window_nf type.
I have included many native Liberty BASIC controls in the Test
Selecting a CHECKBOX, COMBOBOX, LISTBOX, or RADIOBUTTON item causes a NOTICE window
The GRAPHICBOX is used to display a clock which I ported from 'clasic Face clock.bas' (see this article of mine http://lbpe.wikispaces.com/A+True+API+Popup+Menu+-+By+Duncan+B ) which uses the TIMER control to update the clock every second.
The 'Retrieve Text' button will retrieve anything in the TEXTBOX and display it in a NOTICE window.
The 'Retrieve All Items' button will cause a POPUPMENU to appear and the user can select which window the dialog_nf_modal window will be attached to (the hidden window or the Test Window). After selecting the window to attach the dialog_nf_modal window to the program will then open the dlalog_nf_modal window with only a TEXTEDITOR control in the window (neat no menu bar in this window) and then retrieve the current state of all CHECKBOXES and RADIOBUTTONS (set/reset), the current selected LISTBOX and COMBOBOX items (if any), the contents of the COMBOBOX, TEXTBOX, and TEXTEDITOR (if any), and then display them in the TEXTEDITOR in the dialog_nf_modal window.
The 'Retrieved All Items' window.
Is of a dialog_nf_modal type.
When this window is opened the tray icon will have a different popupmenu again (right click only) and the clock in the GRAPHICBOX in the 'Test Window' will continue to be updated every second.
If this window is attached to the 'Test Window' then the 'Test Window' will be disabled but will not
If this window is attached to the hidden window then the 'Test Window' will not be disabled and will be set as the foreground window putting it on top of the dialog_nf_modal window. Moving the mouse over (do not click) the tray icon the 'Retrieved All Items' window will be set as the foreground window putting it on top of the 'Test Window'. I did this to show that the CALLBACK function will be tripped just by moving the mouse over the tray icon. The 'Test Window' will remain enabled and all the controls will still work.
The TrayIconpointer.
This variable works best if it is set before any windows or windowless controls have been opened or are made active .
The TrayIconpointer variable has 4 states:
- [-1] - This value tells the program that a windowless contol is active and to exit the Traylcon() CALLBACK function right away.
- [0] - This value tells the program that there are no windowless controls or windows active and causes the program to wait inside the Traylcon() CALLBACK function.
- [1] - This value tells the program that the Test window is active and causes the program to jump to the Testwindow() function and to wait inside the function.
- [2] - This value tells the program that the dialog window is active and causes the program to jump to the Retrieve() function and to wait inside the function.
I hope that this helps those frustrated LB programmers who have tinkered with the WMLiberty.dll, I spent many hours trying different codes and this solution seems so obvious to me now. Enjoy!