FrameDD: Visual Basic Version

In Visual Basic programs, adding a WM_ACTIVATEAPP event handler is a rather complex task. We must replace the default Window procedure with our own Window procedure that traps WM_ACTIVATEAPP messages, but passes all other messages to the default Window procedure. The ActivateApp.bas module in Fastgraph's Visual Basic examples directory provides code to do this, so most of the work is taken care of by adding the ActivateApp.bas module to your application's Visual Basic project.

We must still enable our own Window procedure by calling the ActivateApp module's HookWindowProc() function in the program's Form_Load() function. Once this is done, the Window procedure defined in the ActivateApp module will call our program's ActivateApp() function when it receives a WM_ACTIVATEAPP message. The ActivateApp() function serves as our WM_ACTIVATEAPP handler and first updates the AppIsActive global. If FrameDD has just become active, we maximize the form and restore the DirectDraw surfaces with fg_ddrestore(). If control is instead being switched to another application, we minimize the form, and if FrameDD is compiled for page flipping, flip to the GDI drawing surface with fg_gdiflip() as shown here:

Public Sub ActivateApp(ByVal wParam As Long)
   AppIsActive = (wParam <> 0)
   If AppIsActive Then
      WindowState = vbMaximized
      Call fg_ddrestore
   Else
      WindowState = vbMinimized
      #If Flip Then
      Call fg_gdiflip
      #End If
   End If
End Sub

We must be sure to call the ActivateApp module's UnHookWindowProc() function in the program's Form_Unload() function. This disables our own Window procedure and directs all messages back to the default Window procedure. Finally, note how the FrameDD message loop in Form_Load() calls Animate() only when AppIsActive is True.


'*****************************************************************************
'                                                                            *
'  FrameDD.frm                                                               *
'                                                                            *
'  This program shows how to set up a full screen DirectDraw application     *
'  for either blitting or flipping. The selection of blitting or flipping is *
'  controlled by the BLIT and FLIP symbols defined below.                    *
'                                                                            *
'  Requires Visual Basic 5.0 or later.                                       *
'                                                                            *
'*****************************************************************************
Const vbWidth = 640
Const vbHeight = 480
' define either Blit or Flip, but not both, for blitting or flipping
#Const Blit = True
#Const Flip = False
Dim hPal As Long
Dim hVB As Long
Dim AppIsActive As Boolean
Dim AppIsRunning As Boolean
Public Sub ActivateApp(ByVal wParam As Long)
   AppIsActive = (wParam <> 0)
   If AppIsActive Then
      WindowState = vbMaximized
      Call fg_ddrestore
   Else
      WindowState = vbMinimized
      #If Flip Then
      Call fg_gdiflip
      #End If
   End If
End Sub
Private Sub Form_Activate()
   Call fg_realize(hPal)
End Sub
Private Sub Form_Load()
   ScaleMode = 3
   #If Blit Then
   Call fg_ddsetup(vbWidth, vbHeight, 8, FG_DX_BLIT)
   #Else
   Call fg_ddsetup(vbWidth, vbHeight, 8, FG_DX_FLIP)
   #End If
   Call fg_setdc(hDC)
   hPal = fg_defpal()
   Call fg_realize(hPal)
   Visible = True
   ' if blitting, create a virtual buffer the same size as the screen
   ' resolution; if flipping, use the primary surface's back buffer
   Call fg_vbinit
   #If Blit Then
   hVB = fg_vballoc(vbWidth, vbHeight)
   #Else
   hVB = 0
   #End If
   Call fg_vbopen(hVB)
   Call fg_vbcolors
   Call fg_mouseini
   Call fg_mousevis(0)
   AppIsActive = True
   AppIsRunning = True
   Call HookWindowProc(hWnd)
   While AppIsRunning
      DoEvents
      If AppIsActive Then Call Animate
   Wend
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyEscape Or KeyCode = vbKeyF12 Then Unload Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
   AppIsRunning = False
   AppIsActive = False
   Call UnHookWindowProc(hWnd)
   Call fg_mousevis(1)
   Call fg_vbclose
   #If Blit Then
   Call fg_vbfree(hVB)
   #End If
   Call fg_vbfin
End Sub
'****************************************************************************\
'                                                                            *
'  Animate()                                                                 *
'                                                                            *
'  Construct the next frame of animation and display it with either blitting *
'  or flipping, as directed by the BLIT and FLIP symbols above.              *
'                                                                            *
'****************************************************************************/
Private Sub Animate()
   ' fill drawing surface with the next color
   Call fg_setcolor((fg_getcolor() + 1) And &HFF)
   Call fg_fillpage
   ' blit or flip surface to the screen
   #If Blit Then
   Call fg_vbpaste(0, vbWidth - 1, 0, vbHeight - 1, 0, vbHeight - 1)
   #Else
   Call fg_ddflip
   #End If
End Sub

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.