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

A thermometer control

, 9 Jul 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Creating an analog-style thermometer

Screenshot - Thermometer_demo.jpg


This article demonstrates a complex analog-style thermometer control in C#. The control is in pure managed code. Most visual elements can be adjusted through the control's properties.


Due to the flexibility of the control, it can be used to display all sorts of data and not just degrees as used in this example.

Screenshot - Layouts.jpg


The control consists of 2 classes: ManometerBase and Thermometer.

Screenshot - architecture.jpg

The implementation is done in the Thermometer class.


All painting is done in the OnPaint method of the Thermometer class. Every element has its own Paint method and the methods are marked protected virtual.

protected override void OnPaint(PaintEventArgs e)
    // Set smoothingmode to AntiAlias
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    // Shadow
    // Background
    // Border
    // Inner shadow
    // Bars
    // Numbers
    // Paint the text(s)
    // Paint the Arrows
    // Reflex
    // Reset smoothingmode
    e.Graphics.SmoothingMode = SmoothingMode.Default;

Painting the numbers

Most methods are straightforward, but aligning the numbers in the ellipse is somewhat tricky.

protected virtual void PaintNumbers(Graphics g)
    double tmpAngle = StartAngle;
    for (double d = Min; d<= Max; d+= Interval)
        String text = Math.Round(d, Decimals).ToString();
        PointF p = 
            tmpAngle, MeasureText(g, text, Font, (int)numberRect.Width));
        if (ClockWise)
            tmpAngle -= NumberSpacing;
            tmpAngle += NumberSpacing;
            g.DrawString(text, Font, new SolidBrush(ForeColor), p);

The solution is to find the center point of the text and then calculate the offset based on the text's position in the ellipse and its dimensions.

private PointF CalcTextPosition(double a, SizeF size)
    PointF p = PointInEllipse(numberRect, a);
    p.X -= (float)((size.Width/2)*(1 + Math.Cos(Convert.ToRadians(a))));
    p.Y -= (float)((size.Height/2)*(1 - Math.Sin(Convert.ToRadians(a))));
    return p;

The private method CalcTextPosition makes a call to PointInEllipse. This method calculates a point in an ellipse based on the angle and the size of the ellipse, which is simple math.

private static PointF PointInEllipse(RectangleF rect , double angle)
    double r1 = rect.Width/2;
    double r2 = rect.Height/2;
    double x = 
        (float)(r1 * Math.Cos(Convert.ToRadians(angle))) + r1 + rect.X;
    double y = 
        -(float)(r2 * Math.Sin(Convert.ToRadians(angle))) + r2 + rect.Y;
    return new PointF((float)x, (float)y);

Using the code

To test the control, download the demo project and then build and run it. To change the appearance of the control, use the properties:

  • Interval - The interval between each number on the control; this is a floating point number.
  • Max - The maximum on the scale.
  • Min - The minimum on the scale.
  • StartAngle - The starting point in degrees.
  • StoreMax - Show the maximum stored value (red arrow).
  • StoredMax - The stored maximum.
  • TextDescription - Set the lower text displayed on the control.
  • TextUnit - Set the text to describe the Units used.
  • Value - The value and point of the arrow.
  • ArrowColor - The color of the arrow.
  • BackColor - The inner color of the control.
  • BarsBetweenNumbers - The number of lines between two displayed numbers. Note that the alignment of the lines is dependent on the properties NumberSpacing and Interval.
  • BorderWidth - The width of the outer border.
  • ClockWise - Lay the numbers out clockwise or counter-clockwise.
  • Decimals - The number of decimals displayed; default is 0.
  • NumberSpacing - The spacing between the numbers in degrees.


  • June 09, 2007: Initial release.
  • July 08, 2007:
    • Fixed missing dispose on some objects.
    • Fixed a bug where the designer would hang if the control was added from the toolbar.
    • Changed default text on Unit and Description property.
    • Added the layout section in this document.
    • Added a demo download.
    • Removed solution from source control.


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


About the Author

Niel M.Thomas
Software Developer (Senior)
Denmark Denmark
Name: Niel Morgan Thomas
Born: 1970 in Denmark
Dataengineer from Odense Technical University.
More than 20 years in IT-business.
Current employment:
Working with application development in a major Danish company that produce medical equipment.

Comments and Discussions

QuestionLicense PinmemberMember 1031127231-Dec-13 2:51 
AnswerRe: License PinmemberNiel M.Thomas31-Dec-13 3:15 
Generalvery very useful Pinmemberbatsword7-Nov-10 19:41 
GeneralRe: very very useful PinmemberNiel M.Thomas7-Nov-10 20:17 
GeneralRe: very very useful Pinmemberbatsword11-Nov-10 16:02 
GeneralMy vote of 4 PinmemberMember 366671421-Jul-10 2:39 
GeneralKudos, and more kudos Pinmemberjon_sigler30-Dec-08 17:42 
GeneralVisual Studio 6 PinmemberCodeHead27-Feb-08 4:13 
GeneralRe: Visual Studio 6 PinmemberThe Dogcow Farmer14-Sep-08 3:31 
GeneralPrograming style suggestions! PinmemberMartin#17-Jul-07 0:53 
AnswerRe: Programing style suggestions! PinmemberNiel M.Thomas18-Jul-07 2:06 
GeneralRe: Programing style suggestions! PinmemberMartin#18-Jul-07 2:13 
GeneralRe: Programing style suggestions! PinmemberMichal Brylka19-Jul-07 10:31 
GeneralMessage Removed PinmemberJani Mukkavaara24-Jun-08 3:32 
GeneralRe: Programing style suggestions! PinmvpMartin#24-Jun-08 8:04 
QuestionAm I doing something wrong? Pinmemberrob.iles29-Jun-07 12:36 
GeneralRe: Am I doing something wrong? Pinmemberrob.iles30-Jun-07 13:44 
QuestionVery cool, but... PinmemberThe Marshal20-Jun-07 20:28 
AnswerRe: Very cool, fixed PinmemberNiel M.Thomas20-Jun-07 21:43 
AnswerRe: Very cool, but... PinmemberKevin Gallagher21-Jun-07 7:03 
GeneralGood control Pinmembernitikin19-Jun-07 2:20 
GeneralGreat Control Pinmembermerlin98112-Jun-07 4:14 
Generalnice, but... PinmemberFZelle11-Jun-07 21:40 
GeneralRe: nice, but... PinmemberThe Marshal21-Jun-07 9:06 
GeneralRe: nice, but... PinmemberFZelle21-Jun-07 22:17 
GeneralRe: nice, but... Pinmembercraigg7517-Jul-07 4:59 
GeneralRe: nice, but... PinmemberFZelle17-Jul-07 5:30 
Generalkudos Pinmemberpita200011-Jun-07 17:00 
Generallovely Pinmemberbryce11-Jun-07 14:14 
GeneralCoool... PinmemberPaul Selormey11-Jun-07 13:29 

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.150414.1 | Last Updated 9 Jul 2007
Article Copyright 2007 by Niel M.Thomas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid