Click here to Skip to main content
Click here to Skip to main content

A fast and performing gauge

, 24 Aug 2012 Zlib
Rate this:
Please Sign up or sign in to vote.
This article shows you how to write a performing control using .NET 2.0 and GDI+.

Sample image


This code shows you how to build a fast and performing control using C# and .NET 2.0.

I wrote a similar control as an ActiveX once, using C++, ATL, and GDI, and wondered if it is possible to write performing code using .NET and GDI+. I needed it for another project. So I wrote this little control to show that it actually works.

How the code works

The code consists of a C# application and a custom control. The custom control really is the interesting part.

Deriving from Control

We derive from Control as this doesn't give us all these properties we don't actually need like a usercontrol would give us, for example.

public partial class AGauge : Control

Dealing with properties

Hiding, shadowing unwanted properties

Well, there are still properties that show up in the designer that are not necessary. In C#, you can use the new keyword to get rid of them (shadows in VB).

public new Boolean AllowDrop, AutoSize, ForeColor, ImeMode

Overriding useful properties

For properties that you want to use but with a different behaviour, you can use the override keyword (if overrideable) to tell the program to call this overridden property instead of the implementation of the base class, which in our case is the implementation in Control.

public override System.Drawing.Color BackColor..
public override System.Drawing.Font Font..
public override System.Windows.Forms.ImageLayout BackgroundImageLayout..

Custom properties

To be able to further customize the control in the designer, we need to add some properties of our own. E.g.,

System.ComponentModel.Description("The value.")]
public Single Value..

The Browsable attribute tells the designer to show the property in the toolbox or not. The Category attribute tells the designer where to show the property if the categorized view is selected, and the Description attribute adds a description to the property that the designer can show in the toolbox.

Events and Delegates

An event can carry additional information that is sent to the "listening" program, e.g., the form's event handler for this event.

Custom event arguments

We want the event to carry the number of the range the needle is in (if it changes from being in one range to being in another). To add some data to the event, we derive from the standard event args and add a variable which is initialized in the constructor. This will hold the extra information sent along.

public class ValueInRangeChangedEventArgs : EventArgs
 public Int32 valueInRange;
 public ValueInRangeChangedEventArgs(Int32 valueInRange)
  this.valueInRange = valueInRange;

Event delegate

The event handler "listening" for our event needs to be of a type that "understands" our event. With the delegate statement, we define this type.

public delegate void ValueInRangeChangedDelegate(Object sender, 
                     ValueInRangeChangedEventArgs e);

And the event

[Description("This event is raised if the value falls into a defined range.")]
public event ValueInRangeChangedDelegate ValueInRangeChanged;

The event is of the type we defined in the delegate statement. The Description attribute enables the designer to show a description for the event in the Toolbox.


The constructor is called when the control is created, e.g., before it will be shown in the designer. Here, we set the style of the control to enable double buffering. This isn't really necessary since we will do our own double buffering, but it doesn't hurt to do so.

public AGauge()
 SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

Overriding member functions

We need to override some of the member functions.

First, we override OnPaintBackground to ensure that the background is not painted each time the control is refreshed, this uses too much CPU even if double buffering is enabled. One drawback is that we need to handle the drawing of a background image ourselves, but this isn't too much of a problem.

protected override void OnPaintBackground(PaintEventArgs pevent)

If the control is resized, we need to refresh it. So we override OnResize.

protected override void OnResize(EventArgs e)
  drawGaugeBackground = true;

The global variable "drawGaugeBackground" is set to true to tell the control to completely redraw itself. Refresh forces the control to redraw, or if you like to call OnPaint, under the hood, a Windows message is sent, but this is a different story.

Finally, we need to override OnPaint to show some output to the user.

This is what our control really does, it shows the output to the user. It doesn't handle user input like a scrollbar would do. A scrollbar would override OnMouseMove, OnMouseDown, OnKeyPressed, and so on. OnPaint is the heart of our control.

protected override void OnPaint(PaintEventArgs pe)

OnPaint, which is called every time the control is redrawn, e.g., if the value of the gauge changed, determines if it should completely redraw itself or simply paint the background part with the performant function DrawImage. If the background hasn't changed, it only needs to draw the needle, thus avoiding costly GDI+ functions to be called every time. The background changes, e.g., if a property like a color has changed, or the control is resized, for example.


So it really is possible to write fast and performing controls with GDI+ if we use double buffering and blitting (DrawImage).

If you like VB better than C#, you can search for "SpeedyHMI" on SourceForge, this project I wrote contains this gauge written in VB.

Download, build, run and, Enjoy!


This article, along with any associated source code and files, is licensed under The zlib/libpng License


About the Author

Software Developer (Senior)
Germany Germany
Once upon a time there was a C64.
He was a very friendly bilingual fellow looking like a bread box..
His user could always wake him up in no time and ask him to LOAD "*",8,1
C64's friends in school were called CBM, PET and Schneider.
They were boring to talk to after a while so his user got him a girlfriend called Amiga and a business partner called IBM.
Since vitamins are good, his user also got him an Apple to chew on.
As time went by the C64 and his friends got old and eventually were buried in oblivion.
In good memory his user did light a fire for them with a Sparc.
Since then cloning became very popular and the descendants of the C64's business partner rule the scene of his former user.
The descendants did breed many multilingual children of all sizes and shapes and the C64's former user is living happily with them ever after.

