Extending the CFormAttributes Class

The beauty of a value-added form class is that it’s a simple matter to add new features retrospectively. As an example, let’s look at how you can add support for pseudo-MDI minimize and restore behavior. Because all document windows in an MDI application are contained within the client area of the parent window, minimizing that window naturally takes away all of the children too. This is convenient since it instantly clears the application off the desktop (without closing it, of course).

The MDI window feature in Visual Basic gives you this minimize behavior for free. With an SDI or a DIY-DI application, however, you have no such luxury. Because a Visual Basic form has no Minimize event, you must write code that plugs into the Resize event and decide for yourself when a form is minimized or restored by investigating the WindowState property. The behavior we’re going to construct will watch for transitions from normal to minimized and from minimized back to normal. (This second operation is usually called “restore.”) We’ll write the code as a new method of the CFormAttributes class and then simply add a call to it from appropriate Resize event handlers.

Trapping the event, of course, is only half the story—you also need to do something to take away the rest of the forms. One possibility is to set the WindowState to follow the window containing the trap, but in practice that looks messy because Windows animates zoom boxes all over the place and you end up with lots of task bar buttons (or icons for earlier versions of Microsoft Windows NT). It’s quicker and visually more effective to hide all the other forms when you trap a minimize event and to restore them when you trap a restore event. The only tricky part is to remember the prevailing state of each form before hiding it, just in case any were hidden already. Here’s the code you’ll need:

Public PreviouslyVisible As Boolean
Private nPiPrevWindowState As Integer

Public Sub PropagateMinMaxEvents ()
    If frmPiSelf.WindowState = vbMinimized _
            And nPiPrevWindowState = vbNormal Then
        Call HideAllForms
    ElseIf frmPiSelf.WindowState = vbNormal _
            And nPiPrevWindowState = vbMinimized Then
        Call UnhideAllForms
    End If
    nPiPrevWindowState = frmPiSelf.WindowState
End Sub

Private Sub HideAllForms()
    Dim frmForm As Form
    For Each frmForm In Forms
        If Not frmForm Is frmPiSelf Then
            frmForm.My.PreviouslyVisible = frmForm.Visible
            frmForm.Visible = False
        End If
    Next frmForm
End Sub

Private Sub UnhideAllForms()
    ' This is just the opposite of HideAllForms.
End Sub

To activate the new behavior, you need to choose which forms will trigger it and call PropagateMinMaxEvents from their Resize event handlers. The publication editing program referred to in Figure 14-2 on page 583 has this call coded in the Resize events of all the forms, so minimizing any form hides all the others and shows a single button on the task bar. Restoring from that button restores each form to its previous state. To add minimize behavior to the example application shown in Figure 14-1 on page 582, you would code a single call to PropagateMinMaxEvents in the Resize event of the main form (the one carrying the menu bar). This mimics the MDI paradigm more closely because of the definite parent form.

Visual Basic 5 has another trick that you could use here, which is to add custom Minimize and Restore events to your forms through the CFormAttributes class. You can do this very simply by making a small modification to the PropagateMinMaxEvents method on the following page.

Event Minimize()
Event Restore()

Public Sub PropagateMinMaxEvents ()
    If frmPiSelf.WindowState = vbMinimized _
            And nPiPrevWindowState = vbNormal Then
        RaiseEvent Minimize
    ElseIf frmPiSelf.WindowState = vbNormal _
            And nPiPrevWindowState = vbMinimized Then
        RaiseEvent Restore
    End If
    nPiPrevWindowState = frmPiSelf.WindowState
End Sub

In case you didn’t spot it, calls to HideAllForms and UnhideAllForms have been replaced with calls to a new Visual Basic procedure, RaiseEvent. This diminutive keyword is very powerful, and you’ll see other examples of it later. When you define the CFormAttributes instance on a form, a new object, My, appears in the code window’s Object drop-down list box, and when you choose it, you’ll see Minimize and Restore events in the Procedure drop-down list box. These events work in exactly the same way as normal events do, so selecting Minimize inserts an empty procedure named My_Minimize into the code. One caveat is that the syntax for defining the CFormAttributes instance is slightly different if you want to see the events:

Public WithEvents My As CFormAttributes

Unfortunately, the New keyword is not allowed in combination with the WithEvents keyword, so you’ll also need to add a line to the Form_Load event:

Private Sub Form_Load()
    Set My = New CFormAttributes
    My.LoadActions Me
End Sub