简单的量杯液位显示控件

来源:互联网 发布:isis 剿灭 知乎 编辑:程序博客网 时间:2024/04/28 03:20

简单的量杯液位显示控件

http://blog.csdn.net/laviewpbt/article/details/754633

前几天做了个简单控制实验,通过PLC和串口通讯来控制阀门的开关,从而调节水箱的水位。我们通过CCD摄像机摄取水位图像后经采集卡送至上位机,并分析识别水位的高度,为了动态形象的描述水位的位置,就写了个简单的量杯液位控件。

Imports System.Drawing.Drawing2D

Imports System.ComponentModel

Public Class Cup

    Inherits System.Windows.Forms.Control

 

    Public Sub New()

        setstyle(ControlStyles.AllPaintingInWmPaint, True)

        setstyle(ControlStyles.DoubleBuffer, True)

        setstyle(ControlStyles.UserPaint, True)

    End Sub

 

    Private mValue As Integer = 50               '当前值

    Private mMax As Integer = 100                '最大值

    Private mMin As Integer = 2                  '最小值

    Private mMajorInterval As Integer            '主要数据之间的间距

    Private mMajorFactor As Single               '主要刻度相对于宽度的比例

    Private mMiddleMarkFactor As Single          '次要刻度相对于宽度的比例

    Private mDivision As Integer                 '两个主要数据之间的等分数

    Private mDivisionMarkFactor As Single        '夹在主要刻度和次要刻度之间的刻度相对于宽度的比例

    Private mScaleDirection As Direction         '刻度的位置

    Private mGradientStartColor As Color = Color.FromArgb(155, 158, 117) '填充的渐变起始色

    Private mGradientEndColor As Color = Color.FromArgb(140, 138, 110)   '填充的渐变终止色

    Private mBorderStyle As Border3DStyle = Border3DStyle.Flat           '边框样式

 

    Public Enum Direction

        Left = 0

        Right = 1

    End Enum

 

    Public Property Value() As Integer

        Get

            Return mValue   '液位值

        End Get

        Set(ByVal Value As Integer)

            If Value < mMin Then

                mValue = mMin

            ElseIf Value > mMax Then   '取保在合理的范围内

                mValue = mMax

            Else

                mValue = Value

            End If

            Me.Invalidate()

        End Set

    End Property

 

    <DefaultValue(10)> _

    Public Property Max() As Integer

        Get

            Return mMax

        End Get

        Set(ByVal Value As Integer)

            mMax = Value

            If mMax < mValue Then

                mValue = mMax       '取保在合理的范围内

            End If

            If mMax < mMin Then

                mMin = mMax

            End If

            Me.Invalidate()

        End Set

    End Property

 

    <DefaultValue(0)> _

    Public Property Min() As Integer

        Get

            Return mMin

        End Get

        Set(ByVal Value As Integer)

            mMin = Value                 '取保在合理的范围内

            If mMin > mMax Then

                mMax = mMin

            End If

            If mMin > mValue Then

                mValue = mMin

            End If

            Me.Invalidate()

        End Set

    End Property

 

    <DefaultValue(1)> _

    Public Property MajorInterval() As Integer

        Get

            Return mMajorInterval

        End Get

        Set(ByVal Value As Integer)

            If Value > mMax - mMin Then

                mMajorInterval = mMax - mMin    '取保在合理的范围内

            ElseIf Value <= 0 Then

                mMajorInterval = 1

            Else

                mMajorInterval = Value

            End If

            Me.Invalidate()

        End Set

    End Property

 

 

    <DefaultValue(0.5)> _

    Public Property MajorFactor() As Single

        Get

            Return mMajorFactor

        End Get

        Set(ByVal Value As Single)

            If Value >= 1 Or Value <= 0 Then     '取保在合理的范围内

                mMajorFactor = 0.5

            Else

                mMajorFactor = Value

            End If

            Me.Invalidate()

        End Set

    End Property

 

    <DefaultValue(0.25)> _

        Public Property MiddleMarkFactor() As Single

        Get

            Return mMiddleMarkFactor

        End Get

        Set(ByVal Value As Single)

            If Value >= 1 Or mMiddleMarkFactor <= 0 Then    '取保在合理的范围内

                mMiddleMarkFactor = 0.3

            ElseIf Value >= mMajorFactor Then

                mMiddleMarkFactor = mMajorFactor * 0.75

            Else

                mMiddleMarkFactor = Value

            End If

            Me.Invalidate()

        End Set

    End Property

 

    <DefaultValue(9)> _

    Public Property Division() As Integer     '取保在合理的范围内

        Get

            Return mDivision

        End Get

        Set(ByVal Value As Integer)

            If Value > 10 Then

                mDivision = 10

            ElseIf Value < 0 Then

                mDivision = 2

            Else

                mDivision = Value

            End If

            Me.Invalidate()

        End Set

    End Property

 

 

 

 

    <DefaultValue(0.125)> _

    Public Property DivisionMarkFactor() As Single

        Get

            Return mDivisionMarkFactor

        End Get

        Set(ByVal Value As Single)

            If Value >= 1 Or mDivisionMarkFactor <= 0 Then    '取保在合理的范围内

                mDivisionMarkFactor = 0.2

            ElseIf Value > mMiddleMarkFactor Then

                mDivisionMarkFactor = mMiddleMarkFactor * 0.75

            Else

                mDivisionMarkFactor = Value

            End If

            Me.Invalidate()

        End Set

    End Property

 

    Public Property GradientStartColor() As Color

        Get

            Return mGradientStartColor

        End Get

        Set(ByVal Value As Color)

            mGradientStartColor = Value        '边界的渐变色

            Me.Invalidate()

        End Set

    End Property

 

 

 

    Public Property GradientEndColor() As Color

        Get

            Return mGradientEndColor

        End Get                                     '中间的颜色

        Set(ByVal Value As Color)

            mGradientEndColor = Value

            Me.Invalidate()

        End Set

    End Property

 

 

 

    Public Property ScaleDirection() As Direction

        Get

            Return mScaleDirection

        End Get

        Set(ByVal Value As Direction)              '刻度的位置

            mScaleDirection = Value

            Me.Invalidate()

        End Set

    End Property

 

    Public Property BorderStyle() As Border3DStyle

        Get

            Return (mBorderStyle)

        End Get

        Set(ByVal Value As Border3DStyle)            '边框

            mBorderStyle = Value

            Me.Invalidate()

        End Set

    End Property

 

 

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        Dim g As Graphics = e.Graphics

        Dim GradientBrush As LinearGradientBrush

        Dim LiquidArea As RectangleF

        LiquidArea = New RectangleF(0, Me.Height - (Me.Value - Me.Min) / (Me.Max - Me.Min) * Me.Height, Me.Width / 2.0F, (Me.Value - Me.Min) / (Me.Max -Me.Min) * Me.Height)

        If Not LiquidArea.Height = 0 Then

            GradientBrush = New LinearGradientBrush(LiquidArea, GradientStartColor, GradientEndColor, LinearGradientMode.Horizontal)

            g.FillRectangle(GradientBrush, LiquidArea)   '一般中间颜色要深些,左半渐变区域

            LiquidArea.Offset(LiquidArea.Width, 0)

            GradientBrush = New LinearGradientBrush(LiquidArea, GradientEndColor, GradientStartColor, LinearGradientMode.Horizontal)

            g.FillRectangle(GradientBrush, LiquidArea)   '右半渐变区域

            GradientBrush.Dispose()

        End If

 

        Dim IntervalHeight As Single

        IntervalHeight = Me.Height / ((Me.Max - Me.Min) / Me.MajorInterval)   '每个大刻度所对应的象素距离

        Dim i, j As Integer

        If Me.ScaleDirection = Direction.Left Then

            For i = 0 To ((Me.Max - Me.Min) / Me.MajorInterval)

                '先绘制主要刻度的线条

                g.DrawLine(New Pen(Me.ForeColor), 0.0F, Me.Height - i * IntervalHeight, Me.Width * MajorFactor, Me.Height - i * IntervalHeight)

                For j = 0 To Me.Division - 1  '绘制每个最小刻度的线条

                    g.DrawLine(New Pen(Me.ForeColor), 0.0F, Me.Height - i * IntervalHeight + IntervalHeight * j / Me.Division, Me.Width *Me.DivisionMarkFactor, Me.Height - i * IntervalHeight + IntervalHeight * j / Me.Division)

                Next

                If Me.Division Mod 2 <> 1 Then   '当小刻度的数量是基数时,不绘制次要刻度

                    g.DrawLine(New Pen(Me.ForeColor), 0.0F, Me.Height - (i + 0.5F) * IntervalHeight, Me.Width * Me.MiddleMarkFactor, Me.Height - (i + 0.5F) * IntervalHeight)

                End If  '绘制数字提示

                Dim FontSize As SizeF = g.MeasureString((Me.MajorInterval * i + +Me.Min).ToString, Me.Font)

                g.DrawString((Me.MajorInterval * i + +Me.Min).ToString, Me.Font, New SolidBrush(Me.ForeColor), Me.Width * Me.MajorFactor - FontSize.Width / 2, Me.Height - i * IntervalHeight - FontSize.Height)

            Next

        Else

            For i = 0 To ((Me.Max - Me.Min) / Me.MajorInterval)

                '先绘制主要数据的线条

                g.DrawLine(New Pen(Me.ForeColor), Me.Width, Me.Height - i * IntervalHeight, Me.Width - Me.Width * MajorFactor, Me.Height - i * IntervalHeight)

                For j = 0 To Me.Division - 1  '绘制每个最小刻度的线条

                    g.DrawLine(New Pen(Me.ForeColor), Me.Width, Me.Height - i * IntervalHeight + IntervalHeight * j / Me.Division, Me.Width - Me.Width *Me.DivisionMarkFactor, Me.Height - i * IntervalHeight + IntervalHeight * j / Me.Division)

                Next

                If Me.Division Mod 2 <> 1 Then   '当小刻度的数量是基数时,不绘制次要刻度

                    g.DrawLine(New Pen(Me.ForeColor), Me.Width, Me.Height - (i + 0.5F) * IntervalHeight, Me.Width - Me.Width * Me.MiddleMarkFactor, Me.Height - (i + 0.5F) * IntervalHeight)

                End If  '绘制数字提示

                Dim FontSize As SizeF = g.MeasureString((Me.MajorInterval * i + +Me.Min).ToString, Me.Font)

                g.DrawString((Me.MajorInterval * i + +Me.Min).ToString, Me.Font, New SolidBrush(Me.ForeColor), Me.Width - Me.Width * Me.MajorFactor - FontSize.Width / 2, Me.Height - i * IntervalHeight - FontSize.Height)

            Next

        End If

        ControlPaint.DrawBorder3D(e.Graphics, Me.ClientRectangle, mBorderStyle)   '绘制边界

    End Sub

End Class

 

效果图如下:

 


0 0
原创粉丝点击