## Introduction

The code snippet is a demo of a pretty simple digital LCD without external bitmap. Look at the picture above. The current system time is just your computer's local time. Below is used to test. The color of digits can be changed by the following three sliders. The slider named `VALUE `

is to change the number of the middle LCD screen.To know more, please download the demo. I'd like and be very happy if it could help you, or if you could give some of your good new ideas.

## Background

The class was absolutely original. I created it today. So, you can use it in any way in any place.

## Using the Code

As I say just now, it's just a pretty simple class. To use it, we only add its files into your project and call `Init() `

method to accomplish the initialization. Let's take a look at the definition of the class:

class DigitalLCDClass
{
public:
void Clear();
BOOL SetText(char *cMsg);
void UpdateData(HDC hdc);
BOOL Init();
BOOL SetColor(COLORREF rgb);
BOOL SetPos(int xPos,int yPos);
BOOL SetItem(int iItem);
DigitalLCDClass();
virtual ~DigitalLCDClass();
private:
char *msg;
UINT m_nItem;
int m_nYPos;
int m_nXPos;
void DrawDigit(HDC hdc,int digit,int iPos,COLORREF col);
void Display(HDC hdc,char *cData);
COLORREF col;
};

I only took several hours to create it, so it is quite simple. I'll add more functions to it later. Use it as follows:

#include "DigitalLCDClass.h"
...
DigitalLCDClass dig;
...
dig.Init();
dig.SetColor(RGB(255,0,0));
dig.SetText("2009");
...
dig.UpdateData(hdc);

...

Okay! After that, let us focus on its principles:

In fact, I created another demo like this before. But, I used a bitmap to realize it last time. Even if it's better than this, it needs to read external files. The LCD digits on the bitmap are from 0 to 9. I only make a rectangle to copy it in proper time. This time, do not use PhotoShop to make bitmaps, only use some GDI functions.

How to draw a digit? As a matter of fact, it's really easy. Careful observation! The digit 8 is full lights on. Others are all part of digit 8. At the top, the digit 8 is a horizontal line of the uppermost part. The line I use is a rectangle to draw it. One big and one smaller.

Okay, now we could draw a digit 8 using a rectangle, use the API `FillRect()`

to finish it. How to display others? The question comes up! Think over, how many lights does digit 8 have? Seven! Yeah! If the digit 8 need to show, all the lights should be on. So, digit 1 only needs two lights on, at the bottom right and top right.

Digit 2: Top, top right, middle, bottom left and bottom are all lights on.

...

To realize this, I use this method:

