📄 digitaldevice.cpp
字号:
void DigitalDevice::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
static k = 1;
switch(DD_Status)
{
case DD_TEST:
switch(DD_WorkMode)
{
case DD_NUMBER:
DD_CurrentNumber ++;
if(DD_CurrentNumber > GetMaxNumber())
DD_CurrentNumber = 0;
DrawFace();
break;
case DD_STRING:
SYSTEMTIME tm;
GetLocalTime(&tm);
if(k == 1)
DD_CurrentString.Format(_T("%02d:%02d:%02d"), tm.wHour, tm.wMinute, tm.wSecond);
else
DD_CurrentString.Format(_T("%02d %02d %02d"), tm.wHour, tm.wMinute, tm.wSecond);
DrawFace();
k = - k;
break;
}
break;
default:
break;
}
CStatic::OnTimer(nIDEvent);
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetSize(UINT width, UINT height)
//输入参数:仪表的宽度和高度
//返回值:无
//作用:设置仪表的大小
void DigitalDevice::DD_SetSize(UINT width, UINT height)
{
DD_Width = width;
DD_Height = height;
RECT rect;
GetWindowRect(&rect);
MoveWindow(rect.left, rect.top, width, height);
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_Update()
//输入参数:无
//返回值:无
//作用:该函数立刻重画仪表的界面
void DigitalDevice::DD_Update()
{
switch(DD_DisMode)
{
case DD_LED:
DrawFace();
break;
case DD_TEXT:
DrawText();
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetTextStyle(UINT charwidth, UINT charheight, UINT charthick, UINT charspace)
//输入参数:charwidth是字符的宽度,charheight是字符的高度,charthick是字符的笔划宽度,charspace是字符之间的空白距离,以上单位均是象素
//返回值:无
//作用:设置文字的显示样式
void DigitalDevice::DD_SetTextStyle(UINT charwidth, UINT charheight, UINT charthick, UINT charspace)
{
DD_CharWidth = charwidth;
DD_CharHeight = charheight;
DD_CharThick = charthick;
DD_CharSpace = charspace;
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetTextPos(int x, int y)
//输入参数:开始绘制文字(文字的左上角)的坐标
//返回值:无
//作用:设置要显示的文字的位置
void DigitalDevice::DD_SetTextPos(int x, int y)
{
DD_TextPos.x = x;
DD_TextPos.y = y;
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetWorkMode(UINT mode)
//输入参数:工作方式常量,可以是DD_NUMBER或者是DD_STRING,分别表示要测试的量是数值或者是字串
//返回值:无
//作用:设置仪表的工作方式
void DigitalDevice::DD_SetWorkMode(UINT mode)
{
DD_WorkMode = mode;
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:SetCurrentNumber(double num)
//输入参数:期望的当前示数
//返回值:无
//作用:设置仪表当前的示数,该函数自动完成重画
void DigitalDevice::SetCurrentNumber(double num)
{
DD_CurrentNumber = num;
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetCurrentString(CString string)
//输入参数:期望的当前显示字符串,只能包含数字,'.'和':'
//返回值:无
//作用:设置仪表当前的显示字符串,该函数自动完成重画
void DigitalDevice::DD_SetCurrentString(CString string)
{
if(DD_Status != DD_WORK)
return;
DD_CurrentString = string;
switch(DD_DisMode)
{
case DD_LED:
DrawFace();
break;
case DD_TEXT:
DrawText();
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetStatus(UINT status)
//输入参数:仪表的状态常量,可以是DD_TEST(测试状态)或者DD_WORK(正常工作)
//返回值:无
//作用:设置仪表当前的状态
void DigitalDevice::DD_SetStatus(UINT status)
{
DD_Status = status;
DD_CurrentNumber = 0;
DD_CurrentString = _T("");
switch(DD_DisMode)
{
case DD_LED:
DrawFace();
break;
case DD_TEXT:
DrawText();
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetCurrentNumber(double num)
//输入参数:当前要显示的数值
//返回值:无
//作用:设置仪表当前要显示的数值
void DigitalDevice::DD_SetCurrentNumber(double num)
{
if(DD_Status != DD_WORK)
return;
DD_CurrentNumber = num;
DrawFace();
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetTextLightColor(COLORREF color)
//输入参数:文字的颜色(点亮状态)
//返回值:无
//作用:设置仪表文字的颜色(点亮状态)
void DigitalDevice::DD_SetTextLightColor(COLORREF color)
{
DD_TextLightColor = color;
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetTextDarkColor(COLORREF color)
//输入参数:文字的颜色(熄灭状态)
//返回值:无
//作用:设置仪表文字的颜色(非点亮状态)
void DigitalDevice::DD_SetTextDarkColor(COLORREF color)
{
DD_TextDarkColor = color;
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetBits(UINT bits)
//输入参数:数码管位数,如果设置成0,将不显示数字,外观和普通的静态控件一样
//返回值:无
//作用:设置仪表数码管的位数
void DigitalDevice::DD_SetBits(UINT bits)
{
DD_Bits = bits;
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetBorderStyle(UINT left, UINT top, UINT right, UINT bottom)
//输入参数:四条边框线的显示方式,0表示不显示,1表示显示
//返回值:无
//作用:设置仪表边框的显示样式
void DigitalDevice::DD_SetBorderStyle(UINT left, UINT top, UINT right, UINT bottom)
{
DD_BorderLeft = left;
DD_BorderTop = top;
DD_BorderRight = right;
DD_BorderBottom = bottom;
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:GetMaxNumber()
//输入参数:无
//返回值:无
//作用:获取仪表能表示的最大数值
double DigitalDevice::GetMaxNumber()
{
UINT i;
double maxval;
// CString text = _T("");
// for(i = 0; i < DD_Bits; i ++)
// text += _T("9");
maxval=0;
for(i = 0; i < DD_Bits; i ++)
maxval=maxval+(10^i)*9;
return maxval;
// return atof(T2A(text));
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetBorderWidth(UINT width)
//输入参数:边框的线条宽度,单位是象素
//返回值:无
//作用:设置仪表的边框的粗细
void DigitalDevice::DD_SetBorderWidth(UINT width)
{
DD_BorderWidth = width;
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetBkColor(COLORREF color)
//输入参数:背景颜色
//返回值:无
//作用:设置仪表的背景颜色
void DigitalDevice::DD_SetBkColor(COLORREF color)
{
DD_BkColor = color;
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:DD_SetBdColor(COLORREF lightcolor, COLORREF darkcolor)
//输入参数:边框的亮部颜色,边框的暗部颜色
//返回值:无
//作用:设置仪表的边框颜色
void DigitalDevice::DD_SetBdColor(COLORREF lightcolor, COLORREF darkcolor)
{
DD_BdLightColor = lightcolor;
DD_BdDarkColor = darkcolor;
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:GetNextColor(COLORREF current_color, int delta)
//输入参数:current_color为源颜色,delta为变化量(可以取正负值)
//返回值:无
//作用:获得current_color的同样色调的下一颜色(如果delta大于0则为更亮的颜色,小于0则为更暗的颜色)
COLORREF DigitalDevice::GetNextColor(COLORREF current_color, int delta)
{
long red, green, blue;
red = GetRValue(current_color) + delta;
green = GetGValue(current_color) + delta;
blue = GetBValue(current_color) + delta;
if(red > 255)
red = 255;
if(green > 255)
green = 255;
if(blue > 255)
blue = 255;
if(red < 0)
red = 0;
if(green < 0)
green = 0;
if(blue < 0)
blue = 0;
return RGB(red, green, blue);
}
/////////////////////////////////////////////////////////////////////////////////////////
//函数名:GetNextColor(COLORREF color, double ratio)
//输入参数:current_color为源颜色,ratio为变化比率
//返回值:无
//作用:获得current_color的同样色调的下一颜色(如果ratio大于1则为更亮的颜色,小于1则为更暗的颜色)
COLORREF DigitalDevice::GetNextColor(COLORREF current_color, double ratio)
{
long red, green, blue;
red = (long)(GetRValue(current_color) * ratio);
green = (long)(GetGValue(current_color) * ratio);
blue = (long)(GetBValue(current_color) * ratio);
if(red > 255)
red = 255;
if(green > 255)
green = 255;
if(blue > 255)
blue = 255;
if(red < 0)
red = 0;
if(green < 0)
green = 0;
if(blue < 0)
blue = 0;
return RGB(red, green, blue);
}
void DigitalDevice::ShowFont(CDC *pDC, CRect rect, int XSize, int YSize, COLORREF TextColor, COLORREF BKColor, CString strVal)
{
TEXTMETRIC tm;
CFont fontText;
CSize sizeText;
fontText.CreateFont( XSize,YSize, 0, 0, 400, FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS, _T("Times New Roman"));
CFont* pOldFont = (CFont*) pDC->SelectObject(&fontText);
pDC->GetTextMetrics(&tm);
sizeText = pDC->GetTextExtent(strVal);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(TextColor);
pDC->DrawText(strVal, rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
pDC->SelectObject(pOldFont);
fontText.DeleteObject();
}
void DigitalDevice::DrawText()
{
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();
//绘制要显示的文字
ShowFont(&dcmem,CRect(0,0,DD_Width,DD_Height),DD_CharHeight,DD_CharWidth,DD_TextLightColor,
DD_BkColor,DD_CurrentString);
//将内存DC的内容一次画在屏幕DC上
dc.BitBlt(0, 0, bmp.bmWidth, bmp.bmHeight, &dcmem, 0, 0, SRCCOPY);
dcmem.SelectObject(pbitmapold);
bmpc.DeleteObject();
dcmem.DeleteDC();
}
//函数名:DD_SetBdColor(COLORREF lightcolor, COLORREF darkcolor)
//输入参数:边框的亮部颜色,边框的暗部颜色
//返回值:无
//作用:设置仪表的边框颜色
void DigitalDevice::DD_SetDisMode(UINT mode)
{
DD_DisMode = mode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -