Option Explicit
Option Compare Text
Private Declare Sub LineTo Lib "gdi32" (ByVal hdc As Long, ByVal xEnd As Long, ByVal yEnd As Long)
Private Declare Sub BitBlt Lib "gdi32" (ByVal dDestDC As Long, ByVal X As Long, ByVal Y As Long, _
ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDc As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
ByVal dwRop As Long)
Private Declare Sub Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long)
Private Declare Sub TextOut Lib "gdi32" Alias "TextOutA" _
(ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, _
ByVal lpString As String, ByVal nCount As Long)
Private Declare Sub PaintRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long)
Private Declare Sub MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, _
ByVal pLastPoint As Long)
Private Declare Sub DeleteDC Lib "gdi32" (ByVal hdc As Long)
Private Declare Sub DeleteObject Lib "gdi32" (ByVal hObject As Long)
Private Declare Function CreatePolygonRgn Lib "gdi32" (ByRef lppt As POINTAPI, ByVal cPoints As Long, _
ByVal fnPolyFillMode As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Private i As Long
Private j As Long
Private lngTextWidth As Long
Private lngTextHeight As Long
Private lngColor As Long
Private lngLineCount As Long
Private lngVertexCount As Long
Private lngRadius As Long
Private dblRadian As Double
Private Points() As POINTAPI
Private PointsCopy() As POINTAPI
Private CenterPos As POINTAPI
Private LabelPos As POINTAPI
Private strLabel() As String
Private lngNumerator() As Long
Private lngDenominator() As Long
Private lngCompatibleDC As Long
Private Const PI As Double = 3.14159265
Private Const ALTERNATE As Long = 1
Private Const WINDING As Long = 2
Private Sub Form_Click()
strLabel(0) = "国語"
strLabel(1) = "数学"
strLabel(2) = "理科"
strLabel(3) = "社会"
strLabel(4) = "英語"
lngNumerator(0) = 20
lngNumerator(1) = 20
lngNumerator(2) = 20
lngNumerator(3) = 20
lngNumerator(4) = 20
lngDenominator(0) = 19
lngDenominator(1) = 19
lngDenominator(2) = 6
lngDenominator(3) = 20
lngDenominator(4) = 13
picDrawSpace.Cls
Call subPaintPolygon(Me, CenterPos.X, CenterPos.Y, lngVertexCount, True)
lngDenominator(0) = 15
lngDenominator(1) = 18
lngDenominator(2) = 6
lngDenominator(3) = 14
lngDenominator(4) = 1
Call subPaintPolygon(Me, CenterPos.X, CenterPos.Y, lngVertexCount, True, vbYellow)
Call subDrawPolygon(Me, CenterPos.X, CenterPos.Y, lngVertexCount, &H6600&, vbGreen, lngLineCount)
Me.Refresh
End Sub
Private Sub Form_Load()
Dim n As Long
lngVertexCount = 5
lngLineCount = 5
lngRadius = 100
With CenterPos
.X = 180
.Y = 150
End With
n = lngVertexCount - 1
ReDim Points(n)
ReDim strLabel(n)
ReDim lngNumerator(n)
ReDim lngDenominator(n)
ReDim PointsCopy(n)
For i = 0 To n
dblRadian = (360 / lngVertexCount) * i * PI / 180
With Points(i)
.X = Sin(dblRadian) * lngRadius
.Y = Cos(dblRadian) * -lngRadius
End With
Next i
With Me
.Font.Size = 9
.ScaleMode = vbPixels
.AutoRedraw = True
lngTextWidth = .TextWidth("X")
lngTextHeight = .TextHeight("X")
With .picDrawSpace
.Visible = False
.AutoRedraw = True
.ScaleMode = vbPixels
.FillStyle = vbFSSolid
.BackColor = Me.BackColor
.Move 0, 0, Me.Width \ 15, Me.Height \ 15
lngCompatibleDC = CreateCompatibleDC(.hdc)
End With
End With
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call DeleteDC(lngCompatibleDC)
Erase Points()
Erase PointsCopy()
Erase strLabel()
Erase lngNumerator()
Erase lngDenominator()
End Sub
Private Sub subDrawPolygon(ByVal pObject As Object, _
ByVal pX As Long, _
ByVal pY As Long, _
ByVal pN As Long, _
Optional ByVal pColor1 As Long = vbBlack, _
Optional ByVal pColor2 As Long = vb3DShadow, _
Optional ByVal pGraduations As Long = 1)
Dim w_Margin As Long
Dim w_TextWidth As Long
With pObject
.ForeColor = pColor1
For i = 0 To pN - 1
MoveToEx pObject.hdc, pX, pY, 0
LineTo pObject.hdc, pX + Points(i).X, pY + Points(i).Y
Next i
End With
For j = 1 To pGraduations
For i = 0 To pN - 1
With PointsCopy(i)
.X = pX + Points(i).X * j \ pGraduations
.Y = pY + Points(i).Y * j \ pGraduations
End With
Next i
If j = pGraduations Then
lngColor = pColor1
Else
lngColor = pColor2
End If
pObject.ForeColor = lngColor
Call Polygon(pObject.hdc, PointsCopy(0), pN)
Next j
w_Margin = 4
For i = 0 To pN - 1
w_TextWidth = LenB(StrConv(strLabel(i), vbFromUnicode)) * lngTextWidth
With Points(i)
If .X = 0 Then
LabelPos.X = .X - w_TextWidth \ 2
ElseIf .X > 0 Then
LabelPos.X = .X + w_Margin
Else
LabelPos.X = .X - w_TextWidth - w_Margin
End If
If .Y = 0 Then
LabelPos.Y = .Y - lngTextHeight \ 2
ElseIf .Y > 0 Then
LabelPos.Y = .Y + w_Margin
Else
LabelPos.Y = .Y - lngTextHeight - w_Margin
End If
End With
TextOut pObject.hdc, LabelPos.X + pX, LabelPos.Y + pY, strLabel(i), LenB(strLabel(i))
Next i
End Sub
Private Sub subPaintPolygon(ByVal pObject As Object, _
ByVal pX As Long, _
ByVal pY As Long, _
ByVal pN As Long, _
Optional ByVal pZeroToAll As Boolean = False, _
Optional ByVal pColor As Long = vbWhite)
Dim hRgn As Long
For i = 0 To pN - 1
With PointsCopy(i)
If lngNumerator(i) = 0 Then
If pZeroToAll Then
.X = pX + Points(i).X
.Y = pY + Points(i).Y
Else
.X = pX
.Y = pY
End If
Else
.X = pX + Points(i).X * lngDenominator(i) \ lngNumerator(i)
.Y = pY + Points(i).Y * lngDenominator(i) \ lngNumerator(i)
End If
End With
Next i
With picDrawSpace
.FillColor = pColor
hRgn = CreatePolygonRgn(PointsCopy(0), pN, WINDING)
Call PaintRgn(.hdc, hRgn)
Call DeleteObject(hRgn)
Call BitBlt(pObject.hdc, 0, 0, .Width, .Height, .hdc, 0, 0, vbSrcCopy)
End With
End Sub