Für die Visualisierung von Meß und Regelaufgaben bietet .NET nichts berauschendes an Steuerelementen. Deshalb wird dazu meistens LabView verwendet.
Leider ist es relativ teuer. Deshalb habe ich begonnen Steuerelemente selber zu erstellen. Als erstes einen vertikalen Balken,
der einen bestimmten Wert als Balkenlänge darstellen kann. Es sind auch negative Werte zulässig.
Zusätzlich wird ein MIN und MAX-Wert durch eine Markierung und den Wert gehalten. Geschrieben wurde der Code mit dem kostenlosen SharpDevelop
' Option Strict On Imports System.ComponentModel Imports System.Math Public Partial Class LevelMeter Inherits System.Windows.Forms.UserControl Dim sglValue As Single = 0 dim intValue as integer Dim sglMax As Single = 100 Dim sglMin As Single = 0 dim intZeroValue as integer Dim intStoreMax As Integer dim sglStoreMax as single Dim intStoreMin As Integer Dim sglStoreMin As Single dim bolReset as boolean Dim msngBorderWidth As Single = 1.0! Dim rctMinMarker As Rectangle Dim rctMaxMarker As Rectangle Dim rctLevelBack As Rectangle Dim rctLevel As Rectangle dim intDecimals as integer Dim intMarginTop As Integer = 8 Dim intMarginLeft As Integer = 8 Dim intMarginBottom As Integer = 8 Dim intWidthLevel As Integer = 13 Dim intMarkerHeight As Integer = 6 dim colLevelBackground as Color = color.lightgray Dim colLevel As Color = color.limeGreen Dim colMinMarker As Color = color.Green dim colMaxMarker as Color = color.red Dim colMinMarkertextBox As Color = color.forestgreen Dim colMaxMarkertextBox As Color = color.red Dim colMarkertext As Color = color.white Dim penDrawingPen As New System.Drawing.Pen(System.Drawing.Color.Black, msngBorderWidth) Dim penDrawingBrush As New System.Drawing.solidbrush(System.Drawing.Color.lightgray) Dim drawFont As New Font("Arial", 8) Dim grfGraphics As System.Drawing.Graphics Public Sub New() Me.Size = New Size(200, 60) Me.SetStyle(ControlStyles.DoubleBuffer _ Or ControlStyles.UserPaint _ Or ControlStyles.AllPaintingInWmPaint, _ True) intZeroValue = CInt((Me.Height - intMarginTop - intMarginBottom)/(sglMax - sglMin)* (-sglMin)) Me.UpdateStyles() End Sub Public Property value() as single Get return sglValue End Get Set(ByVal Value As Single) setValues(csng(round(value,intdecimals))) End Set End Property Public Property Decimals() as integer Get return intDecimals End Get Set(ByVal Value As integer) if intdecimals >= 0 and intdecimals < 5 then intDecimals = value End Set End Property Public Property Min() as single Get return sglMin End Get Set(ByVal Value As single) If sglMin < sglMax Then sglMin = value setvalues(sglValue) resetmarkers() end if End Set End Property Public Property Max() as single Get return sglMax End Get Set(ByVal Value As single) if sglMax > sglMin then sglMax = value setvalues(sglValue) resetmarkers() end if End Set End Property Public Property BorderWidth() As Single Get Return msngBorderWidth End Get Set(ByVal Value As Single) If msngBorderWidth <> Value Then msngBorderWidth = Value Me.Invalidate() End If End Set End Property Public Property resetMinMax() As Boolean Get return bolReset End Get Set(ByVal value As Boolean) If value = true Then resetMarkers() End If End Set End Property Public Property colorLevel() as color Get return colLevel End Get Set(ByVal Value As color) colLevel = value Me.Invalidate() End Set End Property Public Property colorLevelBackground() as color Get return colLevelBackground End Get Set(ByVal Value As color) colLevelBackground = value Me.Invalidate() End Set End Property Public Property colorMinMarker() as color Get return colMinMarker End Get Set(ByVal Value As color) colMinMarker = value Me.Invalidate() End Set End Property Public Property colorMaxMarker() as color Get return colMaxMarker End Get Set(ByVal Value As color) colMaxMarker = value Me.Invalidate() End Set End Property Public Property colorMinMarkerTextBox() as color Get return colMinMarkerTextBox End Get Set(ByVal Value As color) colMinMarkerTextBox = value Me.Invalidate() End Set End Property Public Property colorMaxMarkerTextBox() as color Get return colMaxMarkerTextBox End Get Set(ByVal Value As color) colMaxMarkerTextBox = value Me.Invalidate() End Set End Property Public Property colorMarkerText() as color Get return colMarkerText End Get Set(ByVal Value As color) colMarkerText = value Me.Invalidate() End Set End Property Private Sub LevelMeter_Paint(ByVal sender As Object, ByVal pe As System.Windows.Forms.PaintEventArgs) Handles Me.Paint dim msr as sizeF grfGraphics = pe.Graphics 'Draw Level Background penDrawingBrush.Color = colLevelBackground rctLevelBack.X = intMarginLeft + 3 rctLevelBack.Y = intMarginTop rctLevelBack.Height = Me.Height - intMarginTop - intMarginBottom rctLevelBack.Width = intWidthLevel - 6 grfGraphics.fillRectangle(penDrawingBrush, rctLevelBack) 'Draw Level penDrawingBrush.Color = colLevel rctLevel.X = intMarginLeft + 3 If intValue < intZeroValue Then 'liegt der Nullpunkt im Skalenbereich? rctLevel.Y = Me.Height - intZeroValue - intMarginBottom rctLevel.Height = -intValue + intZeroValue Else rctLevel.Y = Me.Height - intMarginBottom - intValue rctLevel.Height = intValue - intZeroValue end if rctLevel.Width = intWidthLevel - 6 grfGraphics.fillRectangle(penDrawingBrush, rctLevel) 'Draw Zeroline if intzerovalue <> 0 then grfGraphics.DrawLine(penDrawingPen,intMarginleft -3,Me.Height - intZeroValue - intMarginBottom,intMarginleft + intWidthLevel + 3,Me.Height - intZeroValue - intMarginBottom) 'Draw Max Marker penDrawingBrush.Color = colMaxMarker rctMaxMarker.X = intMarginLeft rctMaxMarker.Y = Me.Height - intStoreMax - intMarginBottom - 3 rctMaxMarker.Width = intWidthLevel rctMaxMarker.Height = intMarkerHeight grfGraphics.fillEllipse(penDrawingBrush, rctMaxMarker) 'Draw Min Marker penDrawingBrush.Color = colMinMarker rctMinMarker.X = intMarginLeft rctMinMarker.Y = Me.Height - intStoreMin - intMarginBottom -3 rctMinMarker.Width = intWidthLevel rctMinMarker.Height = intMarkerHeight grfGraphics.fillEllipse(penDrawingBrush, rctMinMarker) 'Draw Min Box msr = grfGraphics.MeasureString(sglStoreMin.ToString, drawFont) penDrawingBrush.Color = colMinMarkertextBox grfGraphics.fillRectangle(penDrawingBrush,intMarginleft + intWidthLevel + 3,Me.Height - intStoreMin - intMarginBottom - cint(msr.Height/2), msr.width,msr.height) penDrawingBrush.Color = colMarkertext grfGraphics.DrawString(sglStoreMin.ToString,drawFont,penDrawingBrush,intMarginleft + intWidthLevel + 3,Me.Height - intStoreMin - intMarginBottom - cint(msr.Height/2)) 'Draw Max Box msr = grfGraphics.MeasureString(sglStoreMax.ToString, drawFont) penDrawingBrush.Color = colMaxMarkertextBox grfGraphics.fillRectangle(penDrawingBrush,intMarginleft + intWidthLevel + 3,Me.Height - intStoreMax - intMarginBottom - cint(msr.height/2), msr.width,msr.height) penDrawingBrush.Color = colMarkertext grfGraphics.DrawString(sglStoreMax.ToString,drawFont,penDrawingBrush, intMarginleft + intWidthLevel + 3,Me.Height - intStoreMax - intMarginBottom - cint(msr.height/2)) End Sub Private Sub Level_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize setvalues(sglValue) resetmarkers() Me.Invalidate() End Sub Private Sub setValues(byval value as single) If value < sglmin Then sglvalue = sglmin ElseIf value > sglmax Then sglvalue = sglmax End If sglValue = value intValue = CInt((Me.Height - intMarginTop - intMarginBottom)/(sglMax - sglMin)*(sglValue- sglMin)) If sglValue > sglStoreMax Then sglStoreMax = sglValue If sglValue < sglStoreMin Then sglStoreMin = sglValue If intValue > (Me.Height - intMarginTop - intMarginBottom) Then intValue = (Me.Height - intMarginTop - intMarginBottom) if intValue < 0 then intValue = 0 If intValue > intStoreMax Then intStoreMax = intValue If intValue < intStoreMin Then intStoreMin = intValue Me.Invalidate() End Sub Private Sub ResetMarkers() sglStoreMax = sglValue sglStoreMin = sglValue intStoreMax = intValue intStoreMin = intValue bolReset = False 'Der Nullpunkt des Balkens von unten in Pixel. intZeroValue = CInt((Me.Height - intMarginTop - intMarginBottom)/(sglMax - sglMin)* (-sglMin)) me.Invalidate() End Sub End Class