Click here to Skip to main content
15,867,330 members
Articles / Multimedia / GDI+
Article

Aqua Gauge

Rate me:
Please Sign up or sign in to vote.
4.93/5 (157 votes)
4 Sep 2007CPOL5 min read 524.2K   46.5K   388   106
A Gauge Control Developed using GDI+ with Aqua Skin
Screenshot - AquaGauge.gif

Introduction

I have chosen to develop this .NET user control to explore the easy yet powerful .NET GDI+. This simple gauge control developed using .NET 2.0 can cater to the entire range of monitoring purposes. Let's see how to develop such a glossy control using GDI+.

Overridden UserControl Methods

Normally, If we create user controls that have been fully drawn by the user, we should override the OnPaint and OnPaintBackground methods. Additionally, the control styles are to be set as appropriate. The following common styles can be set using this.SetStyle(ControlStyles.XXXXX, true/false);.

SupportsTransparentBackColorThis will enable your control to support a transparent backcolor if set to true.
ControlStyles.ResizeRedrawAllows repainting when the control is resized.
ControlStyles.AllPaintingInWmPaintIf true, the control ignores the window message WM_ERASEBKGND to reduce flicker.
This style should only be applied if the UserPaint bit is set to true.
ControlStyles.UserPaintIf true, the control paints itself rather than the operating system doing so.
ControlStyles.OptimizedDoubleBufferIf true, the control is first drawn to a buffer rather than directly to the screen, which can reduce flicker. If you set this property to true, you should also set the AllPaintingInWmPaint to true.

The OnPaint and OnPaintBackground methods will be called whenever the control needs to be repainted. For example, when the control is resized or the form is minimized and maximized, the OnPaint method will be called.

OnPaintBackground vs. OnPaint

OnPaintBackground paints the background (and thereby the shape) of the Window and is guaranteed to be fast. In contrast, OnPaint paints the details and might be slower because individual paint requests are combined into one Paint event that covers all areas that have to be redrawn. You might want to invoke the OnPaintBackground if, for instance, you want to draw a gradient-colored background for your control.

While OnPaintBackground has an event-like nomenclature and takes the same argument as the OnPaint method, OnPaintBackground is not a true event method. There is no PaintBackground event and OnPaintBackground does not invoke event delegates. When overriding the OnPaintBackground method, a derived class is not required to invoke the OnPaintBackground method of its base class.

Drawing the Guage Dial

First, let's see how to draw the dial. The dial requires a Scale, Threshold Indicator, some text and the current value to be displayed.

Drawing the scale requires calculating the positions for the rules that are to be drawn at the circumference. Let's say we need to draw a scale starting from 0 to 10 from angle 90 degrees to 270 degrees on the dial. In this case, the difference in the degrees (270-90 = 180) must be divided into 10 parts. To find the position for each part to be drawn, we need the following formula:

x = centerX + radius * cos(180/partNo)
y = centerY + radius * sin(180/partNo)

Note: when using Math.Cos or Math.Sin we should give angles in radians.

Circle Formula

After finding the position, we can draw any type of scale mark on the circumference. I have chosen to draw a line as a scale mark. Since the dial area is not going to be changed often, it can be drawn in OnPaintBackground overridden method.

Drawing the Pointer

The pointer may need to be repainted often. So, it is better to draw it in the OnPaint method. Finding the pointer position is the same as the logic for drawing the scale. The pointer can be drawn using graphicsObj.FillPolygon() method and it can be transformed to any angle that will represent the current value. Otherwise, the pointer can be redrawn for every change made for the current value.

Drawing the Glossiness

Drawing the glossiness is very simple. All you have to do is, after painting all the dial and pointer, fill two ellipses with gradient coloring. The LinearGradientBrush class provides the ability to draw gradient fills. Masking the gradient layer over the dial gives the glossiness as shown in the below figure.

Glosiness

Using the AquaGauge Control

This AquaGauge control can be used as any other user control provided by Windows. The following are the control-specific properties that can be used to configure this gauge to suit your requirements.

Property NameTypeDescription
DialColorColorGets or Sets the background color for the gauge.
DialTextStringGets or Sets the Text displayed on the gauge dial.
EnableTransparentBackgroundboolEnables or Disables Transparent Background color. Note: Enabling this will reduce the performance and may make the control flicker.
GlossinessfloatGets or Sets the strength of the Glossiness.
MaxValuefloatGets or Sets the maximum value shown on the gauge scale.
MinValuefloatGets or Sets the minimum value shown on the gauge scale.
NoOfDivisionsintGets or Sets the number of divisions on the gauge scale.
NoOfSubDivisionsintGets or Sets the number of subdivisions displayed on the scale for each division.
RecommendedValuefloatGets or Sets the recommended value on the scale. This will be used as a pivot point for drawing the threshold area.
ThresholdPercentfloatGets or Sets the Threshold area percentage on the scale.
ValuefloatGets or Sets the value to which the pointer will point.

