⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sevensegnumctl.cpp

📁 数码管显示控件源代码
💻 CPP
字号:
// SevenSegNumCtl.cpp : Implementation of the CSevenSegNumCtrl ActiveX Control class.

#include "stdafx.h"
#include "SevenSegNum.h"
#include "SevenSegNumCtl.h"
#include "SevenSegNumPpg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


IMPLEMENT_DYNCREATE(CSevenSegNumCtrl, COleControl)


/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CSevenSegNumCtrl, COleControl)
	//{{AFX_MSG_MAP(CSevenSegNumCtrl)
	// NOTE - ClassWizard will add and remove message map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CSevenSegNumCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CSevenSegNumCtrl)
	DISP_FUNCTION(CSevenSegNumCtrl, "SetValue", SetValue, VT_EMPTY, VTS_I2)
	DISP_FUNCTION(CSevenSegNumCtrl, "SetBackColor", SetBackColor, VT_EMPTY, VTS_I4)
	DISP_FUNCTION(CSevenSegNumCtrl, "SetForeColor1", SetForeColor1, VT_EMPTY, VTS_I4)
	DISP_FUNCTION(CSevenSegNumCtrl, "SetForeColor2", SetForeColor2, VT_EMPTY, VTS_I4)
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CSevenSegNumCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CSevenSegNumCtrl, COleControl)
	//{{AFX_EVENT_MAP(CSevenSegNumCtrl)
	// NOTE - ClassWizard will add and remove event map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CSevenSegNumCtrl, 1)
	PROPPAGEID(CSevenSegNumPropPage::guid)
END_PROPPAGEIDS(CSevenSegNumCtrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CSevenSegNumCtrl, "SEVENSEGNUM.SevenSegNumCtrl.1",
	0x61169e8a, 0x2a92, 0x49f2, 0xa3, 0x15, 0x9d, 0xb3, 0x56, 0xd9, 0x5d, 0xfa)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CSevenSegNumCtrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DSevenSegNum =
		{ 0x2ce5402c, 0xe606, 0x48e7, { 0x99, 0x10, 0x28, 0x13, 0x30, 0xdb, 0x8f, 0x5d } };
