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

📄 clockctl.cpp

📁 这个是我们学校用的VC++教案
💻 CPP
字号:
// ClockCtl.cpp : Implementation of the CClockCtrl ActiveX Control class.

#include "stdafx.h"
#include "Clock.h"
#include "ClockCtl.h"
#include "ClockPpg.h"

#include "math.h"

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


IMPLEMENT_DYNCREATE(CClockCtrl, COleControl)


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

BEGIN_MESSAGE_MAP(CClockCtrl, COleControl)
	//{{AFX_MSG_MAP(CClockCtrl)
	ON_WM_CREATE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


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

BEGIN_DISPATCH_MAP(CClockCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CClockCtrl)
	DISP_PROPERTY_NOTIFY(CClockCtrl, "IfDigitShape", m_ifDigitShape, OnIfDigitShapeChanged, VT_BOOL)
	DISP_PROPERTY_NOTIFY(CClockCtrl, "UpdateInterval", m_updateInterval, OnUpdateIntervalChanged, VT_I4)
	DISP_STOCKPROP_BACKCOLOR()
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CClockCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


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

BEGIN_EVENT_MAP(CClockCtrl, COleControl)
	//{{AFX_EVENT_MAP(CClockCtrl)
	EVENT_STOCK_CLICK()
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


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

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CClockCtrl, 2)
	PROPPAGEID(CClockPropPage::guid)
	PROPPAGEID(CLSID_CColorPropPage)  // 颜色属性页
END_PROPPAGEIDS(CClockCtrl)


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

IMPLEMENT_OLECREATE_EX(CClockCtrl, "CLOCK.ClockCtrl.1",
	0xb3b06026, 0x5309, 0x11d6, 0x88, 0x8d, 0x9f, 0x18, 0x9e, 0x3e, 0xc1, 0x70)


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

IMPLEMENT_OLETYPELIB(CClockCtrl, _tlid, _wVerMajor, _wVerMinor)


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

const IID BASED_CODE IID_DClock =
		{ 0xb3b06024, 0x5309, 0x11d6, { 0x88, 0x8d, 0x9f, 0x18, 0x9e, 0x3e, 0xc1, 0x70 } };
const IID BASED_CODE IID_DClockEvents =
		{ 0xb3b06025, 0x5309, 0x11d6, { 0x88, 0x8d, 0x9f, 0x18, 0x9e, 0x3e, 0xc1, 0x70 } };


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

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

