Click here to Skip to main content
11,924,280 members (64,370 online)
Click here to Skip to main content
Add your own
alternative version


397 bookmarked

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.

You may also be interested in...

Comments and Discussions

GeneralRe: Version for C++ Pin
A.J.Bauer30-Jan-09 1:39
memberA.J.Bauer30-Jan-09 1:39 
GeneralGauge Graph Pin
Atul Shriram rane12-Dec-08 1:37
memberAtul Shriram rane12-Dec-08 1:37 
QuestionWeb Pin
Fradique Lee11-Nov-08 14:58
memberFradique Lee11-Nov-08 14:58 
GeneralA gradient into the the range bloc color Pin
zitun15-Oct-08 3:48
memberzitun15-Oct-08 3:48 
Generalsome bugs Pin
Martin Radu14-Sep-08 1:49
memberMartin Radu14-Sep-08 1:49 
GeneralGood job!!! Pin
Martin Radu12-Sep-08 1:12
memberMartin Radu12-Sep-08 1:12 
Generalcopy to clipboard in context menu Pin
JulienV21-Apr-08 21:58
memberJulienV21-Apr-08 21:58 
GeneralRe: copy to clipboard in context menu Pin
A.J.Bauer22-Apr-08 22:37
memberA.J.Bauer22-Apr-08 22:37 
QuestionNice efforts, but have some issues with tweaking the gauge Pin
Salam Y. ELIAS17-Apr-08 10:27
memberSalam Y. ELIAS17-Apr-08 10:27 
GeneralRe: Nice efforts, but have some issues with tweaking the gauge Pin
Salam Y. ELIAS17-Apr-08 10:35
memberSalam Y. ELIAS17-Apr-08 10:35 
GeneralGreat Work Pin
mail@martin-hagenow.de15-Apr-08 7:18
membermail@martin-hagenow.de15-Apr-08 7:18 
GeneralCongratulations Pin
kjqua20-Jan-08 4:42
memberkjqua20-Jan-08 4:42 
QuestionShow region's limits in range Pin
JulienV14-Nov-07 1:14
memberJulienV14-Nov-07 1:14 
GeneralSupport for the control Pin
A.J.Bauer28-Oct-07 21:46
memberA.J.Bauer28-Oct-07 21:46 
Hi there all,

I see that there are questions about the control from time to time.
Guys, this code is provided as is, it is supposed to be educational at most..
If I wanted to write a full blown control I would have invested more time and would have tried to sell it too.
The focus was to show that you can build performing code with .NET thats all.
If you have problems with it dig into the code and try to figure it out yourselves!

Hope you understand and some thanks are always welcome.

Cheers mates and have fun with it.Smile | :)

Don't Panic, debug it!

