home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch12 / Hex1.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-06-19  |  2.4 KB  |  92 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. ' Bound this object.
  34. Public Sub Bound(ByRef xmin As Single, ByRef ymin As Single, ByRef xmax As Single, ByRef ymax As Single)
  35. Dim dx As Single
  36.  
  37.     dx = Radius * Cos(PI / 6)
  38.     xmin = Cx - dx
  39.     xmax = Cx + dx
  40.     ymin = Cy - Radius
  41.     ymax = Cy + Radius
  42. End Sub
  43.  
  44.  
  45. ' Draw on the PictureBox.
  46. Public Sub Draw(ByVal pic As PictureBox)
  47. Dim i As Integer
  48. Dim theta As Single
  49. Dim dtheta As Single
  50. Dim clr As OLE_COLOR
  51.  
  52.     ' Pick an appropriate color.
  53.     If Highlighted Then
  54.         clr = vbRed
  55.     Else
  56.         clr = vbBlack
  57.     End If
  58.  
  59.     ' Draw.
  60.     theta = PI / 2
  61.     pic.CurrentX = Cx + Radius * Cos(theta)
  62.     pic.CurrentY = Cy + Radius * Sin(theta)
  63.  
  64.     dtheta = 2 * PI / NUM_SEGMENTS
  65.     For i = 1 To NUM_SEGMENTS
  66.         theta = theta + dtheta
  67.         pic.Line -(Cx + Radius * Cos(theta), Cy + Radius * Sin(theta)), clr
  68.     Next i
  69. End Sub
  70. ' Return True if the point is in the hex.
  71. Public Function IsAt(ByVal X As Single, ByVal Y As Single) As Boolean
  72. Dim points(1 To NUM_SEGMENTS) As POINTAPI
  73. Dim hrgn As Long
  74. Dim i As Integer
  75. Dim theta As Single
  76. Dim dtheta As Single
  77.  
  78.     theta = PI / 2
  79.     dtheta = 2 * PI / NUM_SEGMENTS
  80.     For i = 1 To NUM_SEGMENTS
  81.         points(i).X = 100 * (Cx + Radius * Cos(theta))
  82.         points(i).Y = 100 * (Cy + Radius * Sin(theta))
  83.         theta = theta + dtheta
  84.     Next i
  85.  
  86.     hrgn = CreatePolygonRgn(points(1), 5, ALTERNATE)
  87.     IsAt = PtInRegion(hrgn, 100 * X, 100 * Y)
  88.     DeleteObject hrgn
  89. End Function
  90.  
  91.  
  92.