Click here to Skip to main content
13,091,363 members (49,789 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


39 bookmarked
Posted 27 Dec 2009

A Simple Digital LCD Demo

, 28 Dec 2009
Rate this:
Please Sign up or sign in to vote.
Just a simple digital LCD class and do not use an external bitmap.You can use and change it.


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.


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  
 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();  

 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;


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; =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; =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; =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; =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; =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; =m_nYPos+53;rt.bottom =m_nYPos+54;
   ::FillRect (hdc,&rt,hBrush);//bottom

   rt.left =m_nXPos+9+iPos*offset;rt.right =m_nXPos+10+iPos*offset; =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; =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; =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; =m_nYPos+13;rt.bottom =m_nYPos+31;
   ::FillRect (hdc,&rt,hBrush);//top right   

   rt.left =m_nXPos+9+iPos*offset;rt.right =m_nXPos+10+iPos*offset; =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; =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||
   rt.left =m_nXPos+9+23+iPos*offset;rt.right =m_nXPos+10+23+iPos*offset; =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; =m_nYPos+13+22;rt.bottom =m_nYPos+31+22;
   ::FillRect (hdc,&rt,hBrush);//bottom right

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; =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++)

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.


I'll update it later.


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


About the Author

Aric Wang
China China
Although I was a college studnet now as the same I was also a teacher of C/C++/3DS MAX/website for National Computer Rank Examination.I began to study computer when I was a child 8 year's old.I use BASIC/C/C++/VB/VF/VC/DELPHI/.NET to programming.When in college I was the chairman of Computer Association of our school and I take part in ACM Competition twice in two years.I also was the only person who take part in the National Mathematical Contest in Modeling twice in two years of our school.I like computer programming very much.Hope to be your friend.thx!

You may also be interested in...

Comments and Discussions

Generalthanks Pin
futurejo3-Nov-10 2:46
memberfuturejo3-Nov-10 2:46 
It's a great helpful to me,
GeneralRe: thanks Pin
Aric Green10-Feb-11 19:42
memberAric Green10-Feb-11 19:42 
GeneralSome comments Pin
mluri4-Jan-10 6:49
membermluri4-Jan-10 6:49 
GeneralRe: Some comments Pin
Aric Green4-Jan-10 14:15
memberAric Green4-Jan-10 14:15 
GeneralRe: Some comments Pin
Aric Green4-Jan-10 14:33
memberAric Green4-Jan-10 14:33 
GeneralRe: Some comments Pin
Rick York3-Aug-10 7:06
memberRick York3-Aug-10 7:06 
GeneralMy vote of 5 Pin
TV Mogul2-Jan-10 15:57
memberTV Mogul2-Jan-10 15:57 
GeneralRe: My vote of 5 Pin
Aric Green2-Jan-10 22:02
memberAric Green2-Jan-10 22:02 
GeneralGreat control Pin
DerrekCurtis30-Dec-09 8:24
memberDerrekCurtis30-Dec-09 8:24 
GeneralMy vote of 1 Pin
zxc4code28-Dec-09 21:36
memberzxc4code28-Dec-09 21:36 
GeneralRe: My vote of 1 Pin
wanghe28-Dec-09 22:12
memberwanghe28-Dec-09 22:12 
GeneralRe: My vote of 1 Pin
Aric Green28-Dec-09 22:54
memberAric Green28-Dec-09 22:54 
GeneralRe: My vote of 1 Pin
CODEPC29-Dec-09 3:33
memberCODEPC29-Dec-09 3:33 
GeneralRe: My vote of 1 Pin
CODEPC29-Dec-09 3:34
memberCODEPC29-Dec-09 3:34 
GeneralRe: My vote of 1 Pin
Aric Green29-Dec-09 4:21
memberAric Green29-Dec-09 4:21 
GeneralRe: My vote of 1 Pin
Aric Green29-Dec-09 12:11
memberAric Green29-Dec-09 12:11 
GeneralRe: My vote of 1 Pin
CODEPC30-Dec-09 2:04
memberCODEPC30-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 28 Dec 2009
Article Copyright 2009 by Aric Wang
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid