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

📄 mytimectl.cpp

📁 Visual c++程序设计培训教程
💻 CPP
字号:
// MyTimeCtl.cpp : Implementation of the CMyTimeCtrl ActiveX Control class.

#include "stdafx.h"
#include "MyTime.h"
#include "MyTimeCtl.h"
#include "MyTimePpg.h"
#include "math.h"


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


IMPLEMENT_DYNCREATE(CMyTimeCtrl, COleControl)


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

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


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

BEGIN_DISPATCH_MAP(CMyTimeCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CMyTimeCtrl)
	DISP_STOCKPROP_BACKCOLOR()
	DISP_STOCKPROP_FORECOLOR()
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CMyTimeCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


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

BEGIN_EVENT_MAP(CMyTimeCtrl, COleControl)
	//{{AFX_EVENT_MAP(CMyTimeCtrl)
	EVENT_CUSTOM("NewHour", FireNewHour, VTS_I2)
	EVENT_CUSTOM("NewMinute", FireNewMinute, VTS_NONE)
	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(CMyTimeCtrl, 2)
	PROPPAGEID(CMyTimePropPage::guid)
	PROPPAGEID(CLSID_CColorPropPage)
END_PROPPAGEIDS(CMyTimeCtrl)


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

IMPLEMENT_OLECREATE_EX(CMyTimeCtrl, "MYTIME.MyTimeCtrl.1",
	0xaab72d27, 0x2a2b, 0x11d6, 0x9c, 0x7c, 0x52, 0x54, 0xab, 0x31, 0xb8, 0xa3)


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

IMPLEMENT_OLETYPELIB(CMyTimeCtrl, _tlid, _wVerMajor, _wVerMinor)


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

const IID BASED_CODE IID_DMyTime =
		{ 0xaab72d25, 0x2a2b, 0x11d6, { 0x9c, 0x7c, 0x52, 0x54, 0xab, 0x31, 0xb8, 0xa3 } };
const IID BASED_CODE IID_DMyTimeEvents =
		{ 0xaab72d26, 0x2a2b, 0x11d6, { 0x9c, 0x7c, 0x52, 0x54, 0xab, 0x31, 0xb8, 0xa3 } };


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

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

IMPLEMENT_OLECTLTYPE(CMyTimeCtrl, IDS_MYTIME, _dwMyTimeOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CMyTimeCtrl::CMyTimeCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CMyTimeCtrl

BOOL CMyTimeCtrl::CMyTimeCtrlFactory::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_MYTIME,
			IDB_MYTIME,
			afxRegApartmentThreading,
			_dwMyTimeOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CMyTimeCtrl::CMyTimeCtrl - Constructor

CMyTimeCtrl::CMyTimeCtrl()
{
	InitializeIIDs(&IID_DMyTime, &IID_DMyTimeEvents);

	// TODO: Initialize your control's instance data here.
	SetInitialSize(200, 230);  // 设置控件初始尺寸大小
}


/////////////////////////////////////////////////////////////////////////////
// CMyTimeCtrl::~CMyTimeCtrl - Destructor

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


/////////////////////////////////////////////////////////////////////////////
// CMyTimeCtrl::OnDraw - Drawing function

void CMyTimeCtrl::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);
	pdc->SetBkMode(TRANSPARENT);   // 设置背景透明

	//使用GetBackColor获取背景色属性,并构造一支画刷来填充控件
	CBrush bkBrush(TranslateColor(GetBackColor()));
	pdc->FillRect(rcBounds, &bkBrush);

	CTime time;
	time = CTime::GetCurrentTime();//获取系统时间



