一个简单的组件开发源码(自定义Picturebox)
2010-02-24 10:43
471 查看
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
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
相关文章推荐
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- ios开发UI基础—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- iOS开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- 微信/易信公共平台开发(一):开发了一个简单易用的PHP类(提供源码),十几行代码搞定一个公众号
- iOS开发之UI基础--纯代码自定义UItableviewcell实现一个简单的微博界面布局
- [微信小程序]组件化开发,以一个自定义模块框组件当做示例(附完整示例代码和效果图)
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- iOS开发UI基础—24使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- ios中的开发一些小工具(就是一个写UIView的自定义组件的脱离环境,可以重新使用)解耦
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- MySvn--一个简单单机svn程序源码-delphi开发