📄 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, 1, 0, //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("%02d:%02d:%02d", tm.wHour, tm.wMinute, tm.wSecond);
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_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
DrawFace();
// 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("%d", (int)DD_CurrentNumber);
int tmplen = DD_Bits - tmp.GetLength();
if(tmplen >= 0)
{
format.Format("%%%d.%dlf", tmplen, tmplen);
text.Format(format, DD_CurrentNumber);
}
else
{
text = "";
UINT j;
for(j = 0; j < DD_Bits; j ++)
text += ".";
}
}
else //字符串模式
{
format.Format("%%%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 == '.')
index = 10;
else if(c == ':')
index = 11;
else if(c == '-')
index = 12;
else if(c == ' ')
index = 13;
else
index = atoi(&c);
for(i = 0; i < FONT_LENGTH; i ++) //遍历对应字的所有笔划,找出要绘制的笔划并绘制之
{
if(DD_Font[index][i]) //如果对应的笔划为1(需要绘制)
{
switch(i) //根据i的具体位置在不同部位画笔划
{
case 0:
dc->Rectangle(pos_x + DD_CharThick, pos_y, pos_x + DD_CharWidth - DD_CharThick, pos_y + DD_CharThick);
break;
case 1:
dc->Rectangle(pos_x, pos_y + DD_CharThick, pos_x + DD_CharThick, pos_y + (DD_CharHeight - DD_CharThick) / 2);
break;
case 2:
dc->Rectangle(pos_x + DD_CharThick, pos_y + (DD_CharHeight - DD_CharThick) / 2, pos_x + DD_CharWidth - DD_CharThick, pos_y + (DD_CharHeight - DD_CharThick) / 2 + DD_CharThick);
break;
case 3:
dc->Rectangle(pos_x, pos_y + DD_CharThick + (DD_CharHeight - DD_CharThick) / 2, pos_x + DD_CharThick, pos_y + DD_CharHeight - DD_CharThick);
break;
case 4:
dc->Rectangle(pos_x + DD_CharThick, pos_y + DD_CharHeight - DD_CharThick, pos_x + DD_CharWidth - DD_CharThick, pos_y + DD_CharHeight - DD_CharThick + DD_CharThick);
break;
case 5:
dc->Rectangle(pos_x + DD_CharWidth - DD_CharThick, pos_y + DD_CharThick + (DD_CharHeight - DD_CharThick) / 2, pos_x + DD_CharWidth, pos_y + DD_CharHeight - DD_CharThick);
break;
case 6:
dc->Rectangle(pos_x + DD_CharWidth - DD_CharThick, pos_y + DD_CharThick, pos_x + DD_CharWidth, pos_y + (DD_CharHeight - DD_CharThick) / 2);
break;
case 7:
dc->Rectangle(pos_x + (DD_CharWidth - DD_CharThick) / 2, pos_y + DD_CharHeight - DD_CharThick, pos_x + (DD_CharWidth - DD_CharThick) / 2 + DD_CharThick, pos_y + DD_CharHeight);
break;
case 8:
dc->Rectangle(pos_x + (DD_CharWidth - DD_CharThick) / 2, pos_y + (DD_CharHeight - DD_CharThick) / 4, pos_x + (DD_CharWidth - DD_CharThick) / 2 + DD_CharThick, pos_y + DD_CharThick + (DD_CharHeight - DD_CharThick) / 4);
dc->Rectangle(pos_x + (DD_CharWidth - DD_CharThick) / 2, pos_y + DD_CharHeight - (DD_CharHeight - DD_CharThick) / 4 - DD_CharThick, pos_x + (DD_CharWidth - DD_CharThick) / 2 + DD_CharThick, pos_y + DD_CharHeight - (DD_CharHeight - DD_CharThick) / 4);
break;
default:
break;
}
}
}
dc->SelectObject(ppenold);
pen.DeleteObject();
dc->SelectObject(pbrushold);
brush.DeleteObject();
}
/////////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -