// ClockCtl.cpp : Implementation of the CClockCtrl ActiveX Control class.
#include "stdafx.h"
#include "Clock.h"
#include "ClockCtl.h"
#include "ClockPpg.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, "Interval", m_interval, OnIntervalChanged, VT_I2)
DISP_FUNCTION(CClockCtrl, "Hello", Hello, VT_EMPTY, VTS_NONE)
DISP_STOCKPROP_BACKCOLOR()
DISP_STOCKPROP_FORECOLOR()
//}}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_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(CClockCtrl, 2) // 属性页的个数
PROPPAGEID(CClockPropPage::guid)
PROPPAGEID(CLSID_CColorPropPage) // 显示操作系统内置的颜色选择框
END_PROPPAGEIDS(CClockCtrl)
/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid
IMPLEMENT_OLECREATE_EX(CClockCtrl, "CLOCK.ClockCtrl.1",
0xd3927ffc, 0x24fa, 0x4e00, 0xb4, 0x6e, 0x4e, 0x12, 0x81, 0xdb, 0x7e, 0x42)
/////////////////////////////////////////////////////////////////////////////
// Type library ID and version
IMPLEMENT_OLETYPELIB(CClockCtrl, _tlid, _wVerMajor, _wVerMinor)
/////////////////////////////////////////////////////////////////////////////
// Interface IDs
const IID BASED_CODE IID_DClock =
{ 0x6071b2db, 0xe15a, 0x45a2, { 0x86, 0x83, 0x6, 0xd3, 0x68, 0x70, 0x9d, 0xfe } };
const IID BASED_CODE IID_DClockEvents =
{ 0xa6275980, 0xfa36, 0x4fdd, { 0x99, 0xc1, 0xd, 0x24, 0xf0, 0x3d, 0xa3, 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);
// TODO: Initialize your control's instance data here.
m_interval = 1000;
}
/////////////////////////////////////////////////////////////////////////////
// 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);
// 前景色,背景色
CBrush brush(TranslateColor(GetBackColor()));
pdc->FillRect(rcBounds, &brush);
pdc->SetTextColor(TranslateColor(GetForeColor()));
// 显示时间文字
CTime time = CTime::GetCurrentTime();
if (0 == time.GetSecond())
{
FireNewMinute();
}
CString str = time.Format("%H:%M:%S");
pdc->SetBkMode(TRANSPARENT);
pdc->TextOut(0, 0, str);
}
/////////////////////////////////////////////////////////////////////////////
// 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_Short(pPX, "Interval", m_interval, 1000); // 属性持久化即将属性(外部显示为Interval,内部显示为m_interval)的值存盘,默认值为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
int CClockCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetTimer(1, m_interval, NULL); // 定时刷新窗口
return 0;
}
void CClockCtrl::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
// VB中拖放控件并设置其属性这就属于设计模式
// 运行含有控件的VB EXE程序这就是运行模式
if (AmbientUserMode()) // 在运行模式下才刷新控件窗口
{
//Invalidate(); // 刷新窗口
InvalidateControl();
}
COleControl::OnTimer(nIDEvent);
}
void CClockCtrl::OnIntervalChanged()
{
// TODO: Add notification handler code
if (m_interval<0 || m_interval>6000)
{
m_interval = 1000;
}
else
{
m_interval = m_interval / 1000 * 1000;
}
KillTimer(1);
SetTimer(1, m_interval, NULL); // 使用属性中设置的值为定时器的值来刷新控件界面
SetModifiedFlag(); // 这句话必须写,是取值用户在属性中设置的值存入m_interval中用的
BoundPropertyChanged(0x1); // [id(1)] short Interval;中的属性方法的索引值0x1;通知控件所在的容器进行持久性存盘操作
}
void CClockCtrl::Hello()
{
// TODO: Add your dispatch handler code here
AfxMessageBox("Hello world!");
}