GeneralBaseArcSweep [modified] Pin
c0d3Dr4g0n5-Oct-07 19:54
memberc0d3Dr4g0n5-Oct-07 19:54 
GeneralRe: BaseArcSweep Pin
Ross Presser22-Apr-08 13:06
memberRoss Presser22-Apr-08 13:06 
GeneralProblem with ScaleLinesMajorStepValue [modified] Pin
XsiXmaX27-Sep-07 22:39
memberXsiXmaX27-Sep-07 22:39 
GeneralTransparency and Captions color Pin
Sebastian Ledesma26-Sep-07 10:44
memberSebastian Ledesma26-Sep-07 10:44 
GeneralPocket PC porting Pin
Mark7016-Sep-07 23:11
memberMark7016-Sep-07 23:11 
QuestionReg New RangeIdx Pin
praveenkumar palla22-Aug-07 4:09
memberpraveenkumar palla22-Aug-07 4:09 
GeneralRegarding range visible Pin
praveenkumar palla11-Aug-07 4:16
memberpraveenkumar palla11-Aug-07 4:16 
GeneralRe: Regarding range visible Pin
A.J.Bauer12-Aug-07 1:21
memberA.J.Bauer12-Aug-07 1:21 
GeneralRe: Regarding range visible [modified] Pin
praveenkumar palla13-Aug-07 1:55
memberpraveenkumar palla13-Aug-07 1:55 
GeneralRe: Regarding range visible Pin
A.J.Bauer13-Aug-07 4:40
memberA.J.Bauer13-Aug-07 4:40 
GeneralRe: Regarding range visible [modified] Pin
praveenkumar palla13-Aug-07 20:47
memberpraveenkumar palla13-Aug-07 20:47 
GeneralRe: Regarding range visible Pin
A.J.Bauer13-Aug-07 22:02
memberA.J.Bauer13-Aug-07 22:02 
GeneralRe: Regarding range visible Pin
praveenkumar palla13-Aug-07 22:33
memberpraveenkumar palla13-Aug-07 22:33 
GeneralRe: Regarding range visible Pin
A.J.Bauer13-Aug-07 23:15
memberA.J.Bauer13-Aug-07 23:15 
GeneralRe: Regarding range visible Pin
praveenkumar palla13-Aug-07 23:29
memberpraveenkumar palla13-Aug-07 23:29 
GeneralVery cool! Pin
Bit-Smacker9-Aug-07 9:43
memberBit-Smacker9-Aug-07 9:43 
GeneralRe: Very cool! Pin
A.J.Bauer10-Aug-07 0:23
memberA.J.Bauer10-Aug-07 0:23 
QuestionRegarding Gauge's scaleLinesMajorStepValue property [modified] Pin
praveenkumar palla26-Jul-07 1:04
memberpraveenkumar palla26-Jul-07 1:04 
AnswerRe: Regarding Gauge's scaleLinesMajorStepValue property Pin
A.J.Bauer2-Aug-07 22:30
memberA.J.Bauer2-Aug-07 22:30 
GeneralThanks Pin
ecain23-Jul-07 15:33
memberecain23-Jul-07 15:33 
GeneralRe: Thanks Pin
A.J.Bauer2-Aug-07 22:31
memberA.J.Bauer2-Aug-07 22:31 
GeneralMultiple needle version Pin
FrontBottom28-Jun-07 15:26
memberFrontBottom28-Jun-07 15:26 
GeneralRe: Multiple needle version Pin
A.J.Bauer2-Aug-07 22:37
memberA.J.Bauer2-Aug-07 22:37 
GeneralRe: Multiple needle version [modified] Pin
praveenkumar palla13-Aug-07 23:38
memberpraveenkumar palla13-Aug-07 23:38 
GeneralRe: Multiple needle version Pin
ch_cu220-Feb-08 5:11
memberch_cu220-Feb-08 5:11 
GeneralRe: Multiple needle version Pin
Indianz200610-Apr-08 0:25
memberIndianz200610-Apr-08 0:25 
GeneralRe: Multiple needle version Pin
Juwi_uk27-May-10 12:19
memberJuwi_uk27-May-10 12:19 
GeneralRe: Multiple needle version Pin
xzzxg17-Dec-12 20:07
memberxzzxg17-Dec-12 20:07 
GeneralSome jackass is selling your code for $100 Pin
Michael Chapman28-Jun-07 2:05
memberMichael Chapman28-Jun-07 2:05 
GeneralRe: Some jackass is selling your code for $100 Pin
FrontBottom28-Jun-07 4:22
memberFrontBottom28-Jun-07 4:22 
GeneralRe: Some jackass is selling your code for $100 Pin
A.J.Bauer28-Jun-07 7:44
memberA.J.Bauer28-Jun-07 7:44 
GeneralRe: Some jackass is selling your code for $100 Pin
Michael Chapman28-Jun-07 10:35
memberMichael Chapman28-Jun-07 10:35 
GeneralConverted to Pin
Kevin Cary-Grimm1-Jun-07 10:21
memberKevin Cary-Grimm1-Jun-07 10:21 
GeneralRe: Converted to Pin
A.J.Bauer4-Jun-07 1:13
memberA.J.Bauer4-Jun-07 1:13 
GeneralRe: Converted to Pin
Kevin Cary-Grimm4-Jun-07 9:30
memberKevin Cary-Grimm4-Jun-07 9:30 
GeneralRe: Converted to Pin
A.J.Bauer6-Jun-07 0:07
memberA.J.Bauer6-Jun-07 0:07 

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.

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