Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Simple Digital LCD Demo

0.00/5 (No votes)
28 Dec 2009 1  
Just a simple digital LCD class and do not use an external bitmap.You can use and change it.
DigitalLCDdemo

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();                	//Clear the screen of LCD
 BOOL SetText(char *cMsg);	 	//This method is used to set the digits 
				//the LCD should to display
 void UpdateData(HDC hdc);   	//We could do this in OnPaint or use to a timer.
 BOOL Init();		  	//Accomplish the initialization.
 BOOL SetColor(COLORREF rgb); 	//Set the color of the digits
 BOOL SetPos(int xPos,int yPos); 	//Set the position of screen
 BOOL SetItem(int iItem);      	//Set the maximum digits the LCD can be display.

 DigitalLCDClass();      		//Construction of the class.
 virtual ~DigitalLCDClass();  

private:
 char *msg;             	//A buffer to store the text of digits
 UINT m_nItem;          	//maximum of digits can be display, changed by SetItem()
 int m_nYPos;		//Position of LCD screen
 int m_nXPos;
 void DrawDigit(HDC hdc,int digit,int iPos,COLORREF col);  	//These functions used 
							//to draw digits
 void Display(HDC hdc,char *cData);	//We will talk about it later in details.
  COLORREF col;			//Color of digits.
};

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); //Create a brush to fill the rectangle.

//Here: 2,3,5,0,6,7,8,9 need the top light on.  Whether to the top line .

   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);//Top 
   }

//Whether to draw middle line.
   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);//middle
   }

   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);//bottom
   }

   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);//top left
   }

   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);//top right   
   }

   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);//bottom left
   }
   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);//bottom right
   }
   
   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)); //Light off, we do here, like this.
  
  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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here