home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch12 / vbdLine.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-06-18  |  7.4 KB  |  260 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "vbdLine"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15. ' VbDraw Line/Rectangle object.
  16.  
  17. Implements vbdObject
  18.  
  19. ' Indicates a box rather than a line.
  20. Public IsBox As Boolean
  21.  
  22. ' The surface on which the user is clicking
  23. ' to define the object. This is set only during
  24. ' creation of this object.
  25. Public WithEvents m_Canvas As PictureBox
  26. Attribute m_Canvas.VB_VarHelpID = -1
  27. Private m_DrawingStarted As Boolean
  28.  
  29. ' Constituent vbdPolygon object.
  30. Private m_Polygon As vbdPolygon
  31. Private m_Object As vbdObject
  32.  
  33. ' Rubberband variables.
  34. Private m_StartX As Single
  35. Private m_StartY As Single
  36. Private m_LastX As Single
  37. Private m_LastY As Single
  38.  
  39. ' Start drawing a rubberband box.
  40. Private Sub m_Canvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  41.     m_DrawingStarted = True
  42.  
  43.     ' Start using dotted vbInvert mode.
  44.     m_Canvas.DrawMode = vbInvert
  45.     m_Canvas.DrawStyle = vbDot
  46.  
  47.     ' Start the first rubberband box.
  48.     m_StartX = X
  49.     m_StartY = Y
  50.     m_LastX = X
  51.     m_LastY = Y
  52.     If IsBox Then
  53.         m_Canvas.Line (m_StartX, m_StartY)-(m_LastX, m_LastY), , B
  54.     Else
  55.         m_Canvas.Line (m_StartX, m_StartY)-(m_LastX, m_LastY)
  56.     End If
  57. End Sub
  58.  
  59. ' Continue drawing the rubberband box.
  60. Private Sub m_Canvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  61.     If Not m_DrawingStarted Then Exit Sub
  62.  
  63.     ' Erase the old box.
  64.     If IsBox Then
  65.         m_Canvas.Line (m_StartX, m_StartY)-(m_LastX, m_LastY), , B
  66.     Else
  67.         m_Canvas.Line (m_StartX, m_StartY)-(m_LastX, m_LastY)
  68.     End If
  69.  
  70.     ' Update the point.
  71.     m_LastX = X
  72.     m_LastY = Y
  73.  
  74.     ' Draw the new box.
  75.     If IsBox Then
  76.         m_Canvas.Line (m_StartX, m_StartY)-(m_LastX, m_LastY), , B
  77.     Else
  78.         m_Canvas.Line (m_StartX, m_StartY)-(m_LastX, m_LastY)
  79.     End If
  80. End Sub
  81.  
  82.  
  83. ' Finish drawing the rubberband box.
  84. Private Sub m_Canvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  85.     If Not m_DrawingStarted Then Exit Sub
  86.  
  87.     ' Erase the old box.
  88.     If IsBox Then
  89.         m_Canvas.Line (m_StartX, m_StartY)-(m_LastX, m_LastY), , B
  90.     Else
  91.         m_Canvas.Line (m_StartX, m_StartY)-(m_LastX, m_LastY)
  92.     End If
  93.  
  94.     ' Go back to vbCopyPen drawing mode.
  95.     m_Canvas.DrawMode = vbCopyPen
  96.  
  97.     ' Stop receiving events from the canvas.
  98.     Set m_Canvas = Nothing
  99.  
  100.     ' Create the vbdPolygon that represents us.
  101.     Set m_Polygon = New vbdPolygon
  102.     Set m_Object = m_Polygon
  103.     If IsBox Then
  104.         ' Rectangle.
  105.         With m_Polygon
  106.             .NumPoints = 4
  107.             .X(1) = m_StartX
  108.             .X(2) = m_LastX
  109.             .X(3) = m_LastX
  110.             .X(4) = m_StartX
  111.             .Y(1) = m_StartY
  112.             .Y(2) = m_StartY
  113.             .Y(3) = m_LastY
  114.             .Y(4) = m_LastY
  115.             .IsClosed = True
  116.         End With
  117.     Else
  118.         ' Line.
  119.         With m_Polygon
  120.             .NumPoints = 2
  121.             .X(1) = m_StartX
  122.             .X(2) = m_LastX
  123.             .Y(1) = m_StartY
  124.             .Y(2) = m_LastY
  125.             .IsClosed = False
  126.         End With
  127.     End If
  128.  
  129.     ' Tell the form to save us.
  130.     frmVbDraw.AddObject Me
  131. End Sub
  132.  
  133. ' Add this transformation to the current one.
  134. Private Sub vbdObject_AddTransformation(M() As Single)
  135.     m_Object.AddTransformation M
  136. End Sub
  137.  
  138. Private Property Set vbdObject_Canvas(ByVal RHS As PictureBox)
  139.     Set m_Canvas = RHS
  140. End Property
  141.  
  142. Private Property Get vbdObject_Canvas() As PictureBox
  143.     Set vbdObject_Canvas = m_Canvas
  144. End Property
  145.  
  146. ' Clear the object's transformation.
  147. Private Sub vbdObject_ClearTransformation()
  148.     m_Object.ClearTransformation
  149. End Sub
  150.  
  151. ' Draw the object in a metafile.
  152. Private Sub vbdObject_DrawInMetafile(ByVal mf_dc As Long)
  153.     m_Object.DrawInMetafile mf_dc
  154. End Sub
  155. ' Return the object's DrawWidth.
  156. Public Property Get vbdObject_DrawWidth() As Integer
  157.     vbdObject_DrawWidth = m_Object.DrawWidth
  158. End Property
  159. ' Set the object's DrawWidth.
  160. Public Property Let vbdObject_DrawWidth(ByVal new_value As Integer)
  161.     m_Object.DrawWidth = new_value
  162. End Property
  163.  
  164. ' Return the object's DrawStyle.
  165. Public Property Get vbdObject_DrawStyle() As DrawStyleConstants
  166.     vbdObject_DrawStyle = m_Object.DrawStyle
  167. End Property
  168. ' Set the object's DrawStyle.
  169. Public Property Let vbdObject_DrawStyle(ByVal new_value As DrawStyleConstants)
  170.     m_Object.DrawStyle = new_value
  171. End Property
  172.  
  173. ' Return the object's ForeColor.
  174. Public Property Get vbdObject_ForeColor() As OLE_COLOR
  175.     vbdObject_ForeColor = m_Object.ForeColor
  176. End Property
  177. ' Set the object's ForeColor.
  178. Public Property Let vbdObject_ForeColor(ByVal new_value As OLE_COLOR)
  179.     m_Object.ForeColor = new_value
  180. End Property
  181.  
  182. ' Return the object's FillColor.
  183. Public Property Get vbdObject_FillColor() As OLE_COLOR
  184.     vbdObject_FillColor = m_Object.FillColor
  185. End Property
  186. ' Set the object's FillColor.
  187. Public Property Let vbdObject_FillColor(ByVal new_value As OLE_COLOR)
  188.     m_Object.FillColor = new_value
  189. End Property
  190.  
  191. ' Return the object's FillStyle.
  192. Public Property Get vbdObject_FillStyle() As FillStyleConstants
  193.     vbdObject_FillStyle = m_Object.FillStyle
  194. End Property
  195. ' Set the object's FillStyle.
  196. Public Property Let vbdObject_FillStyle(ByVal new_value As FillStyleConstants)
  197.     m_Object.FillStyle = new_value
  198. End Property
  199.  
  200. ' Return this object's bounds.
  201. Public Sub vbdObject_Bound(ByRef xmin As Single, ByRef ymin As Single, ByRef xmax As Single, ByRef ymax As Single)
  202.     m_Object.Bound xmin, ymin, xmax, ymax
  203. End Sub
  204. ' Draw the object on the canvas.
  205. Public Sub vbdObject_Draw(ByVal pic As Object)
  206.     m_Object.Draw pic
  207. End Sub
  208.  
  209. ' Set the object's Selected status.
  210. Private Property Let vbdObject_Selected(ByVal RHS As Boolean)
  211.     m_Object.Selected = RHS
  212. End Property
  213.  
  214. ' Return the object's Selected status.
  215. Private Property Get vbdObject_Selected() As Boolean
  216.     vbdObject_Selected = m_Object.Selected
  217. End Property
  218.  
  219. ' Return True if the object is at this location.
  220. Private Function vbdObject_IsAt(ByVal X As Single, ByVal Y As Single) As Boolean
  221.     vbdObject_IsAt = m_Object.IsAt(X, Y)
  222. End Function
  223.  
  224.  
  225. ' Initialize the object using a serialization string.
  226. ' The serialization does not include the
  227. ' ObjectType(...) part.
  228. Private Property Let vbdObject_Serialization(ByVal RHS As String)
  229. Dim token_name As String
  230. Dim token_value As String
  231. Dim next_x As Integer
  232. Dim next_y As Integer
  233.  
  234.     ' Start with a new polygon.
  235.     Set m_Polygon = New vbdPolygon
  236.     Set m_Object = m_Polygon
  237.  
  238.     ' Read tokens until there are no more.
  239.     Do While Len(RHS) > 0
  240.         ' Read a token.
  241.         GetNamedToken RHS, token_name, token_value
  242.         Select Case token_name
  243.             Case "IsBox"
  244.                 IsBox = CBool(token_value)
  245.             Case "vbdPolygon"
  246.                 m_Object.Serialization = token_value
  247.         End Select
  248.     Loop
  249. End Property
  250. ' Return a serialization string for the object.
  251. Public Property Get vbdObject_Serialization() As String
  252. Dim txt As String
  253.  
  254.     txt = txt & "  IsBox(" & Format$(IsBox) & ") "
  255.     txt = txt & m_Object.Serialization
  256.  
  257.     vbdObject_Serialization = "vbdLine(" & txt & ")"
  258. End Property
  259.  
  260.