Creating a Window |
Top Previous Next |
Opening a window in Emergence BASIC is easily done with the OPENWINDOW function. When you use EBASIC to write Windows based software your program becomes event driven. Every action a user takes when running your program is sent to a special subroutine as a message. Your program must then decide whether or not to respond to this message. A message is really just a number. This number represents an action taken by the user. The syntax of the OPENWINDOW function is: OPENWINDOW variable, left, top, width, height, flags, parent, title, handler Parameters: variable - The name of the WINDOW variable used to store the window left - The left edge of the window top - The top edge of the window width - The width of the window height - The height of the window flags - A numeric value specifying creation style flags parent - a WINDOW variable if this is a child window or NULL title - The text shown in the caption of the window handler - The address of a subroutine to handle messages for the window. Use the & operator. The OPENWINDOW command will return 1 if the window was created successfully or 0 if the window could not be created. You can also check the window variable to see if it equals 0. If so then the window could not be opened. Example: REM define a window variable The example shows the steps necessary to open a window and wait for a message. Windows will call your handler subroutine anytime there is a message for the window you created. System variables and constants The previous example also introduces the ‘@’ symbol. Emergence BASIC defines a number of constants for use in your program. Special variables contain information about the message sent to a window. In either case the ‘@’ symbol is used to differentiate these names from variables you define in your program. System variables like @MESSAGE are set by EBASIC and cannot be used as normal variables. You can define your own constants with the SETID statement. The appendix contains a list of all of the system variables and their meanings. Creation style flags In the above example we used @SIZE in the flags parameter. This tells EBASIC that the window should be resizable. More than one flag can be used and combined with the '|' symbol (meaning OR). EBASIC contains many predefined flags:
Waiting for messages In the previous example, we introduced a few new ideas. The WAITUNTIL command tells EBASIC to process messages until a condition is true. The WAITUNTIL command sleeps until something is done with one of the windows your program is using. When a message is sent to your window, the handler subroutine is called that was defined when the window was opened. After your subroutine executes a RETURN statement, your program will continue waiting for messages. In our example, we check the value of the variable ‘w1’ to see of we should wait for more messages or end the program. The syntax of WAITUNTIL is: WAITUNTIL condition Emergence BASIC also contains another statement for processing messages. The WAIT statement can be used when more control over message processing is desired. WAIT processes any messages that are available for your window, sleeps if none are available, and then returns. The syntax of WAIT is: WAIT {NoSleep} If the optional NoSleep parameter is set to 1 then the WAIT command will check for messages and return immediately. WAIT will be covered in more detail in the Messages and Message loops section Handling Messages When your windows handler subroutine is called, the system variable @MESSAGE will contain the message ID. This variable can then be compared against any of the messages you wish to handle using an IF or SELECT statement. Some messages contain additional information and set @WPARAM and @LPARAM accordingly. A good example of this is information from the keyboard. When a key is pressed your window will receive, among others, the message @IDCHAR. In this case @WPARAM will contain the ASCII value of the key. For backwards compatibility with other languages you can use @CLASS, @CODE and @QUAL in place of @MESSAGE, @WPARAM and @LPARAM respectively. Example code fragment: SUB mywin When developing your handler subroutine you can handle as many or as few messages as you need. Any messages that you do not respond to are simply 'thrown away' by the system. At a minimum you should check for @IDCLOSEWINDOW to handle the close button of the window. Returning Values If a particular message requires a return value you can specify it in the RETURN statement. The next section will explore messages and the message loops in more detail. See Also: System Variables and Constants in the appendix for a list of message IDs |