Points of Interest

Whenever we draw images with lots of manipulations, it is recommended to draw it on an image object and then paint. For example, drawing the gauge dial requires lots of CPU-consuming operations. So, we can draw the dial onto an image and then draw using graphicsObj.DrawImage(). Whenever changes are made on the dial properties, we can recreate the image object. It would improve the performance.

History

  • Version 1.0 - Initial Version

All comments and suggestions are welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect BNY Mellon
India India
Ambalavanar, working as a .NET Solutions Architect at BNY Mellon (iNautix), Chennai. Enjoys designing and developing UI & highly scalable apps.

Comments and Discussions

 
QuestionHow to make it as OCX or DLL file? Pin
Member 150848553-Mar-21 7:39
Member 150848553-Mar-21 7:39 
QuestionCan I use this gauge for c# winforms Pin
ezgii25-Feb-21 23:27
ezgii25-Feb-21 23:27 
QuestionGreat control. How do I scale Can the Gauge DialText when the control gets Re-sized? Pin
Tim8w19-Jan-21 3:35
Tim8w19-Jan-21 3:35 
GeneralSize Change Pin
Sven So.13-Sep-17 4:19
Sven So.13-Sep-17 4:19 
PraiseVisualStudio Community 2017 & C# .Net Framework 4 Pin
coarist21-Jun-17 7:11
coarist21-Jun-17 7:11 
Questionusing two gauge at the same time Pin
farshadsomayehee16-May-17 19:49
farshadsomayehee16-May-17 19:49 
QuestionTest bench Pin
farshadsomayehee13-May-17 5:57
farshadsomayehee13-May-17 5:57 
QuestionAquaGauge Pin
panicni17-Oct-16 21:47
panicni17-Oct-16 21:47 
QuestionDo you have similar control that I can use in ASP.net? Pin
Member 58128513-Jul-16 17:13
Member 58128513-Jul-16 17:13 
GeneralMy vote of 5 Pin
Dieter Vander Donckt22-Oct-15 22:23
Dieter Vander Donckt22-Oct-15 22:23 
QuestionDemo project PinPopular
assismauroSJC29-Apr-15 8:21
assismauroSJC29-Apr-15 8:21 
GeneralMy vote of 5 Pin
Gun Gun Febrianza16-Nov-14 22:40
Gun Gun Febrianza16-Nov-14 22:40 
GeneralRe: My vote of 5 Pin
panicni17-Oct-16 22:12
panicni17-Oct-16 22:12 
QuestionLicence Issue Pin
Sulman Bin Khurshid12-Nov-14 8:27
Sulman Bin Khurshid12-Nov-14 8:27 
QuestionPlease upload your test bench project too Pin
YDLU20-Aug-14 10:28
YDLU20-Aug-14 10:28 
Questiondigital value Pin
Member 912643918-Aug-14 4:45
Member 912643918-Aug-14 4:45 
AnswerRe: digital value Pin
Dieter Vander Donckt22-Oct-15 23:34
Dieter Vander Donckt22-Oct-15 23:34 
GeneralRe: digital value Pin
Member 1273596216-Jan-18 5:55
Member 1273596216-Jan-18 5:55 
BugSize problem when control added to VB.Net Windows Form App Pin
Member 1048922610-Apr-14 5:22
Member 1048922610-Apr-14 5:22 
GeneralRe: Size problem when control added to VB.Net Windows Form App Pin
Dieter Vander Donckt22-Oct-15 22:21
Dieter Vander Donckt22-Oct-15 22:21 
GeneralRe: Size problem when control added to VB.Net Windows Form App Pin
MrGreen812-Apr-17 10:16
MrGreen812-Apr-17 10:16 
GeneralRe: Size problem when control added to VB.Net Windows Form App Pin
theskiguy19-Jul-17 9:24
theskiguy19-Jul-17 9:24 
QuestionDon't know how to use it PinPopular
Jason.LYJ8-Apr-14 4:14
professionalJason.LYJ8-Apr-14 4:14 
SuggestionNice component, globalization of digital decimal point Pin
Member 25129777-Jan-14 10:05
Member 25129777-Jan-14 10:05 
Hi, very nice component man.
I made all the adjustments suggested and it works perfectly.
I also made another improvement and like to share it.
The digital display causes a Format exception because my computer are not set to english culture and uses comma as decimal separator, which is not handled by the routine.
I changes the following lines to make the code work even with another decimal separator.

In the function DisplayNumber I changed the line:
C#
if (i < chars.Length - 1 && chars[i + 1] == '.')

by
C#
if (i < chars.Length - 1 && chars[i + 1].ToString() == System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator)


and in the same function I also changed the line:
C#
if (c != '.')

by
C#
if(c.ToString() != System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator )


That made the code work in my language (portuguese).
GeneralMy vote of 5 Pin
cesarms895-Apr-13 6:35
cesarms895-Apr-13 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.