First: Visual Basic

Visual Basic programs consist of a project (vbp) file and one or more source code form (frm) files. The project file defines the forms and modules used in the program. All Fastgraph Visual Basic projects must include either Fastgraph's native module (FGWin.bas) or DirectX module (FGWinD.bas). Form files include the program's functions, sub procedures, and event handlers. Visual Basic automatically takes care of many of the traditional Windows programming details such as setting up the window class, the window procedure, and the message loop.

Here is the Visual Basic form file for our first example program:

'*****************************************************************************
'                                                                            *
'  First.frm                                                                 *
'                                                                            *
'  This is the first Fastgraph for Windows example program. It demonstrates  *
'  tasks common to most Fastgraph for Windows programs and serves as a       *
'  template for building the other examples.                                 *
'                                                                            *
'*****************************************************************************
Dim hPal As Long
Dim hVB As Long
Dim cxClient As Long, cyClient As Long
Private Sub Form_Activate()
   Call fg_realize(hPal)
   Refresh
End Sub
Private Sub Form_Load()
   ScaleMode = 3
   Call fg_setdc(hDC)
   hPal = fg_defpal()
   Call fg_realize(hPal)
   Call fg_vbinit
   hVB = fg_vballoc(640, 480)
   Call fg_vbopen(hVB)
   Call fg_vbcolors
   Call fg_setcolor(19)
   Call fg_fillpage
End Sub
Private Sub Form_Paint()
   Call fg_vbscale(0, fg_getmaxx(), 0, fg_getmaxy(), 0, cxClient - 1, 0, cyClient - 1)
End Sub
Private Sub Form_Resize()
   cxClient = ScaleWidth
   cyClient = ScaleHeight
   Refresh
End Sub
Private Sub Form_Unload(Cancel As Integer)
   Call fg_vbclose
   Call fg_vbfree(hVB)
   Call fg_vbfin
End Sub

The form for our first example simply consists of event-handling procedures that provide our first look at some Fastgraph functions. Note that our program does not explicitly call these procedures. Instead, they are called by Visual Basic in response to Windows events such as creating or resizing the window (the event-handling procedures are analogous to the traditional WindowProc() message handlers). Our program includes event handlers for the form's activate, load, paint, resize, and destroy events.

A load event occurs when the form is first created during program execution. This event occurs once per program instance and is analogous to the WM_CREATE Windows message. Hence, it is a good place for any application-specific initialization code. Our load event procedure is in the Form_Load() Sub procedure:

Private Sub Form_Load()
   ScaleMode = 3
   Call fg_setdc(hDC)
   hPal = fg_defpal()
   Call fg_realize(hPal)
   Call fg_vbinit
   hVB = fg_vballoc(640,480)
   Call fg_vbopen(hVB)
   Call fg_vbcolors
   Call fg_setcolor(19)
   Call fg_fillpage
End Sub

Form_Load() first sets the ScaleMode property to 3 (units=pixels), which Fastgraph needs to update the form's client area properly. It then calls fg_setdc() to make the form's device context (obtained with the hDC property) available to other Fastgraph functions. Next, Form_Load() creates the default logical palette with fg_defpal() and realizes the palette with fg_realize(). It then initializes Fastgraph's virtual buffer environment, creates a 640x480 virtual buffer and makes it the active virtual buffer, and assigns the logical palette colors to the virtual buffer. Finally, Form_Load() fills the virtual buffer with blue pixels (color 19 is blue when using Fastgraph's default 256-color virtual buffers with the default logical palette).

An activate event occurs when the form gains the input focus. This happens when control shifts from another Windows application to our program, so it is analogous to the WM_SETFOCUS Windows message. Our activate event procedure is in the Form_Activate() Sub procedure:

Private Sub Form_Activate()
   Call fg_realize(hPal)
   Refresh
End Sub

Form_Activate() first calls fg_realize() to activate the program's logical palette (in case another program has changed the logical palette colors). It then uses the Refresh method to force a paint event to redraw the form's client area. This sequence is typical of activate event procedures in Fastgraph programs.

A paint event occurs when the form's client area must be repainted; it is analogous to the WM_PAINT Windows message. Our paint event procedure is in the Form_Paint() Sub procedure:

Private Sub Form_Paint()
   Call fg_vbscale(0, fg_getmaxx(), 0, fg_getmaxy(),
                   0, cxClient - 1, 0, cyClient - 1)
End Sub

Form_Paint() just calls fg_vbscale() to display the 640x480 virtual buffer scaled to the size of the form's client area. This sequence is typical of paint event procedures in Fastgraph programs.

A resize event occurs whenever the size of the form changes, and also upon creation of a form. It is analogous to the WM_SIZE Windows message. Our resize event procedure is in the Form_Resize() Sub procedure:

Private Sub Form_Resize()
   cxClient = ScaleWidth
   cyClient = ScaleHeight
   Refresh
End Sub

Form_Resize() first saves the new width and height of the client area (in pixels) in the variables cxClient and cyClient. These quantities are passed to fg_vbscale() in Form_Paint(). It then uses the Refresh method to generate a paint event that updates the resized form.

An unload event occurs after removing a form and often signals a program exit; it is analogous to the WM_DESTROY Windows message. Our unload event procedure is in the Form_Unload() Sub procedure:

Private Sub Form_Unload(Cancel As Integer)
   Call fg_vbclose
   Call fg_vbfree(hVB)
   Call fg_vbfin
End Sub

Form_Unload() calls fg_vbclose() to close the virtual buffer, fg_vbfree() to release its memory, and fg_vbfin() to terminate virtual buffer processing.

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.