?? ledctrl.cpp
字號(hào):
// LedCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "LedCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLedCtrl
CLedCtrl::CLedCtrl()
{
m_bDrawFadedNotches = false;
m_crColorForeground = 0x0000FF00;
m_crColorBackground = 0;
m_brBackground.CreateSolidBrush(m_crColorBackground);
m_strNumber = "0";
m_bGotMetrics = false;
}
CLedCtrl::~CLedCtrl()
{
}
BEGIN_MESSAGE_MAP(CLedCtrl, CStatic)
//{{AFX_MSG_MAP(CLedCtrl)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLedCtrl message handlers
//繪制一個(gè)數(shù)字
void CLedCtrl::Draw(CMemDC* pDC, DWORD dwChar, int nCol)
{
//計(jì)算點(diǎn)寬度和長(zhǎng)度
if (!m_bGotMetrics)
{
int nHeight = m_recClient.bottom;
(nHeight * 0.07) < 1 ? m_nNotchWidth = 1 : m_nNotchWidth = (int)(nHeight * 0.07);
(nHeight * 0.35) < 1 ? m_nNotchLength = 1 : m_nNotchLength = (int)(nHeight * 0.35);
m_nNotchWidth = m_nNotchWidth;
m_bGotMetrics = true;
}
//設(shè)置列寬度
if ( nCol > 0 )
nCol = (nCol*m_nNotchLength) + (m_nNotchWidth*4) * nCol;
//設(shè)置淡入淡出顏色
COLORREF crNotchColor = m_crColorForeground;
if (dwChar == STCOUNTERALL) {
int r = GetRValue(m_crColorForeground)/3;
int g = GetGValue(m_crColorForeground)/3;
int b = GetBValue(m_crColorForeground)/3;
crNotchColor = RGB(r,g,b);
}
//創(chuàng)建畫筆
CPen pen(PS_SOLID , m_nNotchWidth, crNotchColor);
pDC->SelectObject(&pen);
//如果有第1畫,則畫第1畫
if ( (dwChar & NOTCH1) || dwChar == STCOUNTERALL)
{
pDC->MoveTo( nCol + m_nNotchWidth*2, m_nNotchWidth );
pDC->LineTo( nCol + m_nNotchLength, m_nNotchWidth );
}
//如果有第2畫,則畫第2畫
if ( dwChar & NOTCH2 || dwChar == STCOUNTERALL)
{
pDC->MoveTo(nCol + m_nNotchLength + m_nNotchWidth, m_nNotchWidth*2);
pDC->LineTo(nCol + m_nNotchLength + m_nNotchWidth, m_nNotchLength + (m_nNotchWidth*2) );
}
//如果有第3畫,則畫第3畫
if ( dwChar & NOTCH3 || dwChar == STCOUNTERALL)
{
pDC->MoveTo(nCol + m_nNotchLength + m_nNotchWidth, m_nNotchLength + (m_nNotchWidth*4) );
pDC->LineTo(nCol + m_nNotchLength + m_nNotchWidth, m_nNotchLength*2 + (m_nNotchWidth*3) );
}
//如果有第4畫,則畫第4畫
if ( dwChar & NOTCH4 || dwChar == STCOUNTERALL)
{
pDC->MoveTo( nCol + m_nNotchWidth*2, m_nNotchLength*2 + (m_nNotchWidth*4) );
pDC->LineTo( nCol + m_nNotchLength, m_nNotchLength*2 + (m_nNotchWidth*4) );
}
//如果有第5畫,則畫第5畫
if ( dwChar & NOTCH5 || dwChar == STCOUNTERALL)
{
pDC->MoveTo(nCol + m_nNotchWidth, m_nNotchLength + (m_nNotchWidth*4) );
pDC->LineTo(nCol + m_nNotchWidth, m_nNotchLength*2 + (m_nNotchWidth*3) );
}
//如果有第6畫,則畫第6畫
if ( dwChar & NOTCH6 || dwChar == STCOUNTERALL)
{
pDC->MoveTo(nCol + m_nNotchWidth, m_nNotchWidth*2);
pDC->LineTo(nCol + m_nNotchWidth, m_nNotchLength + (m_nNotchWidth*2) );
}
//如果有第7畫,則畫第7畫
if ( dwChar & NOTCH7 || dwChar == STCOUNTERALL)
{
pDC->MoveTo(nCol + m_nNotchWidth*2, m_nNotchLength + (m_nNotchWidth*3) );
pDC->LineTo(nCol + m_nNotchWidth + m_nNotchLength - m_nNotchWidth, m_nNotchLength + (m_nNotchWidth*3) );
}
}
//設(shè)置是否顯示淡入淡出效果
void CLedCtrl::SetDrawFaded(bool bState)
{
m_bDrawFadedNotches = bState;
}
//設(shè)置背景色
void CLedCtrl::SetColorBackGround(COLORREF crColor)
{
if (crColor != 0xffffffff)
m_crColorBackground = crColor;
else
m_crColorBackground = ::GetSysColor(COLOR_BTNFACE);
//重新生成背景刷
m_brBackground.DeleteObject();
m_brBackground.CreateSolidBrush(m_crColorBackground);
//重畫
Invalidate();
}
//設(shè)置前景色
void CLedCtrl::SetColorForeGround(COLORREF crColor)
{
if (crColor != 0xffffffff)
{
m_crColorForeground = crColor;
}
else
{
m_crColorForeground = ::GetSysColor(COLOR_BTNTEXT);
}
//重畫
Invalidate(FALSE);
}
//顯示數(shù)字
void CLedCtrl::Display(int nNumber)
{
m_strNumber.Format(_T("%d"), nNumber);
//調(diào)用OnPaint方法重畫
Invalidate(FALSE);
}
//控件繪制事件處理方法
void CLedCtrl::OnPaint()
{
GetClientRect(&m_recClient);
CPaintDC dc(this);
CMemDC memDC(&dc, m_recClient);
CMemDC* pDC = &memDC;
CRect clip;
pDC->GetClipBox(&clip);
pDC->FillSolidRect(&m_recClient, m_crColorBackground );
for (int nCount = 0; nCount< m_strNumber.GetLength(); nCount++)
{
if (m_bDrawFadedNotches)
Draw( pDC, STCOUNTERALL, nCount ); // Draw the faded bits
CString str = m_strNumber[nCount];
if ( m_strNumber[nCount] == '0' ) Draw( pDC, STCOUNTER0, nCount );
else if ( m_strNumber[nCount] == '1' ) Draw( pDC, STCOUNTER1, nCount );
else if ( m_strNumber[nCount] == '2' ) Draw( pDC, STCOUNTER2, nCount );
else if ( m_strNumber[nCount] == '3' ) Draw( pDC, STCOUNTER3, nCount );
else if ( m_strNumber[nCount] == '4' ) Draw( pDC, STCOUNTER4, nCount );
else if ( m_strNumber[nCount] == '5' ) Draw( pDC, STCOUNTER5, nCount );
else if ( m_strNumber[nCount] == '6' ) Draw( pDC, STCOUNTER6, nCount );
else if ( m_strNumber[nCount] == '7' ) Draw( pDC, STCOUNTER7, nCount );
else if ( m_strNumber[nCount] == '8' ) Draw( pDC, STCOUNTER8, nCount );
else if ( m_strNumber[nCount] == '9' ) Draw( pDC, STCOUNTER9, nCount );
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -