This article describes an approach to constructing a cool segmented LED displayer that is similar to the LEDs used in alarm clocks and industry monitors. This control can be used to display time, numerical values and even some English characters. I hope it will be helpful in programs such as hardware simulators.
In this section, I would like to give a brief introduction to the ideas behind this control. In a 7-segment LED displayer, one character is composed of at most 7 segments, as in the picture shown below.
Figure 1: Segmented character
Each segment has the following properties: the width of the segment, the bevel rate of the corners in the segment and the interval between segments.
Figure 2: Properties of each segment
If we mark those 7 segments with numbers, as the picture shows in Figure 1, then we can use indices to represent different characters. For example, the character "1" can be represented as (2, 3) and the character "5" can be represented as (1, 6, 7, 3, 4). To draw the whole scene, we should first calculate the bound rectangles where a certain character is drawn. Then the segments in each specified bound rectangle should be drawn. The algorithm for drawing a 7-segmented LED character in a specified bound rectangle is shown in Figure 3.
Figure 3: Drawing one character
Using the code
This control is quite easy to use. Just drag it from the toolbox to your form in design mode. Then adjust its provided properties in the designer or at runtime to modify its appearance. The properties are listed in the table below:
set the width of the border around the control.
set the color of the border around the control.
set the opaque value of the highlight.
0 implies that the highlight will end transparent, while
100 implies that the highlight will keep its transparency from the beginning.
set a value indicating whether to show the highlight area on the control. If it is set to
false, the value of the
HighlightOpaque property is ignored.
set the corner radius for the control. If
false, the value of this property is ignored. The valid value is from
set a value indicating whether the background was filled in gradient colors.
set the first background color. If
true, this color will be the color at the top of the background rectangle. Otherwise, this color will be the solid background color.
set the second background color. If
true, this color will be the color at the bottom of the background rectangle. Otherwise, this color will be ignored.
set the border style. If it is
true, the border of the control will be a round rectangle. Otherwise, the border is a normal rectangle.
set the segment-interval ratio. The larger this value is, the wider the gaps between segments are.
s<code>et the alignment style of the text.
set the segment-width ratio. The larger this value is, the wider the segments are.
set the total number of characters to display. If the number of characters contained in the displaying text is larger than this value, the displaying character will be truncated.
set the bevel rate of each segment. Please refer to Figure 2 for further information.
set the color of faded background characters.
set the text of the control.
Points of Interest
A control at presentation level may do lots of painting. Most of the time, the control has to be repainted when one of its properties is changed. This may cause multiple invalidating if several properties of the control are changed in quick succession. In order to avoid multiple painting, we can implement the
ISupportInitialize interface in our control. First, we set a member variable in the
private bool m_bIsInitializing = false;
m_bIsInitializing = true;
Then we rewrite our properties in the following manner:
public Color FadedColor
if (m_colFadedColor == value)
m_colFadedColor = value;
At last, in the
EndInit() method, we turn off the initialization mode and repaint the control.
m_bIsInitializing = false;
Now we can use batch initialization to prevent successive painting of the control.
mycontrol.BackColor = System.Drawing.Color.Transparent;
mycontrol.BackColor_1 = System.Drawing.Color.Black;
mycontrol.BackColor_2 = System.Drawing.Color.Transparent;
mycontrol.BevelRate = 0.5F;
mycontrol.BorderColor = System.Drawing.Color.White;
mycontrol.FadedColor = System.Drawing.Color.Black;
- 15th July, 2007 -- Original version posted
- 20th July, 2007 -- Downloads updated
- 7th May, 2008 -- Downloads updated
- User can now turn on smooth drawing mode to make the control look better
- Italic text style supported
- Bug fixed: The bottom and right bounds are invisible if the bound rectangle is a regular one
- 12th May, 2008 -- Downloads updated
- The user can copy text on the control when the control got focus.
- The color of border will change (Defined by user) when the control got and lost focus.
- Bug fixed: the anti-alias effect will disappear when the bound is a regulare rect.