Click here to Skip to main content
15,867,330 members
Articles / Multimedia / GDI

A Simple Digital LCD Demo

Rate me:
Please Sign up or sign in to vote.
4.36/5 (12 votes)
28 Dec 2009CPOL2 min read 50.6K   2.2K   39   17
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:

C++
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:

C++
#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:

C++
 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():

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
China China
Secret..

Comments and Discussions

 
Generalthanks Pin
futurejo3-Nov-10 2:46
futurejo3-Nov-10 2:46 
GeneralRe: thanks Pin
Aric Wang10-Feb-11 19:42
Aric Wang10-Feb-11 19:42 
GeneralSome comments Pin
mluri4-Jan-10 6:49
mluri4-Jan-10 6:49 
GeneralRe: Some comments Pin
Aric Wang4-Jan-10 14:15
Aric Wang4-Jan-10 14:15 
GeneralRe: Some comments Pin
Aric Wang4-Jan-10 14:33
Aric Wang4-Jan-10 14:33 
GeneralRe: Some comments Pin
Rick York3-Aug-10 7:06
mveRick York3-Aug-10 7:06 
GeneralMy vote of 5 Pin
Bill SerGio, The Infomercial King2-Jan-10 15:57
Bill SerGio, The Infomercial King2-Jan-10 15:57 
GeneralRe: My vote of 5 Pin
Aric Wang2-Jan-10 22:02
Aric Wang2-Jan-10 22:02 
GeneralGreat control Pin
DerrekCurtis30-Dec-09 8:24
DerrekCurtis30-Dec-09 8:24 
GeneralMy vote of 1 Pin
zxc4code28-Dec-09 21:36
zxc4code28-Dec-09 21:36 
GeneralRe: My vote of 1 Pin
JunWang16328-Dec-09 22:12
JunWang16328-Dec-09 22:12 
GeneralRe: My vote of 1 Pin
Aric Wang28-Dec-09 22:54
Aric Wang28-Dec-09 22:54 
Laugh | :laugh: ,really,I only find a place to store my code.thx !Hope be your friend.

I am not a genius, but shed more sweat!

GeneralRe: My vote of 1 Pin
CODEPC29-Dec-09 3:33
CODEPC29-Dec-09 3:33 
GeneralRe: My vote of 1 Pin
CODEPC29-Dec-09 3:34
CODEPC29-Dec-09 3:34 
GeneralRe: My vote of 1 Pin
Aric Wang29-Dec-09 4:21
Aric Wang29-Dec-09 4:21 
GeneralRe: My vote of 1 Pin
Aric Wang29-Dec-09 12:11
Aric Wang29-Dec-09 12:11 
GeneralRe: My vote of 1 Pin
CODEPC30-Dec-09 2:04
CODEPC30-Dec-09 2:04 

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.