const IID BASED_CODE IID_DSevenSegNumEvents =
		{ 0x6814f233, 0xe9b7, 0x4430, { 0xb5, 0x91, 0xb4, 0xc0, 0x49, 0xb1, 0xb0, 0x59 } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwSevenSegNumOleMisc =
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CSevenSegNumCtrl, IDS_SEVENSEGNUM, _dwSevenSegNumOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CSevenSegNumCtrl::CSevenSegNumCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CSevenSegNumCtrl

BOOL CSevenSegNumCtrl::CSevenSegNumCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_SEVENSEGNUM,
			IDB_SEVENSEGNUM,
			afxRegApartmentThreading,
			_dwSevenSegNumOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CSevenSegNumCtrl::CSevenSegNumCtrl - Constructor

CSevenSegNumCtrl::CSevenSegNumCtrl()
{
	InitializeIIDs(&IID_DSevenSegNum, &IID_DSevenSegNumEvents); 
chVal=48; //初始显示为0 
lForeColor1=RGB(255,0,0); //数码颜色 
lForeColor2=RGB(166,154,151); //不显示的段的颜色 
lBackColor=RGB(192,192,192); //控件背景 

}


/////////////////////////////////////////////////////////////////////////////
// CSevenSegNumCtrl::~CSevenSegNumCtrl - Destructor

CSevenSegNumCtrl::~CSevenSegNumCtrl()
{
	// TODO: Cleanup your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CSevenSegNumCtrl::OnDraw - Drawing function

void CSevenSegNumCtrl::OnDraw( CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid) 
{ 
//共七段,每段分为7个点,画7个封闭的多边形,表示7段的每一段 
POINT p0[7],p1[7],p2[7],p3[7],p4[7],p5[7],p6[7]; 
int iLineWidth;//段的宽度 
int iUnit=3;//将段的宽度分为3份,决定每段的起始和终止部分的斜线夹角 
//取控件宽度和高度数值小的数值来决定段宽 
if(rcBounds.Height() < rcBounds.Width()){ 
iLineWidth=rcBounds.Height()/10; //缺省段宽为控件宽度或高度的1/10 
} 
else{ 
iLineWidth=rcBounds.Width()/10; 
} 
//第零个段 
p0[0].x=iLineWidth/iUnit; 
p0[0].y=iLineWidth/iUnit; 
p0[1].x=iLineWidth/iUnit*(iUnit-1); 
p0[1].y=rcBounds.top; 
p0[2].x=rcBounds.right - iLineWidth/iUnit*(iUnit-1); 
p0[2].y=rcBounds.top; 
p0[3].x=rcBounds.right - iLineWidth/iUnit; 
p0[3].y=iLineWidth/iUnit; 
p0[4].x=rcBounds.right - iLineWidth; 
p0[4].y=iLineWidth; 
p0[5].x=iLineWidth; 
p0[5].y=iLineWidth; 
p0[6].x=iLineWidth/iUnit; 
p0[6].y=iLineWidth/iUnit; 
//第一个段 
p1[0].x=rcBounds.right - iLineWidth/iUnit; 
p1[0].y=iLineWidth/iUnit*2; 
p1[1].x=rcBounds.right ; 
p1[1].y=iLineWidth/iUnit*3 ; 
p1[2].x=rcBounds.right; 
p1[2].y=rcBounds.Height()/2 - iLineWidth/iUnit ; 
p1[3].x=rcBounds.right - iLineWidth/iUnit; 
p1[3].y=rcBounds.Height()/2; 
p1[4].x=rcBounds.right - iLineWidth; 
p1[4].y=rcBounds.Height()/2 - iLineWidth/iUnit*(iUnit-1); 
p1[5].x=rcBounds.right - iLineWidth; 
p1[5].y=iLineWidth/iUnit*(iUnit+1); 
p1[6].x=rcBounds.right - iLineWidth/iUnit; 
p1[6].y=iLineWidth/iUnit*2; 
//第二个段 
p2[0].x=rcBounds.right - iLineWidth/iUnit; 
p2[0].y=rcBounds.Height()/2; 
p2[1].x=rcBounds.right ; 
p2[1].y=rcBounds.Height()/2 + iLineWidth/iUnit ; 
p2[2].x=rcBounds.right; 
p2[2].y=rcBounds.Height() - iLineWidth/iUnit*3 ; 
p2[3].x=rcBounds.right - iLineWidth/iUnit; 
p2[3].y=rcBounds.Height() - iLineWidth/iUnit*2; 
p2[4].x=rcBounds.right - iLineWidth; 
p2[4].y=rcBounds.Height() - iLineWidth/iUnit*(iUnit+1); 
p2[5].x=rcBounds.right - iLineWidth; 
p2[5].y=rcBounds.Height()/2 + iLineWidth/iUnit*(iUnit-1); 
p2[6].x=rcBounds.right - iLineWidth/iUnit; 
p2[6].y=rcBounds.Height()/2; 
//第三个段 
p3[0].x=iLineWidth/iUnit; 
p3[0].y=rcBounds.Height()- iLineWidth/iUnit; 
p3[1].x=iLineWidth/iUnit*(iUnit-1); 
p3[1].y=rcBounds.bottom; 
p3[2].x=rcBounds.right - iLineWidth/iUnit*(iUnit-1); 
p3[2].y=rcBounds.bottom; 
p3[3].x=rcBounds.right - iLineWidth/iUnit; 
p3[3].y=rcBounds.bottom -iLineWidth/iUnit; 
p3[4].x=rcBounds.right - iLineWidth; 
p3[4].y=rcBounds.Height() -iLineWidth; 
p3[5].x=iLineWidth; 
p3[5].y=rcBounds.Height()-iLineWidth; 
p3[6].x=iLineWidth/iUnit; 
p3[6].y=rcBounds.Height() - iLineWidth/iUnit; 
//第四个段 
p4[0].x=iLineWidth/iUnit; 
p4[0].y=rcBounds.Height()/2; 
p4[1].x=rcBounds.left ; 
p4[1].y=rcBounds.Height()/2 + iLineWidth/iUnit ; 
p4[2].x=rcBounds.left; 
p4[2].y=rcBounds.Height() - iLineWidth/iUnit*3 ; 
p4[3].x=iLineWidth/iUnit; 
p4[3].y=rcBounds.Height() - iLineWidth/iUnit*2; 
p4[4].x=iLineWidth; 
p4[4].y=rcBounds.Height() - iLineWidth/iUnit*(iUnit+1); 
p4[5].x=iLineWidth; 
p4[5].y=rcBounds.Height()/2 + iLineWidth/iUnit*(iUnit-1); 
p4[6].x=iLineWidth/iUnit; 
p4[6].y=rcBounds.Height()/2; 
//第五个段 
p5[0].x=iLineWidth/iUnit; 
p5[0].y=iLineWidth/iUnit*2; 
p5[1].x=rcBounds.left ; 
p5[1].y=iLineWidth/iUnit*3 ; 
p5[2].x=rcBounds.left; 
p5[2].y=rcBounds.Height()/2 - iLineWidth/iUnit ; 
p5[3].x=iLineWidth/iUnit; 
p5[3].y=rcBounds.Height()/2; 
p5[4].x=iLineWidth; 
p5[4].y=rcBounds.Height()/2 - iLineWidth/iUnit*(iUnit-1); 
p5[5].x=iLineWidth; 
p5[5].y=iLineWidth/iUnit*(iUnit+1); 
p5[6].x=iLineWidth/iUnit; 
p5[6].y=iLineWidth/iUnit*2; 
//第六个段 
p6[0].x=iLineWidth/iUnit*2; 
p6[0].y=rcBounds.Height()/2; 
p6[1].x=iLineWidth/iUnit*(iUnit+1); 
p6[1].y=rcBounds.Height()/2 - iLineWidth/2 ; 
p6[2].x=rcBounds.right - iLineWidth/iUnit*(iUnit+1); 
p6[2].y=rcBounds.Height()/2 - iLineWidth/2 ; 
p6[3].x=rcBounds.right - iLineWidth/iUnit*2; 
p6[3].y=rcBounds.Height()/2; 
p6[4].x=rcBounds.right - iLineWidth/iUnit*(iUnit+1); 
p6[4].y=rcBounds.Height()/2 + iLineWidth/2 ; 
p6[5].x=iLineWidth/iUnit*(iUnit+1); 
p6[5].y=rcBounds.Height()/2 + iLineWidth/2 ; 
p6[6].x=iLineWidth/iUnit*2; 
p6[6].y=rcBounds.Height()/2; 
//产生封闭多边形的内部颜色画刷(分为要显示的段的颜色画刷,不需显示的段的颜色画刷),控件的背景颜色画刷 
CBrush brRgn,brRect,brBackRgn; 
brRgn.CreateSolidBrush(lForeColor1); 
brRect.CreateSolidBrush(lBackColor); 
brBackRgn.CreateSolidBrush(lForeColor2); 
//填充控件背景 
pdc->FillRect(rcBounds,&brRect); 
//根据上面的7个数组,产生7个区域,绘制7个段 
CRgn rgn0,rgn1,rgn2,rgn3,rgn4,rgn5,rgn6; 
rgn0.CreatePolygonRgn(p0,7,ALTERNATE); 
rgn1.CreatePolygonRgn(p1,7,ALTERNATE); 
rgn2.CreatePolygonRgn(p2,7,ALTERNATE); 
rgn3.CreatePolygonRgn(p3,7,ALTERNATE); 
rgn4.CreatePolygonRgn(p4,7,ALTERNATE); 
rgn5.CreatePolygonRgn(p5,7,ALTERNATE); 
rgn6.CreatePolygonRgn(p6,7,ALTERNATE); 
//根据数码的不同,决定显示的段 
switch(chVal){ 
case 0: 
pdc->FillRgn(&rgn0,&brRgn), pdc->FillRgn(&rgn1,&brRgn); 
pdc->FillRgn(&rgn2,&brRgn), pdc->FillRgn(&rgn3,&brRgn); 
pdc->FillRgn(&rgn4,&brRgn), pdc->FillRgn(&rgn5,&brRgn); 
pdc->FillRgn(&rgn6,&brBackRgn); 
break; 
case 1: 
pdc->FillRgn(&rgn0,&brBackRgn), pdc->FillRgn(&rgn1,&brRgn); 
pdc->FillRgn(&rgn2,&brRgn), pdc->FillRgn(&rgn3,&brBackRgn); 
pdc->FillRgn(&rgn4,&brBackRgn),pdc->FillRgn(&rgn5,&brBackRgn); 
pdc->FillRgn(&rgn6,&brBackRgn); 
break; 
case 2: 
pdc->FillRgn(&rgn0,&brRgn), pdc->FillRgn(&rgn1,&brRgn); 
pdc->FillRgn(&rgn2,&brBackRgn),pdc->FillRgn(&rgn3,&brRgn); 
pdc->FillRgn(&rgn4,&brRgn), pdc->FillRgn(&rgn5,&brBackRgn); 
pdc->FillRgn(&rgn6,&brRgn); 
break; 
case 3: 
pdc->FillRgn(&rgn0,&brRgn), pdc->FillRgn(&rgn1,&brRgn); 
pdc->FillRgn(&rgn2,&brRgn), pdc->FillRgn(&rgn3,&brRgn); 
pdc->FillRgn(&rgn4,&brBackRgn),pdc->FillRgn(&rgn5,&brBackRgn); 
pdc->FillRgn(&rgn6,&brRgn); 
break; 
case 4: 
pdc->FillRgn(&rgn0,&brBackRgn),pdc->FillRgn(&rgn1,&brRgn); 
pdc->FillRgn(&rgn2,&brRgn), pdc->FillRgn(&rgn3,&brBackRgn); 
pdc->FillRgn(&rgn4,&brBackRgn),pdc->FillRgn(&rgn5,&brRgn); 
pdc->FillRgn(&rgn6,&brRgn); 
break; 
case 5: 
pdc->FillRgn(&rgn0,&brRgn),pdc->FillRgn(&rgn1,&brBackRgn); 
pdc->FillRgn(&rgn2,&brRgn), pdc->FillRgn(&rgn3,&brRgn); 
pdc->FillRgn(&rgn4,&brBackRgn),pdc->FillRgn(&rgn5,&brRgn); 
pdc->FillRgn(&rgn6,&brRgn); 
break; 
case 6: 
pdc->FillRgn(&rgn0,&brRgn), pdc->FillRgn(&rgn1,&brBackRgn); 
pdc->FillRgn(&rgn2,&brRgn), pdc->FillRgn(&rgn3,&brRgn); 
pdc->FillRgn(&rgn4,&brRgn),pdc->FillRgn(&rgn5,&brRgn); 
pdc->FillRgn(&rgn6,&brRgn); 
break; 
case 7: 
pdc->FillRgn(&rgn0,&brRgn), pdc->FillRgn(&rgn1,&brRgn); 
pdc->FillRgn(&rgn2,&brRgn), pdc->FillRgn(&rgn3,&brBackRgn); 
pdc->FillRgn(&rgn4,&brBackRgn), pdc->FillRgn(&rgn5,&brBackRgn); 
pdc->FillRgn(&rgn6,&brBackRgn); 
break; 
case 8: 
pdc->FillRgn(&rgn0,&brRgn),pdc->FillRgn(&rgn1,&brRgn); 
pdc->FillRgn(&rgn2,&brRgn), pdc->FillRgn(&rgn3,&brRgn); 
pdc->FillRgn(&rgn4,&brRgn), pdc->FillRgn(&rgn5,&brRgn); 
pdc->FillRgn(&rgn6,&brRgn); 
break; 
case 9: 
pdc->FillRgn(&rgn0,&brRgn), pdc->FillRgn(&rgn1,&brRgn); 
pdc->FillRgn(&rgn2,&brRgn), pdc->FillRgn(&rgn3,&brRgn); 
pdc->FillRgn(&rgn4,&brBackRgn), pdc->FillRgn(&rgn5,&brRgn); 
pdc->FillRgn(&rgn6,&brRgn); 
break; 
default: 
pdc->FillRgn(&rgn0,&brRgn), pdc->FillRgn(&rgn1,&brRgn); 
pdc->FillRgn(&rgn2,&brRgn), pdc->FillRgn(&rgn3,&brRgn); 
pdc->FillRgn(&rgn4,&brRgn), pdc->FillRgn(&rgn5,&brRgn); 
pdc->FillRgn(&rgn6,&brRgn); 
break; 
} 
} 



/////////////////////////////////////////////////////////////////////////////
// CSevenSegNumCtrl::DoPropExchange - Persistence support

void CSevenSegNumCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	// TODO: Call PX_ functions for each persistent custom property.

}


/////////////////////////////////////////////////////////////////////////////
// CSevenSegNumCtrl::OnResetState - Reset control to default state

void CSevenSegNumCtrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}


/////////////////////////////////////////////////////////////////////////////
// CSevenSegNumCtrl::AboutBox - Display an "About" box to the user

void CSevenSegNumCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_SEVENSEGNUM);
	dlgAbout.DoModal();
}


/////////////////////////////////////////////////////////////////////////////
// CSevenSegNumCtrl message handlers

void CSevenSegNumCtrl::SetValue(short chValue) 
{
chVal=chValue; 
//刷新控件 
CRect rc; 
GetClientRect(&rc); 
InvalidateRect(rc,TRUE);

}

void CSevenSegNumCtrl::SetBackColor(long Backcolor) 
{
lBackColor=Backcolor; 
CRect rc; 
GetClientRect(&rc); 
InvalidateRect(rc,TRUE);  

}

void CSevenSegNumCtrl::SetForeColor1(long ForeColor1) 
{
lForeColor1=ForeColor1; 
CRect rc; 
GetClientRect(&rc); 
InvalidateRect(rc,TRUE);  


}

void CSevenSegNumCtrl::SetForeColor2(long ForeColor2) 
{
lForeColor2=ForeColor2; 
CRect rc; 
GetClientRect(&rc); 
InvalidateRect(rc,TRUE);  


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -