home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch12 / Hex2.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-06-19  |  2.6 KB  |  101 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 = "Hex"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. Private Const PI = 3.14159265
  17. Private Const NUM_SEGMENTS = 6
  18.  
  19. Public Cx As Single
  20. Public Cy As Single
  21. Public Radius As Single
  22.  
  23. Public Highlighted As Boolean
  24.  
  25. Private Type POINTAPI
  26.     X As Long
  27.     Y As Long
  28. End Type
  29. Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
  30. Private Declare Function PtInRegion Lib "gdi32" (ByVal hrgn As Long, ByVal X As Long, ByVal Y As Long) As Long
  31. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  32. Private Const ALTERNATE = 1
  33.  
  34. ' Inidcates the object has been drawn lately.
  35. Public Drawn As Boolean
  36. ' Bound this object.
  37. Public Sub Bound(ByRef xmin As Single, ByRef ymin As Single, ByRef xmax As Single, ByRef ymax As Single)
  38. Dim dx As Single
  39.  
  40.     dx = Radius * Cos(PI / 6)
  41.     xmin = Cx - dx
  42.     xmax = Cx + dx
  43.     ymin = Cy - Radius
  44.     ymax = Cy + Radius
  45. End Sub
  46.  
  47.  
  48. ' Draw on the PictureBox.
  49. Public Sub Draw(ByVal pic As PictureBox)
  50. Dim i As Integer
  51. Dim theta As Single
  52. Dim dtheta As Single
  53. Dim clr As OLE_COLOR
  54.  
  55.     ' Do nothing if we're already drawn.
  56.     If Drawn Then Exit Sub
  57.  
  58.     ' Pick an appropriate color.
  59.     If Highlighted Then
  60.         clr = vbRed
  61.     Else
  62.         clr = vbBlack
  63.     End If
  64.  
  65.     ' Draw.
  66.     theta = PI / 2
  67.     pic.CurrentX = Cx + Radius * Cos(theta)
  68.     pic.CurrentY = Cy + Radius * Sin(theta)
  69.  
  70.     dtheta = 2 * PI / NUM_SEGMENTS
  71.     For i = 1 To NUM_SEGMENTS
  72.         theta = theta + dtheta
  73.         pic.Line -(Cx + Radius * Cos(theta), Cy + Radius * Sin(theta)), clr
  74.     Next i
  75.  
  76.     ' Remember that we have been drawn.
  77.     Drawn = True
  78. End Sub
  79. ' Return True if the point is in the hex.
  80. Public Function IsAt(ByVal X As Single, ByVal Y As Single) As Boolean
  81. Dim points(1 To NUM_SEGMENTS) As POINTAPI
  82. Dim hrgn As Long
  83. Dim i As Integer
  84. Dim theta As Single
  85. Dim dtheta As Single
  86.  
  87.     theta = PI / 2
  88.     dtheta = 2 * PI / NUM_SEGMENTS
  89.     For i = 1 To NUM_SEGMENTS
  90.         points(i).X = 100 * (Cx + Radius * Cos(theta))
  91.         points(i).Y = 100 * (Cy + Radius * Sin(theta))
  92.         theta = theta + dtheta
  93.     Next i
  94.  
  95.     hrgn = CreatePolygonRgn(points(1), 5, ALTERNATE)
  96.     IsAt = PtInRegion(hrgn, 100 * X, 100 * Y)
  97.     DeleteObject hrgn
  98. End Function
  99.  
  100.  
  101.