📄 digitaldevice.cpp
字号:
/////////////////////////////////////////////////////////
// 数码式测量设备DigitalDevice封装类
// 版本:1.06
// 最后修改日期:2002.1.25
/////////////////////////////////////////////////////////
// DigitalDevice.cpp : implementation file
//
#include "stdafx.h"
#include "DigitalDevice.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// DigitalDevice
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DigitalDevice(CWnd *parent)
//输入参数:父窗口的指针
//返回值:无
//作用:构造函数,初始化变量的值,创建并显示对象
DigitalDevice::DigitalDevice(CWnd *parent)
{
//初始化字模数组
const UINT font[FONT_MAXNUM][FONT_LENGTH]=
{
1, 1, 0, 1, 1, 1, 1, 0, 0, //0
0, 0, 0, 0, 0, 1, 1, 0, 0, //1
1, 0, 1, 1, 1, 0, 1, 0, 0, //2
1, 0, 1, 0, 1, 1, 1, 0, 0, //3
0, 1, 1, 0, 0, 1, 1, 0, 0, //4
1, 1, 1, 0, 1, 1, 0, 0, 0, //5
1, 1, 1, 1, 1, 1, 0, 0, 0, //6
1, 1, 0, 0, 0, 1, 1, 0, 0, //7
1, 1, 1, 1, 1, 1, 1, 0, 0, //8
1, 1, 1, 0, 1, 1, 1, 0, 0, //9 //9不出小数点
0, 0, 0, 0, 0, 0, 0, 1, 0, //小数点
0, 0, 0, 0, 0, 0, 0, 0, 1, //冒号
0, 0, 1, 0, 0, 0, 0, 0, 0, //负号
0, 0, 0, 0, 0, 0, 0, 0, 0 //空格
};
memcpy(DD_Font, font, sizeof(DD_Font));
DD_Width = 210; //设置仪表的大小
DD_Height = 55;
DD_CharWidth = 21; //设置显示字体的大小和显示样式
DD_CharHeight = 40;
DD_CharThick = 3;
DD_CharSpace = 3;
DD_Bits = 8; //默认采用6位数码管
DD_CurrentNumber = 0; //默认要显示的数字是0
// SYSTEMTIME tm; //默认要显示的字符串是本机的时间
// GetLocalTime(&tm);
// DD_CurrentString.Format(_T("%02d:%02d:%02d"), tm.wHour, tm.wMinute, tm.wSecond);
DD_CurrentString.Format(_T(""));
DD_BorderLeft = 1; //默认设置四周边框都要绘制
DD_BorderRight = 1;
DD_BorderTop = 1;
DD_BorderBottom = 1;
DD_BorderWidth = 3; //默认的边框宽度
DD_BdLightColor = RGB(255, 255, 255); //默认边界颜色
DD_BdDarkColor = RGB(100, 100, 100);
DD_TextPos.x = 10; //设置文字的默认位置
DD_TextPos.y = 7;
DD_BkColor = RGB(0, 0, 0); //设置仪表的背景颜色为黑色
DD_TextLightColor = RGB(0, 255, 0); //设置默认的字体亮颜色
DD_TextDarkColor = GetNextColor(DD_TextLightColor, 0.3);//设置默认的字体暗颜色
DD_WorkMode = DD_STRING; //默认的工作方式
DD_DisMode = DD_LED; //默认LED数字仪表
DD_Status = DD_WORK; //默认仪表状态
//创建相应的静态控件并且显示
CRect rect;
rect.left = rect.top = 0;
rect.bottom = DD_Height - 1;
rect.right = DD_Width - 1;
Create(NULL, WS_CHILD | WS_VISIBLE | SS_NOTIFY, rect, parent);
// SetTimer(1, 500, NULL); //设置自检时使用的计时器
}
DigitalDevice::~DigitalDevice()
{
KillTimer(1);
}
BEGIN_MESSAGE_MAP(DigitalDevice, CStatic)
//{{AFX_MSG_MAP(DigitalDevice)
ON_WM_PAINT()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// DigitalDevice message handlers
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:OnPaint()
//输入参数:无
//返回值:无
//作用:重载函数,绘制整个仪表的界面
void DigitalDevice::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
switch(DD_DisMode)
{
case DD_LED:
DrawFace();
break;
case DD_TEXT:
DrawText();
break;
}
// Do not call CStatic::OnPaint() for painting messages
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DrawFace()
//输入参数:无
//返回值:无
//作用:绘制整个仪表的界面
void DigitalDevice::DrawFace()
{
int i;
CClientDC dc(this);
CBitmap bmpc;
bmpc.CreateCompatibleBitmap(&dc, DD_Width, DD_Height); //创建与系统兼容的位图
BITMAP bmp;
bmpc.GetBitmap(&bmp);
//绘制仪表的背景色
CDC dcmem;
dcmem.CreateCompatibleDC(&dc);
CBitmap * pbitmapold = (CBitmap *)dcmem.SelectObject(&bmpc);
CBrush brush;
brush.CreateSolidBrush(DD_BkColor);
CBrush * pbrushold = (CBrush *)dcmem.SelectObject(&brush);
dcmem.SelectStockObject(NULL_PEN);
dcmem.Rectangle(0, 0, bmp.bmWidth + 1, bmp.bmHeight + 1);
dcmem.SelectObject(pbrushold);
brush.DeleteObject();
//绘制仪表的边框
brush.CreateSolidBrush(DD_BdDarkColor);
pbrushold = (CBrush *)dcmem.SelectObject(&brush);
if(DD_BorderTop)
dcmem.Rectangle(0, 0, DD_Width + 1, DD_BorderWidth);
if(DD_BorderLeft)
dcmem.Rectangle(0, 0, DD_BorderWidth, DD_Height + 1);
dcmem.SelectObject(pbrushold);
brush.DeleteObject();
brush.CreateSolidBrush(DD_BdLightColor);
pbrushold = (CBrush *)dcmem.SelectObject(&brush);
if(DD_BorderBottom)
dcmem.Rectangle(0, DD_Height - DD_BorderWidth +1, DD_Width + 1, DD_Height + 1);
if(DD_BorderRight)
dcmem.Rectangle(DD_Width - DD_BorderWidth + 1, 0, DD_Width + 1, DD_Height + 1);
dcmem.SelectObject(pbrushold);
brush.DeleteObject();
//绘制要显示的文字
CString text;
CString tmp, format;
if(DD_WorkMode == DD_NUMBER) //数值模式
{
tmp.Format(_T("%d"), (int)DD_CurrentNumber);
int tmplen = DD_Bits - tmp.GetLength();
if(tmplen >= 0)
{
format.Format(_T("%%%d.%dlf"), tmplen, tmplen);
text.Format(format, DD_CurrentNumber);
}
else
{
text = _T("");
UINT j;
for(j = 0; j < DD_Bits; j ++)
text += _T(".");
}
}
else //字符串模式
{
format.Format(_T("%%%ds"), DD_Bits);
text.Format(format, DD_CurrentString);
}
char curc;
for(i = 0; i < (int)DD_Bits; i ++)
{
curc = text.GetAt(i);
PutChar(&dcmem, '8', DD_TextPos.x + i * (DD_CharWidth + DD_CharSpace), DD_TextPos.y, DD_TextDarkColor); //绘制背景的8字形暗图案
PutChar(&dcmem, curc, DD_TextPos.x + i * (DD_CharWidth + DD_CharSpace), DD_TextPos.y, DD_TextLightColor); //绘制点亮的字
}
//将内存DC的内容一次画在屏幕DC上
dc.BitBlt(0, 0, bmp.bmWidth, bmp.bmHeight, &dcmem, 0, 0, SRCCOPY);
dcmem.SelectObject(pbitmapold);
bmpc.DeleteObject();
dcmem.DeleteDC();
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_MoveWindow(int x, int y)
//输入参数:仪表左上角的坐标
//返回值:无
//作用:移动仪表窗口,移动后自动完成重画
void DigitalDevice::DD_MoveWindow(int x, int y)
{
CRect rect;
GetWindowRect(&rect);
MoveWindow(x, y, rect.Width(), rect.Height(), true);
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:PutChar(char c, int pos_x, int pos_y, RGBCOLOR color)
//输入参数:要绘制的dc的指针,要绘制的字符,字符的坐标值pos_x, pos_y, 绘制文字的颜色(用RGBCOLOR结构体变量)
//返回值:无
//作用:该函数在仪表界面上指定位置绘制一个字符
void DigitalDevice::PutChar(CDC *dc, char c, int pos_x, int pos_y, COLORREF color)
{
int index, i;
CPen pen, *ppenold;
CBrush brush, *pbrushold;
pen.CreatePen(PS_SOLID, 1, color);
ppenold = (CPen *)dc->SelectObject(&pen);
brush.CreateSolidBrush(color);
pbrushold = (CBrush *)dc->SelectObject(&brush);
if(c == _T('.'))
index = 10;
else if(c == _T(':'))
index = 11;
else if(c == _T('-'))
index = 12;
else if(c == _T(' '))
index = 13;
else
index = atoi(&c);
CPoint ptRgn[6];
int ptNum;
int thick3,thick2,thickrou;
CRect rectRound;
if(DD_CharThick<=2)thick2=1; else thick2=DD_CharThick/2;
thick3=DD_CharThick;
if(DD_CharThick==1)thickrou=2; else thickrou=DD_CharThick; //防止小数点太小
for(i = 0; i < FONT_LENGTH; i ++) //遍历对应字的所有笔划,找出要绘制的笔划并绘制之
{
if(DD_Font[index][i]) //如果对应的笔划为1(需要绘制)
{
switch(i) //根据i的具体位置在不同部位画笔划
{
case 0:
ptRgn[0].x=pos_x+thick3; ptRgn[0].y=pos_y;
ptRgn[1].x=pos_x+DD_CharWidth-thick3; ptRgn[1].y=pos_y;
ptRgn[2].x=pos_x+DD_CharWidth-thick3-DD_CharThick;
ptRgn[2].y=pos_y+DD_CharThick;
ptRgn[3].x=pos_x+thick3+DD_CharThick;
ptRgn[3].y=pos_y+DD_CharThick;
ptNum=4;
dc->Polygon(ptRgn, ptNum);
break;
case 1:
ptRgn[0].x=pos_x; ptRgn[0].y=pos_y;
ptRgn[1].x=pos_x+DD_CharThick; ptRgn[1].y=pos_y+DD_CharThick;
ptRgn[2].x=pos_x+DD_CharThick;
ptRgn[2].y=pos_y+DD_CharHeight/2-DD_CharThick;
ptRgn[3].x=pos_x+thick2;
ptRgn[3].y=pos_y+DD_CharHeight/2-thick2;
ptRgn[4].x=pos_x;
ptRgn[4].y=pos_y+DD_CharHeight/2-DD_CharThick;
ptNum=5;
dc->Polygon(ptRgn, ptNum);
break;
case 2:
ptRgn[0].x=pos_x+thick2+thick3;
ptRgn[0].y=pos_y+DD_CharHeight/2;
ptRgn[1].x=pos_x+DD_CharThick+thick3;
ptRgn[1].y=pos_y+DD_CharHeight/2-DD_CharThick/2;
ptRgn[2].x=pos_x+DD_CharWidth-DD_CharThick-thick3;
ptRgn[2].y=pos_y+DD_CharHeight/2-DD_CharThick/2;
ptRgn[3].x=pos_x+DD_CharWidth-thick2-thick3;
ptRgn[3].y=pos_y+DD_CharHeight/2;
ptRgn[4].x=pos_x+DD_CharWidth-DD_CharThick-thick3;
ptRgn[4].y=pos_y+DD_CharHeight/2+DD_CharThick/2;
ptRgn[5].x=pos_x+DD_CharThick+thick3;
ptRgn[5].y=pos_y+DD_CharHeight/2+DD_CharThick/2;
ptNum=6;
dc->Polygon(ptRgn, ptNum);
break;
case 3:
ptRgn[0].x=pos_x; ptRgn[0].y=pos_y+DD_CharHeight;
ptRgn[1].x=pos_x+DD_CharThick;
ptRgn[1].y=pos_y+DD_CharHeight-DD_CharThick;
ptRgn[2].x=pos_x+DD_CharThick;
ptRgn[2].y=pos_y+DD_CharHeight/2+DD_CharThick;
ptRgn[3].x=pos_x+thick2;
ptRgn[3].y=pos_y+DD_CharHeight/2+thick2;
ptRgn[4].x=pos_x;
ptRgn[4].y=pos_y+DD_CharHeight/2+DD_CharThick;
ptNum=5;
dc->Polygon(ptRgn, ptNum);
break;
case 4:
ptRgn[0].x=pos_x+thick3; ptRgn[0].y=pos_y+DD_CharHeight;
ptRgn[1].x=pos_x+DD_CharWidth-thick3;
ptRgn[1].y=pos_y+DD_CharHeight;
ptRgn[2].x=pos_x+DD_CharWidth-thick3-DD_CharThick;
ptRgn[2].y=pos_y+DD_CharHeight-DD_CharThick;
ptRgn[3].x=pos_x+DD_CharThick+thick3;
ptRgn[3].y=pos_y+DD_CharHeight-DD_CharThick;
ptNum=4;
dc->Polygon(ptRgn, ptNum);
break;
case 5:
ptRgn[0].x=pos_x+DD_CharWidth;
ptRgn[0].y=pos_y+DD_CharHeight;
ptRgn[1].x=pos_x+DD_CharWidth-DD_CharThick;
ptRgn[1].y=pos_y+DD_CharHeight-DD_CharThick;
ptRgn[2].x=pos_x+DD_CharWidth-DD_CharThick;
ptRgn[2].y=pos_y+DD_CharHeight/2+DD_CharThick;
ptRgn[3].x=pos_x+DD_CharWidth-thick2;
ptRgn[3].y=pos_y+DD_CharHeight/2+thick2;
ptRgn[4].x=pos_x+DD_CharWidth;
ptRgn[4].y=pos_y+DD_CharHeight/2+DD_CharThick;
ptNum=5;
dc->Polygon(ptRgn, ptNum);
break;
case 6:
ptRgn[0].x=pos_x+DD_CharWidth; ptRgn[0].y=pos_y;
ptRgn[1].x=pos_x+DD_CharWidth-DD_CharThick;
ptRgn[1].y=pos_y+DD_CharThick;
ptRgn[2].x=pos_x+DD_CharWidth-DD_CharThick;
ptRgn[2].y=pos_y+DD_CharHeight/2-DD_CharThick;
ptRgn[3].x=pos_x+DD_CharWidth-thick2;
ptRgn[3].y=pos_y+DD_CharHeight/2-thick2;
ptRgn[4].x=pos_x+DD_CharWidth;
ptRgn[4].y=pos_y+DD_CharHeight/2-DD_CharThick;
ptNum=5;
dc->Polygon(ptRgn, ptNum);
break;
case 7:
rectRound.SetRect(pos_x+DD_CharWidth/2-thickrou,
pos_y+DD_CharHeight-thickrou*2,
pos_x+DD_CharWidth/2+thickrou,
pos_y+DD_CharHeight);
dc->Ellipse(rectRound);
break;
case 8:
rectRound.SetRect(pos_x+DD_CharWidth/2-thickrou,
pos_y+DD_CharHeight/4-thickrou,
pos_x+DD_CharWidth/2+thickrou,
pos_y+DD_CharHeight/4+thickrou);
dc->Ellipse(rectRound);
rectRound.SetRect(pos_x+DD_CharWidth/2-thickrou,
pos_y+DD_CharHeight*3/4-thickrou,
pos_x+DD_CharWidth/2+thickrou,
pos_y+DD_CharHeight*3/4+thickrou);
dc->Ellipse(rectRound);
break;
default:
break;
}
}
}
dc->SelectObject(ppenold);
pen.DeleteObject();
dc->SelectObject(pbrushold);
brush.DeleteObject();
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:OnTimer(UINT nIDEvent)
//输入参数:定时器事件的ID
//返回值:无
//作用:重载函数,主要处理测试状态下的重画工作
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -