VISUAL BASIC_6
 

6. Menu-Driven and Calculator Projects


Adding Control Arrays:

Next we have to add the three line-width menu items.  The first one will have the Caption &1 Pixel and a control name ‘miPixel’   When different controls have the same array name they must be identified by indexes.   So after entering the control name, move the insertion point to the index field and enter 1 for the first  Pixel Control.   For the subsequent Pixel Controls enter the index values as 2 and 3 respectively.

Inserting Lines in Menus

After highlighting  the item next to the place where you want to insert a line, select insert button.   Type a hyphen(-) symbol for the Caption name and miLine1 for the Control name.   For subsequent lines give the control names as muLine2, miLine3 etc.   Finally the design window will appear as shown in the figure 3.   Insertion or deletion of lines and menu titles and menu items can be done with the help of insert, delete and OK buttons.


                             Figure 3 

Entering Codes for the Controls and Pop-up Menu:

When you select any control, the code window for the particular control will be opened.   Enter the codes for the different controls as shown in the figure 3a.   A pop-up menu is the same as the other menus, except that it does not drop down from the menu bar, but displayed on the form at the place where you right-click the mouse.   The code for creating the pop-up menu is also shown in the figure 3a.                                

Private numPoints As Integer     ' Number of points saved

Private saveX(1000) As Single    ' Saved X coordinates

Private saveY(1000) As Single    ' Saved Y coordinates

Private numLines As Integer        ' Number of separate lines

Private lineStart(500) As Integer  ' Start of each line

Private lineEnd(500) As Integer    ' End of each line

Private lineThickness(500) As Integer  ' DrawWidth for

                                       ' each line

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

  PSet (X, Y)                  ' Draw first point in line

  numPoints = numPoints + 1    ' The next free point

  saveX(numPoints) = X         ' Remember this point

  saveY(numPoints) = Y

  numLines = numLines + 1         ' Next free line

  lineStart(numLines) = numPoints ' Index of the first point

  lineEnd(numLines) = numPoints   ' Initially a single point

 End Sub

lineThickness(numLines) = DrawWidth

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

  If Button = 1 Then           ' Is the left button down?

    Line -(X, Y)               ' Yes, draw a line

    numPoints = numPoints + 1   ' Next free point in array

    saveX(numPoints) = X       ' Remember this point

    saveY(numPoints) = Y

   lineEnd(numLines) = numPoints  ' New ending index for line

  End If

End Sub

 

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

  If Button = 2 Then          'Right mouse button clicked?

    PopupMenu menDraw         'Yes. Display menDraw as popup.

    End If

End Sub

 

Private Sub Form_Paint()

  For Lin = 1 To numLines      ' Draw each line

    aStart = lineStart(Lin)    ' Start index of this line

    anEnd = lineEnd(Lin)       ' End index of this line

    DrawWidth = lineThickness(Lin)

    PSet (saveX(aStart), saveY(aStart))

    For i = aStart To anEnd     ' Draw parts of this line

      Line -(saveX(i), saveY(i))

    Next i

  Next Lin

End Sub

 

Private Sub miErase_Click()

    Cls

  numPoints = 0          ' Set to no points

  numLines = 0           ' Set to no lines

End Sub

 

Private Sub miexit_Click()

End

End Sub

 

Private Sub miPixel_Click(Index As Integer)

  miPixel(DrawWidth).Checked = False  ' Uncheck current width

  DrawWidth = Index                   ' Change width of the pen

  miPixel(Index).Checked = True       ' Check the new width

End Sub

 

Private Sub miPrint_Click()

  For Lin = 1 To numLines      ' Draw each line

    aStart = lineStart(Lin)    ' Start index of this line

    anEnd = lineEnd(Lin)       ' End index of this line

    Printer.DrawWidth = lineThickness(Lin)

    Printer.PSet (saveX(aStart), saveY(aStart))

    For i = aStart To anEnd     ' Draw parts of this line

      Printer.Line -(saveX(i), saveY(i))

    Next i

  Next Lin

  Printer.EndDoc

End Sub

     Figure 3a


 

Copyright © 2001 Selfonline-Education. All rights reserved.