如何让仪表指针绕某点旋转
我想做一个仪表,用panel控件,背景图是仪表盘,然后在上面放了一个picturebox控件,image是指针。
我想让指针绕着靠近末端的一点转动,也就是让picturebox绕着某一点转动。我只要输入0的时候,指针指向仪表盘的0刻度,输入100时指向最大刻度。仪表盘上的0到最大刻度之间大概有150度。
如果有更好的思路做仪表盘的,也请指导一下,谢谢!!!
[解决办法]
http://download.csdn.net/detail/wuyazhe/2090762
按这里的例子,可以自己绘制指针,旋转画布。
[解决办法]
参考一下
[解决办法]
Public Class ybp
Inherits PictureBox
''' <summary>
''' 用这个背景图来放你的仪表盘
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Overloads Property BackgroundImage() As Image
Get
Return MyBase.BackgroundImage
End Get
Set(ByVal value As Image)
Me.Width = value.Width
Me.Height = value.Height
Me.Location = New Point(Me.Parent.Width / 2 - Me.Width / 2, Me.Parent.Height / 2 - Me.Height / 2)
MyBase.BackgroundImage = value
End Set
End Property
Dim _Value As Integer = 35
''' <summary>
''' 仪表盘最大量程,即最大数值
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Value() As Integer
Get
Return _Value
End Get
Set(ByVal value As Integer)
_Value = value
Me.Refresh()
End Set
End Property
Dim ZzColor As Color
Dim ZzWidth As Integer
Dim ZzSlength As Integer
Dim ZzClength As Integer
Dim MaxNumber As Integer = 100
Private Sub ybp_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim ZzColor As Color = Color.Red
Dim ZzWidth As Integer = 3
Dim ZzSlength As Integer = Math.Min(Width, Height) / 5
Dim ZzClength As Integer = Math.Min(Width, Height) / 2 - 10
e.Graphics.DrawEllipse(New Pen(Color.Red, 1), New Rectangle(Width / 2 - 2, Height / 2 - 2, 5, 5))
e.Graphics.DrawLine(New Pen(ZzColor, ZzWidth), CSng(Width / 2 + ZzClength * Math.Sin(_Value / MaxNumber * 2 * Math.PI)), _
CSng(Height / 2 - ZzClength * Math.Cos(_Value / MaxNumber * 2 * Math.PI)), CSng(Width / 2), CSng(Height / 2))
End Sub
End Class
[解决办法]
这个不要用那么多控件,控件只是呈现的,一个就行。你就一张画布,几个图像就可以了,等图像在画布上画好了,你再把它显示在控件上。
Public Class Form1
Dim bb As Image
Dim bt As Image
Dim bv As Image
Dim grb As Graphics
Dim grv As Graphics
Dim len As Integer = 200
Dim ft As Integer = 120
Dim ang As Single = 150 / ft
Dim sang As Single = 150
Dim inc As Boolean
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Height = len + Me.Height - Me.ClientRectangle.Height
Me.Width = len + Me.Width - Me.ClientRectangle.Width
bb = New Bitmap(len, len)
bt = New Bitmap(len \ 2, len \ 2)
bv = New Bitmap(len, len)
Graphics.FromImage(bb).FillPie(Brushes.Black, New Rectangle(Point.Empty, bb.Size), 0, 360)
Graphics.FromImage(bt).DrawLine(Pens.Red, Point.Empty, New PointF(bt.Width / Math.Sqrt(2), bt.Height / Math.Sqrt(2)))
grb = Graphics.FromImage(bv)
grv = Graphics.FromHwnd(Me.Handle)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For i = 0 To ft
Threading.Thread.Sleep(10)
grb.DrawImage(bb, Point.Empty)
grb.TranslateTransform(len \ 2, len \ 2)
grb.RotateTransform(ang * i + sang)
grb.DrawImage(bt, Point.Empty)
grb.RotateTransform(-ang * i - sang)
grb.TranslateTransform(-len \ 2, -len \ 2)
grv.DrawImage(bv, Point.Empty)
Next
End Sub
End Class