Comments and Discussions

QuestionCool! Pinmemberuwe wilhelm19-Mar-14 23:03 
Generalusing the VB version PinmemberMember 103052067-Nov-13 8:42 
GeneralMy vote of 5 Pinmembercodemind9-Sep-13 12:37 
QuestionHow to use it in a MFC app? Pinmemberlanmanck5-Sep-13 16:29 
AnswerRe: How to use it in a MFC app? PinmemberA.J.Bauer8-Sep-13 21:55 
QuestionExcellent work. Thanks! Pinmembertbrendza2-Jul-13 12:32 
Questionhi,A.J.Bauer,how can this Integrate to c# [modified] Pinmemberbadhot30-May-13 16:47 
SuggestionHow to handle update of Range Start and End values PinmemberGlen Paetz29-May-13 13:49 
SuggestionRe: How to handle update of Range Start and End values PinmemberA.J.Bauer29-May-13 19:10 
QuestionIs it possible to get an image of a gauge. PinmemberClark French1-Apr-13 14:54 
SuggestionRe: Is it possible to get an image of a gauge. PinmemberA.J.Bauer1-Apr-13 21:04 
GeneralRe: Is it possible to get an image of a gauge. PinmemberClark French2-Apr-13 2:50 
GeneralMy vote of 5 Pinmemberjon124817-Feb-13 8:11 
GeneralExcellent control PinmemberTom Guidarelli1-Feb-13 7:51 
Questionhow can I add muti-Needle on one control ? [modified] Pinmemberxzzxg17-Dec-12 19:36 
QuestionProblem in using various Gauges Pinmembersaber_ordv918-Oct-12 13:13 
QuestionAGauge - small bug Pinmembermikerj3-Oct-12 0:40 
QuestionGauge Control in C++, ATL PinmemberK ARUN KUMAR28-Sep-12 7:29 
AnswerRe: Gauge Control in C++, ATL PinmemberA.J.Bauer1-Oct-12 3:16 
QuestionImprovement PinmemberCode Artist27-Aug-12 4:23 
AnswerRe: Improvement PinmemberA.J.Bauer27-Aug-12 4:35 
GeneralRe: Improvement PinmemberCode Artist27-Aug-12 4:41 
GeneralMy vote of 5 PinmemberChristian Amado24-Aug-12 17:43 
QuestionHow should I change Gauge type at design time ? Pinmemberd.barile24-Aug-12 7:04 
QuestionGreat Component PinmemberCode Artist24-Aug-12 4:35 
AnswerRe: Great Component [modified] PinmemberA.J.Bauer24-Aug-12 5:18 
GeneralRe: Great Component PinmemberCode Artist24-Aug-12 7:27 
QuestionGreat Piece of Work! PinmemberMember 30266636-Apr-12 7:21 
QuestionHow to drag your control to a Form? Pinmemberjunli202016-Mar-12 2:02 
SuggestionRe: How to drag your control to a Form? PinmemberA.J.Bauer16-Mar-12 2:12 
QuestionFull Screen Pinmemberchad77722-Sep-11 9:32 
GeneralFlip range numbers PinmemberMember 775283418-May-11 22:07 
GeneralRe: Flip range numbers PinmemberA.J.Bauer18-May-11 22:52 
GeneralRe: Flip range numbers PinmemberMember 775283419-May-11 0:31 
GeneralRe: Flip range numbers PinmemberMember 846906312-Dec-11 13:54 
GeneralRe: Flip range numbers PinmemberMember 846906313-Dec-11 7:49 
QuestionIs also C++ control available? PinmemberGilicek7-Apr-11 0:11 
AnswerRe: Is also C++ control available? PinmemberA.J.Bauer7-Apr-11 0:59 
GeneralRe: Is also C++ control available? PinmemberGilicek2-Jun-11 0:10 
GeneralWonderful Control PinmemberBeauGauge00115-Mar-11 6:28 
NewsNice control Pinmemberwolly8022-Dec-10 0:01 
QuestionThank you. Nice Component. Any Hint how to make the background transparent? Pinmembersoftvision8-Sep-10 1:51 
GeneralThank you Pinmembergruber3895-Sep-10 0:11 
GeneralVB Net version PinmemberJuwi_uk9-Aug-10 2:14 
GeneralRe: VB Net version Pinmemberrobjay30-Sep-10 15:04 
QuestionHow would I use the DLL in VB .NET to add a dial on my form? PinmemberZiaUrRehman12-Jul-10 20:48 
QuestionThese properties dont seem to work? PinmemberJuwi_uk23-Apr-10 12:27 
AnswerRe: These properties dont seem to work? PinmemberA.J.Bauer25-Apr-10 22:05 
GeneralRe: These properties dont seem to work? PinmemberJuwi_uk25-Apr-10 22:35 
GeneralRe: These properties dont seem to work? PinmemberA.J.Bauer25-Apr-10 22:59 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 24 Aug 2012
Article Copyright 2007 by A.J.Bauer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid