Click here to Skip to main content
Click here to Skip to main content

Tagged as

A Simple Digital LCD Demo

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

Share

About the Author

Aric Green
Engineer
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!

Comments and Discussions

 
Generalthanks Pinmemberfuturejo3-Nov-10 3:46 
GeneralRe: thanks PinmemberAric Green10-Feb-11 20:42 
GeneralSome comments Pinmembermluri4-Jan-10 7:49 
GeneralRe: Some comments PinmemberAric Green4-Jan-10 15:15 
GeneralRe: Some comments PinmemberAric Green4-Jan-10 15:33 
GeneralRe: Some comments PinmemberRick York3-Aug-10 8:06 
GeneralMy vote of 5 PinmemberTV Mogul2-Jan-10 16:57 
GeneralRe: My vote of 5 PinmemberAric Green2-Jan-10 23:02 
GeneralGreat control PinmemberDerrekCurtis30-Dec-09 9:24 
GeneralMy vote of 1 Pinmemberzxc4code28-Dec-09 22:36 
GeneralRe: My vote of 1 Pinmemberwanghe28-Dec-09 23:12 
GeneralRe: My vote of 1 PinmemberAric Green28-Dec-09 23:54 
GeneralRe: My vote of 1 PinmemberCODEPC29-Dec-09 4:33 
GeneralRe: My vote of 1 PinmemberCODEPC29-Dec-09 4:34 
GeneralRe: My vote of 1 PinmemberAric Green29-Dec-09 5:21 
GeneralRe: My vote of 1 PinmemberAric Green29-Dec-09 13:11 
Are you the editor of codeproject?
 
I am not a genius, but shed more sweat!

GeneralRe: My vote of 1 PinmemberCODEPC30-Dec-09 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 28 Dec 2009
Article Copyright 2009 by Aric Green
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid