|
|
Title | This editor lets you build, save, and restore a collection of points |
Description | This example shows how to let the user define a collection of points, save them into a file, and restore them later in Visual Basic 6. If the user has modified the data, the program asks the user before closing or starting a new file of points. |
Keywords | point, load, edit, save |
Categories | Graphics |
|
|
This program lets you draw, save, and restore a collection of points. It keeps track of modifications and warns you if you are about to exit without saving changes.
[This program is really a precursor to some point geometry programs that do such things as find a minimal spanning tree, find the convex hull, etc.]
Subroutine SaveData writes the X and Y coordinates of the points in the m_Points array into a file. Subroutine LoadData reads those values back into the array.
Subroutine DrawPoints loops through the points drawing them.
|
|
Private m_Points() As POINTAPI
' Save data into the file.
Private Sub SaveData(ftitle As String, fname As String)
Dim fnum As Integer
Dim i As Integer
' Open the file.
fnum = FreeFile
Open fname For Output As fnum
' Save the number of points.
Write #fnum, m_NumPoints
' Save the points.
For i = 1 To m_NumPoints
With m_Points(i)
Write #fnum, .X, .Y
End With
Next i
' Close the file.
Close fnum
' Save the file name and title.
FileTitle = ftitle
FileName = fname
' Make sure the caption gets updated.
DataModified = True
SetDataChanged False
End Sub
' Load data from the file.
Private Sub LoadData(ftitle As String, fname As String)
Dim fnum As Integer
Dim i As Integer
' Open the file.
fnum = FreeFile
Open fname For Input As fnum
' Read the number of points.
Input #fnum, m_NumPoints
ReDim m_Points(1 To m_NumPoints)
' Read the points.
For i = 1 To m_NumPoints
With m_Points(i)
Input #fnum, .X, .Y
End With
Next i
' Close the file.
Close fnum
' Save the file name and title.
FileTitle = ftitle
FileName = fname
' Make sure the caption gets updated.
DataModified = True
SetDataChanged False
' Draw the new polygon.
DrawPoints
End Sub
' Draw the points.
Private Sub DrawPoints()
Dim i As Integer
Cls
For i = 1 To m_NumPoints
Circle (m_Points(i).X, m_Points(i).Y), RADIUS
Next i
End Sub
|
|
When the user moves the mouse, the program moves the most recently added point.
When the user clicks with the mouse, the program creates another point and lets the user start moving it.
|
|
Private Sub Form_MouseMove(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
If Not m_Drawing Then Exit Sub
Circle (m_LastX, m_LastY), RADIUS
m_LastX = X
m_LastY = Y
Circle (m_LastX, m_LastY), RADIUS
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
If m_Drawing Then
' Already drawing.
' Stop or place a point here and continue
If Button = vbRightButton Then
' Stop drawing.
m_Drawing = False
DrawMode = vbCopyPen
DrawPoints
Else
' Add another point to the list.
Circle (m_LastX, m_LastY), RADIUS
AddPoint X, Y
DrawMode = vbCopyPen
Circle (m_Points(m_NumPoints).X, _
m_Points(m_NumPoints).Y), RADIUS
DrawMode = vbInvert
Circle (m_Points(m_NumPoints).X, _
m_Points(m_NumPoints).Y), RADIUS
End If
Else
' Start drawing.
m_Drawing = True
Cls
m_NumPoints = 1
ReDim m_Points(1 To m_NumPoints)
With m_Points(m_NumPoints)
.X = X
.Y = Y
End With
m_LastX = X
m_LastY = Y
Circle (m_LastX, m_LastY), RADIUS
DrawMode = vbInvert
Circle (m_LastX, m_LastY), RADIUS
' Mark the data as modified.
SetDataChanged True
End If
End Sub
Private Sub AddPoint(ByVal X As Long, ByVal Y As Long)
m_NumPoints = m_NumPoints + 1
ReDim Preserve m_Points(1 To m_NumPoints)
With m_Points(m_NumPoints)
.X = X
.Y = Y
End With
End Sub
|
|
Whenever the user creates a point, the program calls subroutin SetDataChanged. That routine sets the variable DataModified to True and puts an asterisk in the program's title.
Function DataSafe returns True if it is safe for the program to discard the current data. If there are no changes to the data, the function simply returns True. If the data has been modified, the program asks the user if it is okay to discard or save the changes and takes appropriate action.
The form's QueryUnload event handler uses DataSafe to decide whether it should close the program's form.
|
|
Private DataModified As Boolean
' Set DataModified. Display an asterisk in the
' form's Caption next to the file name if
' appropriate.
Private Sub SetDataChanged(changed As Boolean)
' Don't bother if it's already been done.
If DataModified = changed Then Exit Sub
DataModified = changed
If changed Then
Caption = "Editor*[" & FileTitle & "]"
Else
Caption = "Editor [" & FileTitle & "]"
End If
End Sub
' Return True if the data is safe.
Private Function DataSafe() As Boolean
' No problem if the data is unmodified.
If Not DataModified Then
DataSafe = True
Exit Function
End If
' See if the user wants to save changes.
Select Case MsgBox("The data has been modified. Do you " & _
"want to save the changes?", _
vbYesNoCancel)
Case vbYes
' Save the data. Procedure SaveData
' will reset DataModified.
mnuFileSave_Click
DataSafe = Not DataModified
Case vbNo
' Discard the changes to the data.
DataSafe = True
Case vbNo
' Cancel.
DataSafe = False
End Select
End Function
' Make sure the data is safe to unload.
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode _
As Integer)
Cancel = Not DataSafe
End Sub
|
|
|
|
|
|