简单的数字 LCD 演示






4.44/5 (12投票s)
这只是一个简单的数字 LCD 类,未使用外部位图。你可以使用和修改它。

引言
这段代码片段是一个相当简单的数字 LCD 演示,未使用外部位图。请看上面的图片。当前系统时间只是你电脑的本地时间。以下用于测试。数字的颜色可以通过以下三个滑块更改。名为 VALUE
的滑块用于更改中间 LCD 屏幕上的数字。要了解更多信息,请下载演示程序。如果它能帮助到你,或者你能提出一些好的新想法,我会非常高兴。
背景
这个类完全是原创的。我今天创建的。所以,你可以在任何地方以任何方式使用它。
Using the Code
正如我刚才所说,这只是一个相当简单的类。要使用它,我们只需将它的文件添加到你的项目中,并调用 Init()
方法来完成初始化。让我们看一下类的定义
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.
};
我只花了几个小时来创建它,所以它非常简单。我稍后会添加更多功能。使用方法如下
#include "DigitalLCDClass.h"
...
DigitalLCDClass dig;
...
dig.Init();
dig.SetColor(RGB(255,0,0));
dig.SetText("2009");
...
dig.UpdateData(hdc);
...
好的!之后,让我们专注于它的原理
事实上,我之前创建了另一个类似的演示程序。但是,上次我使用位图来实现它。即使它比这个更好,也需要读取外部文件。位图上的 LCD 数字是从 0 到 9。我只做一个矩形来在适当的时间复制它。这次,不要使用 PhotoShop 来制作位图,只使用一些 GDI 函数。
如何绘制一个数字? 事实上,这非常容易。仔细观察!数字 8 全部点亮。其他的都是数字 8 的一部分。在顶部,数字 8 是最上部的一条水平线。我使用的线是一个矩形来绘制它。一个大和一个小。
好的,现在我们可以使用一个矩形绘制数字 8,使用 API FillRect()
来完成它。如何显示其他的数字?问题来了!想想,数字 8 有多少个灯亮?七个!是的!如果数字 8 需要显示,所有灯都应该亮起。所以,数字 1 只需要两个灯亮,在右下角和右上角。
数字 2:顶部、右上角、中间、左下角和底部都亮着。
...
为了实现这一点,我使用这种方法
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);
}
完成数字绘制后,绘制背景和熄灭状态,使用方法 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);
}
为了从右到左显示数字,当我们调用 DrawDigit()
时,第三个参数必须是 m_nItem-nLength+i
。第三个参数决定了应该显示的位置(LCD 屏幕上的项目)。
好的!感谢你的支持!我会更加努力。想了解更多关于这个演示程序的信息,请下载源代码。
历史
我稍后会更新它。