IMPLEMENT_OLECTLTYPE(CClockCtrl, IDS_CLOCK, _dwClockOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CClockCtrl::CClockCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CClockCtrl

BOOL CClockCtrl::CClockCtrlFactory::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_CLOCK,
			IDB_CLOCK,
			afxRegApartmentThreading,
			_dwClockOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CClockCtrl::CClockCtrl - Constructor

CClockCtrl::CClockCtrl()
{
	InitializeIIDs(&IID_DClock, &IID_DClockEvents);
	SetInitialSize(150,120);  // 设置控件在容器中第一次显示时的大小为200像素,宽为200像素。
	// TODO: Initialize your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CClockCtrl::~CClockCtrl - Destructor

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


/////////////////////////////////////////////////////////////////////////////
// CClockCtrl::OnDraw - Drawing function

void CClockCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	// TODO: Replace the following code with your own drawing code.
	//pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
	//pdc->Ellipse(rcBounds);
	CRect  Rect;
	::GetClientRect(this->m_hWnd,&Rect);
	int  nCenterX=Rect.right/2;  // 计算得到客户区域的中心坐标
	int  nCenterY=Rect.bottom/2;
	pdc->SetTextColor(TranslateColor(GetForeColor()));
	pdc->SetBkMode(TRANSPARENT);  // 将控件背景颜色设置为透明方式
	// 建立一个刷子,该画刷的颜色为背景所设置的颜色
	CBrush*  pBrush=new  CBrush(TranslateColor(GetBackColor()));
	pdc->FillRect(&Rect,pBrush);
	delete  pBrush;
	CFont*  pOldFont;
	pOldFont=SelectStockFont(pdc);  // 选择库存字体
	CTime  Time=CTime::GetCurrentTime();
	if (m_ifDigitShape)  // 如果是数字时钟,在表盘中以电子表的形式显示当前时间
	{
		CString  CurrentTime=Time.Format("%B:%d:%Y %H:%M:%S");
		pdc->ExtTextOut(rcBounds.left, rcBounds.top, ETO_CLIPPED, 
		                       rcBounds, CurrentTime, strlen(CurrentTime), NULL);
	}
	else  // 模拟时钟的方式显示当前时间
	{
		CString  strDigits;
		int  i, x, y;
		CSize  size;
		CPen  Pen(PS_SOLID,5,RGB(0,0,255));  // 创建一支蓝笔来绘制椭圆
		CPen*  pOldPen=pdc->SelectObject(&Pen);  // 将新笔选入设备描述表
		pdc->Ellipse(5,5,Rect.right-5, Rect.bottom-5);  // 绘制时钟边界
		double  Radians;
		pdc->SetTextColor(RGB(255,0,0));  // 时钟上的文字选择红色
		for(i=1;i<=12;i++)
		{
			strDigits.Format("%d",i);  // 对输出的时钟数字进行格式化
			// 获得时钟数值的范围,以便计算中心
			size=pdc->GetTextExtent(strDigits, strDigits.GetLength());
			Radians=(double)i*6.28/12.0;  // 计算文本输出的位置
			x=nCenterX-(size.cx/2)+(int)((double)(nCenterX-20)*sin(Radians));
			y=nCenterY-(size.cy/2)-(int)((double)(nCenterY-20)*cos(Radians));
			pdc->TextOut(x,y,strDigits);  // 显示钟表盘上的数字
		}
		Radians=(double)Time.GetHour() + (double)Time.GetMinute()/60.0  //计算时针半径
                                       + (double)Time.GetSecond()/3600.0; 
		Radians*=6.28/12.0;
		CPen  HourPen(PS_SOLID,5,RGB(0,255,0));  // 创建绘制时针的画笔
		pdc->SelectObject(&HourPen);  // 将该笔选入设备描述表
		pdc->MoveTo(nCenterX,nCenterY);  // 绘制时针
		pdc->LineTo(nCenterX+(int)((double)(nCenterX/3)*sin(Radians)),
						nCenterY-(int)((double)(nCenterY/3)*cos(Radians)));
		Radians=(double)Time.GetMinute()+(double)Time.GetSecond()/60.0;  //计算分针半径
		Radians*=6.28/60.0;
		CPen  MinutePen(PS_SOLID,3,RGB(255,255,0));  // 创建绘制分针的画笔
		pdc->SelectObject(&MinutePen);  // 将该笔选入设备描述表
		pdc->MoveTo(nCenterX,nCenterY);  // 绘制分针
		pdc->LineTo(nCenterX+(int)((double)((nCenterX*2)/3)*sin(Radians)),
						nCenterY-(int)((double)((nCenterY*2)/3)*cos(Radians)));
		Radians=(double)Time.GetSecond();  // 计算秒针半径
		Radians*=6.28/60.0;
		CPen  SecondPen(PS_SOLID,1,RGB(0,255,255));  // 创建绘制秒针的画笔
		pdc->SelectObject(&SecondPen);  // 将该笔选入设备描述表
		pdc->MoveTo(nCenterX,nCenterY);  //绘制秒针
		pdc->LineTo(nCenterX+(int)((double)((nCenterX*4)/5)*sin(Radians)),
						nCenterY-(int)((double)((nCenterY*4)/5)*cos(Radians)));
		pdc->SelectObject(pOldPen);  // 恢复设备描述表中的旧笔
	}

}


/////////////////////////////////////////////////////////////////////////////
// CClockCtrl::DoPropExchange - Persistence support

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

	// TODO: Call PX_ functions for each persistent custom property.
	PX_Bool(pPX,"IfDigitShape",m_ifDigitShape,TRUE);
	PX_Long(pPX,"UpdateInterval",m_updateInterval,1000);

}


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

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

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


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

void CClockCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_CLOCK);
	dlgAbout.DoModal();
}


/////////////////////////////////////////////////////////////////////////////
// CClockCtrl message handlers

BOOL CClockCtrl::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	KillTimer(m_timer);
	return COleControl::DestroyWindow();
}

int CClockCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (COleControl::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	m_timer=SetTimer(1, (UINT)m_updateInterval, NULL);  //建立定时器,时间间隔为m_updateInterval	
	return 0;
}

void CClockCtrl::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	InvalidateControl();
	COleControl::OnTimer(nIDEvent);
}

void CClockCtrl::OnIfDigitShapeChanged() 
{
	// TODO: Add notification handler code

	SetModifiedFlag();
}

void CClockCtrl::OnUpdateIntervalChanged() 
{
	// TODO: Add notification handler code

	SetModifiedFlag();
}

void CClockCtrl::OnClick(USHORT iButton) 
{
	// TODO: Add your specialized code here and/or call the base class
	// 单击控件时,切换钟表的显示方式
	m_ifDigitShape=!m_ifDigitShape;
	InvalidateControl();
	COleControl::OnClick(iButton);
}

⌨️ 快捷键说明

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