一个简单的组件开发源码(自定义Picturebox)

来源:互联网 发布:罗辑思维人工智能 编辑:程序博客网 时间:2024/05/19 23:11


Option Strict On
Option Explicit On
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D

Public Class SensorPictureBox
    Inherits UserControl

    Public IsFirstRun As Boolean = True

    Private _IsResultPainted As Boolean
    Public Property IsResultPainted() As Boolean
        Get
            Return _IsResultPainted
        End Get
        Set(ByVal value As Boolean)
            _IsResultPainted = value
        End Set
    End Property

    Private _Value() As Double
    Public Property Value() As Double()
        Get
            Return _Value
        End Get
        Set(ByVal value As Double())
            _Value = value
            If Not IsResultPainted Then Me.Invalidate(False)
        End Set
    End Property

    Private _SensorReading(,) As Double = Nothing '{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
    Public Property SensorReading() As Double(,)
        Get
            Return _SensorReading
        End Get
        Set(ByVal value As Double(,))
            _SensorReading = value
            Me.Invalidate(False)
        End Set
    End Property

    Public Sub New()
        InitializeComponent()
        Me.BorderStyle = Windows.Forms.BorderStyle.Fixed3D  '设置窗体为凹陷的效果
        SensorReading = _SensorReading
        Me.AccessibleDescription = "Peter Jiang from LiteON."
        '最简单的实现双缓冲绘图
        'AllPaintingInWmPaint 忽略系统消息,防止图像闪烁
        'DoubleBuffer 设置双缓冲,防止图像抖动
        'UserPaint 自己为 控件做所有的绘图操作
        'Opaque 使窗体绘制时,不绘制背景
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer Or ControlStyles.Opaque, True)
        '更新风格
        Me.UpdateStyles()
    End Sub

    '重载消息处理事件。
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = &H14 Then
            Return
        End If
        MyBase.WndProc(m)
    End Sub

    ' 重载窗口寸改变事件
    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        MyBase.OnResize(e)
        Me.Invalidate(False)
    End Sub

    '重载窗口绘制事件
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        '绘制外框
        e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), Me.ClientRectangle)
        If IsFirstRun Then Exit Sub
        ' 加入在線段或曲線的起始圖形
        Dim startGP As New GraphicsPath()
        '加入四條直線
        startGP.AddEllipse(-1, -1, 2, 2)
        ' 封裝線條端點
        Dim start_cap As New CustomLineCap(Nothing, startGP)
        Dim clPen As New Pen(Color.Green, 2)
        Dim ResultPen As New Pen(Color.Cyan, 2)
        Dim lPen As New Pen(Color.Blue, 1.5)
        Dim font As New Font("Consolas", 10, FontStyle.Regular)
        '設定線段或曲線的開始樣式
        ResultPen.CustomStartCap = start_cap
        Dim X, Y As Integer
        Dim count As Integer = Value.Count - 1
        Dim Points(count) As Point
        For i As Integer = 0 To count Step 1
            X = 90 * i + 20
            Y = CInt(Value(i) * 100)
            Points(i) = New Point(X, Y)
            e.Graphics.DrawString(Value(i).ToString("0.000"), font, _
                    Brushes.BlueViolet, X - 10, Me.Height - 24)
        Next
        e.Graphics.DrawLine(clPen, 0, CInt(Me.Height / 2), Me.Width, CInt(Me.Height / 2))
        For i As Integer = 0 To count - 1
            '設定線段或曲線的結束樣式
            If i = count - 1 Then ResultPen.CustomEndCap = start_cap
            e.Graphics.DrawLine(ResultPen, Points(i), Points(i + 1))
        Next
        If Not IsResultPainted Then Exit Sub
        For iX As Integer = 0 To count Step 1
            X = 90 * iX + 20
            For iY As Integer = 0 To 1
                Dim Y0 As Integer = CInt(SensorReading(iX, iY) * 100)
                Dim Y1 As Integer = CInt(SensorReading(iX, iY + 1) * 100)
                e.Graphics.DrawLine(lPen, X, Y0, X, Y1)
                Select Case iY
                    Case 0
                        e.Graphics.DrawImage(My.Resources.Sensor_Start, New Rectangle(X - 4, Y0 - 4, 8, 8))
                        e.Graphics.DrawImage(My.Resources.Sensor_Nom, New Rectangle(X - 4, Y1 - 4, 8, 8))
                    Case 1
                        e.Graphics.DrawImage(My.Resources.Sensor_End, New Rectangle(X - 3, Y1 - 3, 6, 6))
                End Select
            Next
        Next
    End Sub
End Class

 

 

原创粉丝点击