Begin Programming with Applications and Classes

 


1. How do I automatically display a window when a program starts?

The most important point to remember is that the function or window called Main always runs first when the program begins. Therefore a function called Main is run first, or a window called Main is always shown first (a program cannot have both). So to automatically display a window when the application starts, just make sure the window is called Main.

This happens automatically whenever you use the Developer Environment to create a new program. By choosing the program type 'Main Window', the Developer Environment will automatically generate the main window for you. Here is the code it generates:

  // App1.rc
  Main DIALOG 0,0,240,160
  STYLE DS_MODALFRAME|WS_POPUP|WS_CAPTION|WS_SYSMENU|DS_CENTER
  CAPTION "Main"
  BEGIN
  END

  // App1.cls
  Ubercode 1 Class App1

  public callback function main(in  EventId:integer
                                    ControlObj:control
                                    Key:integer
                                out Cancel:boolean)
  code
    select EventId
    case LOAD_EVENT =>
         // Dialog/Editwindow object was loaded
    case COMMAND_EVENT =>
         // Pushbutton/Bitmapbutton/Menuitem/Radiobutton/Checkbox selected
    case CHANGE_EVENT =>
         // Edit/Scrollbar/Listbox/Combobox/Iconlist was changed
    case UNLOAD_EVENT =>
         // Dialog/Editwindow was closed
    end select
  end function

  end class

The "app1.rc" file contains the layout of the main window, and the "app1.cls" file contains the code for the main window.

It is also possible to display a window when the application starts running, even if the window is not called Main. To make this work, create the window you want to show at start-up, make sure Main is declared as a proper function, and in the code of Main call Show(MyWindow) where MyWindow is the start-up window you created.

2. How do I automatically run code when a program starts?

The code in function main is always run when the program starts. Main is either a normal function like this:

  public function main()
  code
    ...
  end function

or it is a window function declared like this:

  public callback function main(in  EventId:integer
                                    ControlObj:control
                                    Key:integer
                                out Cancel:boolean)
  code
    select EventId
    case Load_event =>
         ...
    end select
  end function

If main is a normal function it runs without showing a window, and if main is a window function, the window appears when the program starts. To write code that runs when main starts, put the code where the dots are (...) above. In the normal function this is the first instruction after the code keyword, and in the window function this is the first instruction in the Load_event.

3. How do I change the icon used by an application?

If you use the Developer Environment to create a new program, this lets you choose the icon and generates the code to automatically display it. Alternately use the Appicon method to set the application icon to the desired icon file, by calling Appicon under the Load event of your main form. There are over one hundred icon files (*.ICO) for you to choose from, which are included in all copies of Ubercode. The application icon is shown whenever a window is minimized, or when using Alt+Tab to switch between applications.

4. How do I prevent an application being closed?

To do this, trap the Unload event of the main window.

If you want to fully prevent the user from closing the application, just return Cancel <- False under the Unload event. This cancels the Unload event and the main window will not be closed. If you do this you will need some other way of ending the application.

You can also give the user the choice of whether to close the application. To do this, use the Unload event to pop-up a Msgbox asking the user to confirm whether they want to quit. If they don't want to quit, set Cancel <- True to cancel the Unload event, but if they do want to quit do nothing and the unload event will run to completion and terminate the application.

5. I want to add my own functions to the run time library. Is this possible?

Yes. The simplest way of doing this is to edit the System class which is automatically included by every class. If you add a public function to the System class, it will be available to all other classes. The disadvantage is if you introduce errors into System, you will not be able to compile other classes until System compiles properly again. Also if you reinstall the software, your changes will be overwritten by the new installation. The best method is to write your own class and store it in the run time library folder (the 'libs' subfolder), and to include it in the USES clause of your classes. Any other class may use your library class. When doing this, it is still important to remember to save a copy of your class, so you can restore it if the software is reinstalled.