Public Class 仪表盘 Private _Bg As Bitmap Private _Max As Integer = 100 Private _Value As Integer = 25 Private _Rote As Single Private _Digit As Bitmap Public Property MaxValue As Integer Get Return _Max End Get Set(value As Integer) If value < _Value Then value = _Value _Max = value _Rote = 360 / _Max * _Value MakeBg() End Set End Property Public Property Value As Integer Get Return _Value End Get Set(value As Integer) If value > _Max Then value = _Max _Value = value _Rote = 360 / _Max * _Value MakeBg() End Set End Property Private Sub 仪表盘_Load(sender As Object, e As EventArgs) Handles MyBase.Load MakeBg() End Sub Private Sub MakeBg() If _Bg IsNot Nothing Then _Bg.Dispose() _Bg = New Bitmap(344, 344) Using G As Graphics = Graphics.FromImage(_Bg) G.SmoothingMode = Drawing2D.SmoothingMode.HighQuality G.DrawImage(My.Resources.表盘底, New Rectangle(0, 0, 344, 344), New Rectangle(0, 0, 344, 344), GraphicsUnit.Pixel) Try Dim Gs As Drawing2D.GraphicsState = G.Save Using Gp As New Drawing2D.GraphicsPath Gp.AddPie(New Rectangle(0, 0, 344, 344), -90, _Rote) Using Txbur As New TextureBrush(My.Resources.表盘底1) G.FillPath(Txbur, Gp) End Using End Using G.TranslateTransform(172, 172) G.RotateTransform(_Rote) G.DrawImage(My.Resources.表盘底指针, New Rectangle(-172, -172, 344, 344), New Rectangle(0, 0, 344, 344), GraphicsUnit.Pixel) G.Restore(Gs) G.DrawImage(My.Resources.表盘液晶数字底, New Rectangle(0, 0, 344, 344), New Rectangle(0, 0, 344, 344), GraphicsUnit.Pixel) MakeDigit() Dim XX, YY As Single XX = (344 - _Digit.Width) / 2 YY = (344 - _Digit.Height) / 2 G.DrawImage(_Digit, New Rectangle(XX, YY, _Digit.Width, _Digit.Height), New Rectangle(0, 0, _Digit.Width, _Digit.Height), GraphicsUnit.Pixel) Catch End Try End Using Me.BackgroundImage = _Bg Me.Invalidate() End Sub Private Sub MakeDigit() If _Digit IsNot Nothing Then _Digit.Dispose() Dim V As Integer = _Value / _Max * 100 Dim S As String = Format(V, "000") Dim W, H As Integer W = 30 * S.Length H = 30 _Digit = New Bitmap(W, H) Using G As Graphics = Graphics.FromImage(_Digit) For I As Integer = 0 To S.Length - 1 G.DrawImage(My.Resources.液晶数字, New Rectangle(I * 30, 0, 30, 30), New Rectangle(30 * S.Substring(I, 1), 0, 30, 30), GraphicsUnit.Pixel) Next End Using End SubEnd Class
评论