Level mit MIN-MAX Steuerelement

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

 

Flash is required!
  1. '
  2. Option Strict On
  3. Imports System.ComponentModel
  4. Imports System.Math
  5.  
  6. Public Partial Class LevelMeter
  7. Inherits System.Windows.Forms.UserControl
  8.  
  9. Dim sglValue As Single = 0
  10. dim intValue as integer
  11. Dim sglMax As Single = 100
  12. Dim sglMin As Single = 0
  13. dim intZeroValue as integer
  14.  
  15. Dim intStoreMax As Integer
  16. dim sglStoreMax as single
  17. Dim intStoreMin As Integer
  18. Dim sglStoreMin As Single
  19.  
  20. dim bolReset as boolean
  21.  
  22. Dim msngBorderWidth As Single = 1.0!
  23.  
  24. Dim rctMinMarker As Rectangle
  25. Dim rctMaxMarker As Rectangle
  26. Dim rctLevelBack As Rectangle
  27. Dim rctLevel As Rectangle
  28. dim intDecimals as integer
  29.  
  30. Dim intMarginTop As Integer = 8
  31. Dim intMarginLeft As Integer = 8
  32. Dim intMarginBottom As Integer = 8
  33. Dim intWidthLevel As Integer = 13
  34. Dim intMarkerHeight As Integer = 6
  35.  
  36. dim colLevelBackground as Color = color.lightgray
  37. Dim colLevel As Color = color.limeGreen
  38. Dim colMinMarker As Color = color.Green
  39. dim colMaxMarker as Color = color.red
  40. Dim colMinMarkertextBox As Color = color.forestgreen
  41. Dim colMaxMarkertextBox As Color = color.red
  42. Dim colMarkertext As Color = color.white
  43.  
  44. Dim penDrawingPen As New System.Drawing.Pen(System.Drawing.Color.Black, msngBorderWidth)
  45. Dim penDrawingBrush As New System.Drawing.solidbrush(System.Drawing.Color.lightgray)
  46. Dim drawFont As New Font("Arial", 8)
  47. Dim grfGraphics As System.Drawing.Graphics
  48.  
  49. Public Sub New()
  50. Me.Size = New Size(200, 60)
  51. Me.SetStyle(ControlStyles.DoubleBuffer _
  52. Or ControlStyles.UserPaint _
  53. Or ControlStyles.AllPaintingInWmPaint, _
  54. True)
  55. intZeroValue = CInt((Me.Height - intMarginTop - intMarginBottom)/(sglMax - sglMin)* (-sglMin))
  56. Me.UpdateStyles()
  57. End Sub
  58.  
  59. Public Property value() as single
  60. Get
  61. return sglValue
  62. End Get
  63. Set(ByVal Value As Single)
  64. setValues(csng(round(value,intdecimals)))
  65. End Set
  66. End Property
  67. Public Property Decimals() as integer
  68. Get
  69. return intDecimals
  70. End Get
  71. Set(ByVal Value As integer)
  72. if intdecimals >= 0 and intdecimals < 5 then intDecimals = value
  73. End Set
  74. End Property
  75. Public Property Min() as single
  76. Get
  77. return sglMin
  78. End Get
  79. Set(ByVal Value As single)
  80. If sglMin < sglMax Then
  81. sglMin = value
  82. setvalues(sglValue)
  83. resetmarkers()
  84. end if
  85. End Set
  86. End Property
  87. Public Property Max() as single
  88. Get
  89. return sglMax
  90. End Get
  91. Set(ByVal Value As single)
  92. if sglMax > sglMin then
  93. sglMax = value
  94. setvalues(sglValue)
  95. resetmarkers()
  96. end if
  97. End Set
  98. End Property
  99.  
  100. Public Property BorderWidth() As Single
  101. Get
  102. Return msngBorderWidth
  103. End Get
  104. Set(ByVal Value As Single)
  105. If msngBorderWidth <> Value Then
  106. msngBorderWidth = Value
  107. Me.Invalidate()
  108. End If
  109. End Set
  110. End Property
  111.  
  112. Public Property resetMinMax() As Boolean
  113. Get
  114. return bolReset
  115. End Get
  116. Set(ByVal value As Boolean)
  117. If value = true Then
  118. resetMarkers()
  119. End If
  120. End Set
  121. End Property
  122. Public Property colorLevel() as color
  123. Get
  124. return colLevel
  125. End Get
  126. Set(ByVal Value As color)
  127. colLevel = value
  128. Me.Invalidate()
  129. End Set
  130. End Property
  131. Public Property colorLevelBackground() as color
  132. Get
  133. return colLevelBackground
  134. End Get
  135. Set(ByVal Value As color)
  136. colLevelBackground = value
  137. Me.Invalidate()
  138. End Set
  139. End Property
  140. Public Property colorMinMarker() as color
  141. Get
  142. return colMinMarker
  143. End Get
  144. Set(ByVal Value As color)
  145. colMinMarker = value
  146. Me.Invalidate()
  147. End Set
  148. End Property
  149. Public Property colorMaxMarker() as color
  150. Get
  151. return colMaxMarker
  152. End Get
  153. Set(ByVal Value As color)
  154. colMaxMarker = value
  155. Me.Invalidate()
  156. End Set
  157. End Property
  158. Public Property colorMinMarkerTextBox() as color
  159. Get
  160. return colMinMarkerTextBox
  161. End Get
  162. Set(ByVal Value As color)
  163. colMinMarkerTextBox = value
  164. Me.Invalidate()
  165. End Set
  166. End Property
  167. Public Property colorMaxMarkerTextBox() as color
  168. Get
  169. return colMaxMarkerTextBox
  170. End Get
  171. Set(ByVal Value As color)
  172. colMaxMarkerTextBox = value
  173. Me.Invalidate()
  174. End Set
  175. End Property
  176. Public Property colorMarkerText() as color
  177. Get
  178. return colMarkerText
  179. End Get
  180. Set(ByVal Value As color)
  181. colMarkerText = value
  182. Me.Invalidate()
  183. End Set
  184. End Property
  185.  
  186. Private Sub LevelMeter_Paint(ByVal sender As Object, ByVal pe As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
  187.  
  188. dim msr as sizeF
  189. grfGraphics = pe.Graphics
  190.  
  191. 'Draw Level Background
  192. penDrawingBrush.Color = colLevelBackground
  193. rctLevelBack.X = intMarginLeft + 3
  194. rctLevelBack.Y = intMarginTop
  195. rctLevelBack.Height = Me.Height - intMarginTop - intMarginBottom
  196. rctLevelBack.Width = intWidthLevel - 6
  197. grfGraphics.fillRectangle(penDrawingBrush, rctLevelBack)
  198.  
  199. 'Draw Level
  200. penDrawingBrush.Color = colLevel
  201. rctLevel.X = intMarginLeft + 3
  202. If intValue < intZeroValue Then 'liegt der Nullpunkt im Skalenbereich?
  203. rctLevel.Y = Me.Height - intZeroValue - intMarginBottom
  204. rctLevel.Height = -intValue + intZeroValue
  205. Else
  206. rctLevel.Y = Me.Height - intMarginBottom - intValue
  207. rctLevel.Height = intValue - intZeroValue
  208. end if
  209.  
  210. rctLevel.Width = intWidthLevel - 6
  211. grfGraphics.fillRectangle(penDrawingBrush, rctLevel)
  212. 'Draw Zeroline
  213. if intzerovalue <> 0 then grfGraphics.DrawLine(penDrawingPen,intMarginleft -3,Me.Height - intZeroValue - intMarginBottom,intMarginleft + intWidthLevel + 3,Me.Height - intZeroValue - intMarginBottom)
  214.  
  215. 'Draw Max Marker
  216. penDrawingBrush.Color = colMaxMarker
  217. rctMaxMarker.X = intMarginLeft
  218. rctMaxMarker.Y = Me.Height - intStoreMax - intMarginBottom - 3
  219. rctMaxMarker.Width = intWidthLevel
  220. rctMaxMarker.Height = intMarkerHeight
  221. grfGraphics.fillEllipse(penDrawingBrush, rctMaxMarker)
  222.  
  223. 'Draw Min Marker
  224. penDrawingBrush.Color = colMinMarker
  225. rctMinMarker.X = intMarginLeft
  226. rctMinMarker.Y = Me.Height - intStoreMin - intMarginBottom -3
  227. rctMinMarker.Width = intWidthLevel
  228. rctMinMarker.Height = intMarkerHeight
  229. grfGraphics.fillEllipse(penDrawingBrush, rctMinMarker)
  230.  
  231. 'Draw Min Box
  232. msr = grfGraphics.MeasureString(sglStoreMin.ToString, drawFont)
  233. penDrawingBrush.Color = colMinMarkertextBox
  234. grfGraphics.fillRectangle(penDrawingBrush,intMarginleft + intWidthLevel + 3,Me.Height - intStoreMin - intMarginBottom - cint(msr.Height/2), msr.width,msr.height)
  235. penDrawingBrush.Color = colMarkertext
  236. grfGraphics.DrawString(sglStoreMin.ToString,drawFont,penDrawingBrush,intMarginleft + intWidthLevel + 3,Me.Height - intStoreMin - intMarginBottom - cint(msr.Height/2))
  237.  
  238. 'Draw Max Box
  239. msr = grfGraphics.MeasureString(sglStoreMax.ToString, drawFont)
  240. penDrawingBrush.Color = colMaxMarkertextBox
  241. grfGraphics.fillRectangle(penDrawingBrush,intMarginleft + intWidthLevel + 3,Me.Height - intStoreMax - intMarginBottom - cint(msr.height/2), msr.width,msr.height)
  242. penDrawingBrush.Color = colMarkertext
  243. grfGraphics.DrawString(sglStoreMax.ToString,drawFont,penDrawingBrush, intMarginleft + intWidthLevel + 3,Me.Height - intStoreMax - intMarginBottom - cint(msr.height/2))
  244. End Sub
  245. Private Sub Level_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
  246. setvalues(sglValue)
  247. resetmarkers()
  248. Me.Invalidate()
  249. End Sub
  250.  
  251. Private Sub setValues(byval value as single)
  252. If value < sglmin Then
  253. sglvalue = sglmin
  254. ElseIf value > sglmax Then
  255. sglvalue = sglmax
  256. End If
  257. sglValue = value
  258. intValue = CInt((Me.Height - intMarginTop - intMarginBottom)/(sglMax - sglMin)*(sglValue- sglMin))
  259. If sglValue > sglStoreMax Then sglStoreMax = sglValue
  260. If sglValue < sglStoreMin Then sglStoreMin = sglValue
  261. If intValue > (Me.Height - intMarginTop - intMarginBottom) Then intValue = (Me.Height - intMarginTop - intMarginBottom)
  262. if intValue < 0 then intValue = 0
  263. If intValue > intStoreMax Then intStoreMax = intValue
  264. If intValue < intStoreMin Then intStoreMin = intValue
  265. Me.Invalidate()
  266. End Sub
  267. Private Sub ResetMarkers()
  268. sglStoreMax = sglValue
  269. sglStoreMin = sglValue
  270. intStoreMax = intValue
  271. intStoreMin = intValue
  272. bolReset = False
  273. 'Der Nullpunkt des Balkens von unten in Pixel.
  274. intZeroValue = CInt((Me.Height - intMarginTop - intMarginBottom)/(sglMax - sglMin)* (-sglMin))
  275. me.Invalidate()
  276. End Sub
  277. End Class