?? 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 + -