void DigitalLCDClass::DrawDigit(HDC hdc, int digit,int iPos=0,COLORREF col=0)
{
RECT rt;
int offset=35;
HBRUSH hBrush;
hBrush=::CreateSolidBrush (col);
if(digit==2 || digit==3||digit==5||digit==0||digit==6||digit==7||digit==8||digit==9)
{
rt.left =m_nXPos+10+iPos*offset;rt.right =m_nXPos+32+iPos*offset;
rt.top =m_nYPos+10;rt.bottom =m_nYPos+12;
::FillRect (hdc,&rt,hBrush);
rt.left =m_nXPos+11+iPos*offset;rt.right =m_nXPos+31+iPos*offset;
rt.top =m_nYPos+11;rt.bottom =m_nYPos+13;
::FillRect (hdc,&rt,hBrush);
}
if(digit==2 || digit==3||digit==4||digit==5||digit==6||digit==8||digit==9)
{
rt.left =m_nXPos+10+iPos*offset;rt.right =m_nXPos+32+iPos*offset;
rt.top =m_nYPos+32;rt.bottom =m_nYPos+33;
::FillRect (hdc,&rt,hBrush);
rt.left =m_nXPos+11+iPos*offset;rt.right =m_nXPos+31+iPos*offset;
rt.top =m_nYPos+31;rt.bottom =m_nYPos+34;
::FillRect (hdc,&rt,hBrush);
}
if(digit==2 || digit==3||digit==5||digit==0||digit==6||digit==8||digit==9)
{
rt.left =m_nXPos+10+iPos*offset;rt.right =m_nXPos+32+iPos*offset;
rt.top =m_nYPos+54;rt.bottom =m_nYPos+56;
::FillRect (hdc,&rt,hBrush);
rt.left =m_nXPos+11+iPos*offset;rt.right =m_nXPos+31+iPos*offset;
rt.top =m_nYPos+53;rt.bottom =m_nYPos+54;
::FillRect (hdc,&rt,hBrush);
}
if(digit==4||digit==5||digit==0||digit==6||digit==8||digit==9)
{
rt.left =m_nXPos+9+iPos*offset;rt.right =m_nXPos+10+iPos*offset;
rt.top =m_nYPos+12;rt.bottom =m_nYPos+32;
::FillRect (hdc,&rt,hBrush);
rt.left =m_nXPos+8+iPos*offset;rt.right =m_nXPos+11+iPos*offset;
rt.top =m_nYPos+13;rt.bottom =m_nYPos+31;
::FillRect (hdc,&rt,hBrush);
}
if(digit==1||digit==2 || digit==3||digit==4||digit==0||digit==7||digit==8||digit==9)
{
rt.left =m_nXPos+9+23+iPos*offset;rt.right =m_nXPos+10+23+iPos*offset;
rt.top =m_nYPos+12;rt.bottom =m_nYPos+32;
::FillRect (hdc,&rt,hBrush);
rt.left =m_nXPos+8+23+iPos*offset;rt.right =m_nXPos+11+23+iPos*offset;
rt.top =m_nYPos+13;rt.bottom =m_nYPos+31;
::FillRect (hdc,&rt,hBrush);
}
if(digit==2||digit==0||digit==6||digit==8)
{
rt.left =m_nXPos+9+iPos*offset;rt.right =m_nXPos+10+iPos*offset;
rt.top =m_nYPos+12+22;rt.bottom =m_nYPos+32+22;
::FillRect (hdc,&rt,hBrush);
rt.left =m_nXPos+8+iPos*offset;rt.right =m_nXPos+11+iPos*offset;
rt.top =m_nYPos+13+22;rt.bottom =m_nYPos+31+22;
::FillRect (hdc,&rt,hBrush);
}
if(digit==1|| digit==3||digit==4||digit==5||digit==0||
digit==6||digit==7||digit==8||digit==9)
{
rt.left =m_nXPos+9+23+iPos*offset;rt.right =m_nXPos+10+23+iPos*offset;
rt.top =m_nYPos+12+22;rt.bottom =m_nYPos+32+22;
::FillRect (hdc,&rt,hBrush);
rt.left =m_nXPos+8+23+iPos*offset;rt.right =m_nXPos+11+23+iPos*offset;
rt.top =m_nYPos+13+22;rt.bottom =m_nYPos+31+22;
::FillRect (hdc,&rt,hBrush);
}
DeleteObject(hBrush);
}

Having finished digits drawing, get to draw background and light off state, use the method `Display()`

:

void DigitalLCDClass::Display(HDC hdc, char *cData)
{
RECT rt;
int nLength=(int)::strlen (cData);
if(nLength>m_nItem) nLength=m_nItem;
rt.left =m_nXPos+6;rt.right =m_nXPos+32+(m_nItem-1)*35+4;
rt.top =m_nYPos+8;rt.bottom =m_nYPos+58;
FillRect(hdc,&rt,(HBRUSH)GetStockObject(4));
for(int i=0;i<m_nItem;i++)
{
DrawDigit(hdc,8,i,RGB(55,55,55));
}
for(i=nLength-1;i>=0;i--)
DrawDigit(hdc,cData[i]-'0',(m_nItem-nLength)+i,col);
}

In order to display digits from right to left, when we call `DrawDigit()`

the third parameter must be `m_nItem-nLength+i`

. The third parameter decides the position (the item in LCD screen) that should be seen.

Okay! Thanks for your support! I'll work harder. Want to know more about this demo, then download the source code.

## History

I'll update it later.