ActiveX控件1.新建工程新建一个MFCActiveXControlWizard,取名:Clock,完成,编译运行,点击黑三角块,选择ActiveXControlTestCoontainer,再选择Edit–InsertControl…-ClockControl-OK。注:在编译的时候,VC自动给生成的ActiveX控件注册了,在运行框中输入:Regsvr32/uD:\cprograms\C_WORKSPACE\Clock\Debug\Clock.ocx即可反注册。2.编辑时钟控件编辑CClockCtrl::OnDraw:voidCClockCtrl::OnDraw(CDC*pdc,constCRect&rcBounds,constCRect&rcInvalid){//TODO:Replacethefollowingcodewithyourowndrawingcode.//pdc-FillRect(rcBounds,CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));//填充背景//pdc-Ellipse(rcBounds);//画椭圆CTimetime=CTime::GetCurrentTime();//获取当前系统时间CStringstr=time.Format(%H:%M:%S);//将时间格式化并存放到字符串变量中pdc-TextOut(0,0,str);//在(0,0)的位置将时间输出}在CClockCtrl类上添加消息处理函数WM_CREATE和WM_TIMER,编辑:intCClockCtrl::OnCreate(LPCREATESTRUCTlpCreateStruct){if(COleControl::OnCreate(lpCreateStruct)==-1)return-1;SetTimer(1,1000,NULL);//设置时间间隔return0;}voidCClockCtrl::OnTimer(UINTnIDEvent){//TODO:Addyourmessagehandlercodehereand/orcalldefault//Invalidate();//使窗口无效,让窗口重绘InvalidateControl();//使整个时钟控件重绘COleControl::OnTimer(nIDEvent);}运行,添加控件,测试。也可以在其它程序中测试,如在VB中测试。3.给控件添加背景色与前景色ClassWizard….-Automation-AddProperty….-再选择一种属性,这样添加BackColor和ForeColor,并选中Stock选项。ActiveX控件的四种属性:在CClockCtrl::OnDraw函数中编辑:voidCClockCtrl::OnDraw(CDC*pdc,constCRect&rcBounds,constCRect&rcInvalid){//TODO:Replacethefollowingcodewithyourowndrawingcode.//pdc-FillRect(rcBounds,CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));//填充背景//pdc-Ellipse(rcBounds);//画椭圆CBrushbrush(TranslateColor(GetBackColor()));//获得控件的背景色OLE_COLOR,并将其转换成COLORREF,再设置到创建的画刷上pdc-FillRect(rcBounds,&brush);//填充背景色//注意:SetBkColor()是用于设置背景色的属性值pdc-SetBkMode(TRANSPARENT);//将文字的背景色设置成透明pdc-SetTextColor(TranslateColor(GetForeColor()));//设置前景色CTimetime=CTime::GetCurrentTime();//获取当前系统时间CStringstr=time.Format(%H:%M:%S);//将时间格式化并存放到字符串变量中pdc-TextOut(0,0,str);//在(0,0)的位置将时间输出}4.在属性表中添加设置颜色的属性页在ClockCtl.cpp中编辑://Propertypages//TODO:Addmorepropertypagesasneeded.Remembertoincreasethecount!BEGIN_PROPPAGEIDS(CClockCtrl,2)//2表示属性页的数量PROPPAGEID(CClockPropPage::guid)PROPPAGEID(CLSID_CColorPropPage)//添加颜色属性页END_PROPPAGEIDS(CClockCtrl)运行,测试OK!5.自定义属性页ClassWizard….-Automation-AddProperty….-手动添加一种属性Interval,类型选择short,implementation选择Membervariable,编辑CClockCtrl::OnIntervalChanged函数:voidCClockCtrl::OnIntervalChanged(){//TODO:Addnotificationhandlercodeif(m_interval0||m_interval6000){m_interval=1000;}else{m_interval=m_interval/1000*1000;//将用户的输入取整KillTimer(1);//销毁先前的定时器SetTimer(1,m_interval,NULL);//设置定时器}SetModifiedFlag();}编辑IDD_PROPPAGE_CLOCK属性对话框,如下图:并对编辑框关联一个变量,右键-ClassWizard…,如下图:6.给控件添加方法和事件添加方法:View-classwizard-Automation()-AddMethod-(Extendalname:Hello,Intervalname:Hello,Returntype:void)-OK-OK。编辑CClockCtrl::Hello()函数:voidCClockCtrl::Hello(){MessageBox(HelloWorld!);}添加标准事件:View-classwizard-ActiveXEvents-(Externalname:Click,Implementation:Stock)-OK-OK。添加自定义事件:对DClockEvents-右键-AddEvent…-(Externalname:NewMinute),接着在CClockCtrl::OnDraw中编辑:voidCClockCtrl::OnDraw(CDC*pdc,constCRect&rcBounds,constCRect&rcInvalid){........................CTimetime=CTime::GetCurrentTime();//获取当前系统时间if(0==time.GetSecond())//当秒数到达,进入另一分钟时{FireNewMinute();//发出事件通知}CStringstr=time.Format(%H:%M:%S);//将时间格式化并存放到字符串变量中pdc-TextOut(0,0,str);//在(0,0)的位置将时间输出}7.使控件自定义的属性持久保存在CClockCtrl::DoPropExchange函数中编辑:voidCClockCtrl::DoPropExchange(CPropExchange*pPX){ExchangeVersion(pPX,MAKELONG(_wVerMinor,_wVerMajor));COleControl::DoPropExchange(pPX);//TODO:CallPX_functionsforeachpersistentcustomproperty.PX_Short(pPx,Interval,m_interval,1000);//使Interval属性具有持久性}并将CClockCtrl::OnCreate函数中修改:SetTimer(1,m_interval,NULL);//设置时间间隔在属性值改变时通知容器,在CClockCtrl::OnIntervalChanged函数中编辑:voidCClockCtrl::OnIntervalChanged(){....................SetTimer(1,m_interval,NULL);//设置定时器BoundPropertyChanged(0x1);//通知容器调度ID为1的属性发生了改变}SetModifiedFlag();}调度id说明:[uuid(DA664AEC-9974-4A9E-9A14-9F29A64C9910),helpstring(DispatchinterfaceforClockControl),hidden]dispinterface_DClock{properties://NOTE-ClassWizardwillmaintainpropertyinformationhere.//Useextremecautionwheneditingthissection.//{{AFX_ODL_PROP(CClockCtrl)[id(DISPID_BACKCOLOR),bindable,requestedit]OLE_COLORBackColor;[id(DISPID_FORECOLOR),bindable,requestedit]OLE_COLORForeColor;[id(1)]shortInterval;//1表示属性的调度ID//}}AFX_ODL_PROPmethods://NOTE-ClassWizardwillmaintainmethodinformationhere.//Useextremecautionwheneditingthissection.//{{AFX_ODL_METHOD(CClockCtrl)[id(2)]voidHello();//}}AFX_ODL_METHOD[id(DISPID_ABOUTBOX)]voidAboutBox();};8.让时间值只在运行时走动在CClockCtrl::OnTimer中编辑:voidCClockCtrl::OnTimer(UINTnIDEvent){//TODO:Addyourmessagehandlercodehereand/orcalldefaultif(AmbientUserMode())//如果返回为真,则表明是在运行时,重绘{//Invalidate();//使窗口无效,让窗口重绘InvalidateControl();//使整个时钟控件重绘}COleControl::OnTimer(nIDEvent);}9.编写一个控件测试程序新建一个基于对话框的MFC的对话框应用程序,取名ClockTest.在对话框资源上右键-插入ActiveX控件…-clockcontrol….,然后再按ctrl+T测试。插入控件:Project-addtoproject-conponentsandcontrols…-RegisteredActiveXControls-ClockControl-OK-OK,这样VC就会给这个工程添加一个CClock类,在资源中添加一个按钮,双击添加命令消息响应,在CClockTestDlg类上添加一个成员变量:private:CClockm_clock;并在ClockTestDlg.h中包含一个:#includeclock.