首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

怎么绘制三角形

2012-01-29 
如何绘制三角形?要做一个指针仪表控件,指针采用三角形,以中心点旋转,请问如何绘制三角形并旋转???[解决办

如何绘制三角形?
要做一个指针仪表控件,指针采用三角形,以中心点旋转,请问如何绘制三角形并旋转???

[解决办法]
给你一个例子:
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
paintClock(Me.ClientRectangle)
End Sub
Friend Sub paintClock(ByVal rect As Rectangle)
If (Not MyBase.IsDisposed AndAlso MyBase.Visible) Then
Dim graphics1 As BufferedGraphics = BufferedGraphicsManager.Current.Allocate(MyBase.CreateGraphics, rect)
If (Not graphics1.Graphics Is Nothing) Then
graphics1.Graphics.Clear(Me.BackColor)
Me.drawClock(graphics1.Graphics, MyBase.ClientRectangle)
End If
graphics1.Render()
graphics1.Dispose()
End If
End Sub

Private Sub drawClock(ByVal g As Graphics, ByVal rectangle As Rectangle)
Dim brush1 As New SolidBrush(Color.SteelBlue)
Dim brush2 As New SolidBrush(Color.SkyBlue)
Dim brush3 As New SolidBrush(Color.Teal)
Dim brush4 As New SolidBrush(Color.Teal)
Dim brush5 As New SolidBrush(Color.Black)
Dim pen1 As New Pen(brush1, 2.0!)
Dim pen2 As New Pen(brush2, 1.5!)
Dim pen3 As New Pen(brush3, 0.2!)
Dim pen4 As New Pen(brush4, 0.2!)
Dim pen5 As New Pen(brush5, 1.0!)
Dim point1 As New Point((MyBase.ClientRectangle.Left + (MyBase.ClientRectangle.Width / 2)), (MyBase.ClientRectangle.Top + (MyBase.ClientRectangle.Height / 2)))
Dim num1 As Integer = 1
Do While (num1 <= 60)
If ((num1 Mod 5) = 0) Then
g.DrawEllipse(pen1, CSng((point1.X + CInt((Math.Cos(((Math.PI / 180 * num1) * 6)) * 60)))), CSng((point1.Y + CInt((Math.Sin(((Math.PI / 180 * num1) * 6)) * 60)))), 2.0!, 2.0!)
Else
g.DrawEllipse(pen2, CSng((point1.X + CInt((Math.Cos(((Math.PI / 180 * num1) * 6)) * 60)))), CSng((point1.Y + CInt((Math.Sin(((Math.PI / 180 * num1) * 6)) * 60)))), 1.5!, 1.5!)
End If
num1 += 1
Loop
Dim point2 As New Point((point1.X + CInt((Math.Cos((Math.PI / 180 * (((DateTime.Now.Hour - 3) * 30) + ((DateTime.Now.Minute / 12) * 6)))) * 40))), (point1.Y + CInt((Math.Sin((Math.PI / 180 * (((DateTime.Now.Hour - 3) * 30) + ((DateTime.Now.Minute / 12) * 6)))) * 40))))
Dim point3 As New Point((point1.X + CInt((Math.Cos((Math.PI / 180 * (((DateTime.Now.Hour + 3) * 30) + ((DateTime.Now.Minute / 12) * 6)))) * 7))), (point1.Y + CInt((Math.Sin((Math.PI / 180 * (((DateTime.Now.Hour + 3) * 30) + ((DateTime.Now.Minute / 12) * 6)))) * 7))))
Dim point4 As New Point((point1.X + CInt((Math.Cos((Math.PI / 180 * ((DateTime.Now.Hour * 30) + ((DateTime.Now.Minute / 12) * 6)))) * -5))), (point1.Y + CInt((Math.Sin((Math.PI / 180 * ((DateTime.Now.Hour * 30) + ((DateTime.Now.Minute / 12) * 6)))) * -5))))
Dim point5 As New Point((point1.X + CInt((Math.Cos((Math.PI / 180 * ((DateTime.Now.Hour * 30) + ((DateTime.Now.Minute / 12) * 6)))) * 5))), (point1.Y + CInt((Math.Sin((Math.PI / 180 * ((DateTime.Now.Hour * 30) + ((DateTime.Now.Minute / 12) * 6)))) * 5))))
Dim pointArray1 As Point() = New Point() {point2, point4, point3, point5}
g.FillPolygon(brush1, pointArray1)
Dim point6 As New Point((point1.X + CInt((Math.Cos(((Math.PI / 180 * (DateTime.Now.Minute - 15)) * 6)) * 48))), (point1.Y + CInt((Math.Sin(((Math.PI / 180 * (DateTime.Now.Minute - 15)) * 6)) * 48))))
Dim point7 As New Point((point1.X + CInt((Math.Cos(((Math.PI / 180 * (DateTime.Now.Minute + 15)) * 6)) * 9))), (point1.Y + CInt((Math.Sin(((Math.PI / 180 * (DateTime.Now.Minute + 15)) * 6)) * 9))))
Dim point8 As New Point((point1.X + CInt((Math.Cos(((Math.PI / 180 * DateTime.Now.Minute) * 6)) * -4))), (point1.Y + CInt((Math.Sin(((Math.PI / 180 * DateTime.Now.Minute) * 6)) * -4))))


Dim point9 As New Point((point1.X + CInt((Math.Cos(((Math.PI / 180 * DateTime.Now.Minute) * 6)) * 4))), (point1.Y + CInt((Math.Sin(((Math.PI / 180 * DateTime.Now.Minute) * 6)) * 4))))
pointArray1 = New Point() {point6, point8, point7, point9}
g.FillPolygon(brush1, pointArray1)
Dim point10 As New Point(point1.X, point1.Y)
Dim point11 As New Point((point1.X + CInt((Math.Cos(((Math.PI / 180 * (DateTime.Now.Second - 15)) * 6)) * 48))), (point1.Y + CInt((Math.Sin(((Math.PI / 180 * (DateTime.Now.Second - 15)) * 6)) * 48))))
g.DrawLine(pen5, point10, point11)
End Sub
End Class

[解决办法]
通过矩阵的旋转可以很方便的控制指针的旋转

热点排行