//	CRgn  rgn;
//	rgn.CreateEllipticRgn(10, 10, 190, 190);
	//pdc->FillRgn(&rgn, &bkBrush);

	double pi = 3.14159265;   // 圆周率
	int cx = rcBounds.left + 100;  // 时钟的中心位置x坐标
	int cy = rcBounds.top  + 100;  // 时钟的中心位置y坐标
  
	// 使用循环画出时钟盘面
	for(int i=0;i<60;i++)  
	{
		int x = cx + 80*cos(i*pi/30) + 0.5;
		int y = cy + 80*sin(i*pi/30) + 0.5;

		if(i%5 == 0)	pdc->Ellipse(x-3, y-3, x+3, y+3); // 大刻度
		else			pdc->Ellipse(x-1, y-1, x+1, y+1); // 小刻度
	}

	CString str = time.Format("%H:%M:%S");//将时间写入字符串

	//使用GetForeColor方法获取前景色
	COLORREF    forecolor = TranslateColor(GetForeColor());
	//设置文本颜色为前景色
	pdc->SetTextColor(forecolor);
	//输出字符串,在时钟下面以数字方式显示时间
	pdc->TextOut(rcBounds.left+70, rcBounds.top+200, str);

	//////////////////////////////////////////////////////////////
	// 下面将使用三种不同粗细的画笔来画出时钟的时针、分针和秒针
	//////////////////////////////////////////////////////////////
	CPen pen1(PS_SOLID, 4, forecolor);   // 时针画笔
	CPen pen2(PS_SOLID, 2, forecolor);   // 分针画笔
	CPen pen3(PS_SOLID, 1, forecolor);   // 秒针画笔

	//根据当前时间计算时针的终点位置(x1,y1)
	int x1 = cx + 40*cos((time.GetHour()-3)*pi/6 + 
		(time.GetMinute()/12)*pi/30) + 0.5;
	int y1 = cy + 40*sin((time.GetHour()-3)*pi/6 +
		(time.GetMinute()/12)*pi/30) + 0.5;
	//根据当前时间计算时针的终点位置(x1,y1)
	int x2 = cx + 60*cos((time.GetMinute()-15)*pi/30)  + 0.5;
	int y2 = cy + 60*sin((time.GetMinute()-15)*pi/30)  + 0.5;
	//根据当前时间计算时针的终点位置(x1,y1)
	int x3 = cx + 70*cos((time.GetSecond()-15)*pi/30)  + 0.5;
	int y3 = cy + 70*sin((time.GetSecond()-15)*pi/30)  + 0.5;

	CPen * pOldPen = pdc->SelectObject(&pen1);
	pdc->MoveTo(cx, cy);    // 画出时针
	pdc->LineTo(x1, y1);

	pdc->SelectObject(&pen2);
	pdc->MoveTo(cx, cy);    // 画出分针
	pdc->LineTo(x2, y2);

	pdc->SelectObject(&pen3);
	pdc->MoveTo(cx, cy);    // 画出秒针
	pdc->LineTo(x3, y3);

	pdc->SelectObject(pOldPen); // 落选画笔

	// 如果是正点(分钟数为0),则触发NewHour事件
	if(time.GetMinute() == 0)	FireNewHour(time.GetHour());// 使用当前小时数为参数
	// 如果是整分(秒数为0),则触发NewMinute事件
	if(time.GetSecond() == 0)	FireNewMinute();
}


/////////////////////////////////////////////////////////////////////////////
// CMyTimeCtrl::DoPropExchange - Persistence support

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

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

}


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

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

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


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

void CMyTimeCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_MYTIME);
	dlgAbout.DoModal();
}


/////////////////////////////////////////////////////////////////////////////
// CMyTimeCtrl message handlers

int CMyTimeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (COleControl::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	SetTimer(1,1000,NULL);  // 设置计时器
	return 0;
}

void CMyTimeCtrl::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	InvalidateControl();    // 重画控件,相当与调用OnDraw函数
	COleControl::OnTimer(nIDEvent);
}

void CMyTimeCtrl::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	KillTimer(1);    // 释放计时器
	CWnd::OnClose();
}

⌨️ 快捷键说明

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