How About COM & ActiveX Control With Visual C++ 6.0 Author: Curtis CHOU mahler@ms16.hinet.net This document can be freely release and distribute without modify.
ACTIVEX CONTROLS... 3 ACTIVEX... 3 MFC ACTIVEX CONTROLWIZARD... 3 MFC ACTIVEX CONTROLS WIZARD... 4 MFC... 4... 5 ACTIVEX... 5 ONDRAW... 5 ONDRAW()... 6 ACTIVEX... 6 (STOCK PROPERTIES)... 6... 6 CLASSWIZARD... 9... 9... 10 GET/SET METHOD...11... 12... 13... 14 ACTIVEX... 16... 16 CLASSWIZARD... 16 CLASSWIZARD... 16 ACTIVEX (ACTIVEX CONTROL EVENT)... 18... 18... 18 (CUSTOM EVENT)... 20 ACTIVEX... 20 ACTIVEX... 20... 22... 24... 26... 27... 30 2 34 MFC
ActiveX Controls ActiveX MFC ActiveX ControlWizard 1. File -> New -> Project 2. Choose MFC ActiveX Control Wizard 3. 99 run-time license Help File 3 34 MFC
4. Window MFC ActiveX Controls Wizard Control module DLL CyyyApp Control CyyyCtrl Property Page CYyyPropPage MFC MFC COleControl ActiveX 4 34 MFC
COleControl CWnd Window Registry HKEY_CLASSES_ROOT HKEY_CLASSES_ROOTCLSID 128bit GUIDGlobal Unique Identifier DLL RegSvr32.exe DLL ActiveX OnDraw(CDC* pdc, const CRect& rcbounds, const CRect& rcinvalid) ; pdc drawing DC rcbounds rcinvalid MFC COleControl::InvalidateControl CWnd::Invalid OnDraw WMPAINT COleControl::OnPaint() DC OnDraw() COleControl::OnDraw() VB WM_PAINT COleControl::OnDraw() 5 34 MFC
ActiveX OnDraw() Active (InActive) (FrameWork) OnDraw() OnDraw() (0,0) DCDevice Context (0,0)DC (Stock Properties) ActiveX BackColor BorderStyle FillColor Font Enabled Text Caption VC ClassWizard 6 34 MFC
1. ClassWizard (Ctrl + W) 2. Automation (Control Class) Add Property 7 34 MFC
3. External name OK 4. ClassWizard 8 34 MFC
ClassWizard ActiveX ClassWizard COleControl::InvalidateControl() InvalidateControl InvalidateControl OnForeColorChanged() VC Dispatch map entry macro DISP_PROPERTY_NOTIFY (Member Variable) DISP_PROPERTY 9 34 MFC
Get/Set Method DISP_PROPERTY_EX DISP_PROPERTY_PARAM Get Set ActiveX Dispatch map entry macro 1. ClassWizard 2. Automation class name Add Property 3. External name 4. Type 5. Implementation Member Variable Get/Set Method 6. OK ClassWizard OK 10 34 MFC
Get/Set Method Get/Set Get : Set : Add Property 11 34 MFC
Add Property ClassWizard Edit Code Read-Only Write-Only Get Set Read-Only Set Write-Only Get 12 34 MFC
Get/Set Method Add Property Parameter List 15 13 34 MFC
MFC COleControl::GetAmbientProperty() Dispatch ID Dispatch ID OleCtl.h COleControl function AmbientBackColor AmbientDiaplayName AmbientFont AmbientForeColor AmbientLocaleID AmbientTextAlign AmbientUserMode AmbientUIDead ID Dispatch ID OleCtl.h OleCtl.h #define DISPID_AUTOSIZE (-500) #define DISPID_BACKCOLOR (-501) #define DISPID_BACKSTYLE (-502) #define DISPID_BORDERCOLOR (-503) #define DISPID_BORDERSTYLE (-504) #define DISPID_BORDERWIDTH (-505) #define DISPID_DRAWMODE (-507) #define DISPID_DRAWSTYLE (-508) #define DISPID_DRAWWIDTH (-509) 14 34 MFC
#define DISPID_FILLCOLOR (-510) #define DISPID_FILLSTYLE (-511) #define DISPID_FONT (-512) #define DISPID_FORECOLOR (-513) #define DISPID_ENABLED (-514) #define DISPID_HWND (-515) #define DISPID_TABSTOP (-516) #define DISPID_TEXT (-517) #define DISPID_CAPTION (-518) #define DISPID_BORDERVISIBLE (-519) #define DISPID_APPEARANCE (-520) 15 34 MFC
ActiveX ClassWizard DoClick Refresh DoClick() COleControl::DoClick() Refresh() CWnd::Invalidate() ClassWizard 1. ClassWizard Automation 2. Class Add Method 3. External name 4. OK Edit ClassWizard 1. ClassWizard Automation 16 34 MFC
2. Class Add Method 3. External name 4. Internal name 5. (Return Type) 6. Parameter 7. OK Edit 17 34 MFC
ActiveX (ActiveX Control Event) 15 ActiveX Control (Handler)COleControl Event Click DbClick Error KeyDown KeyUp MouseDown MouseUp MouseMove Click 1. ClassWizard 2. ActiveX Event CLASS NAME 18 34 MFC
3. [Add Event] External name 4. OK BEGIN_EVENT_MAP(CClockCtrl, COleControl) //{{AFX_EVENT_MAP(CClockCtrl) EVENT_STOCK_CLICK() //}}AFX_EVENT_MAP END_EVENT_MAP() 19 34 MFC
(Custom Event) 1. ClassWizard ActiveX Event 2. [Add Event] 3. External Name 4. Internal name FireXxxxXxxx 5. Parameter 6. OK EVENT MAP EVENT_CUSTOM() BEGIN_EVENT_MAP(CClockCtrl, COleControl) //{{AFX_EVENT_MAP(CClockCtrl) EVENT_CUSTOM("TestIt", FireTestIt, VTS_I2) EVENT_STOCK_CLICK() //}}AFX_EVENT_MAP END_EVENT_MAP() ActiveX ActiveX 20 34 MFC
VB ActiveX MFC Dialog Class 21 34 MFC
(Property Page) 1. Dialog DIALOG 2. DIALOG ActiveX 3. ClassWizard Dialog Class 4. Member Variable Control ID [Add Variable] 22 34 MFC
23 34 MFC
DoDataExchange DDX(Do Data Exchange) OK DoDataExchange() void CDialog2::DoDataExchange(CDataExchange* pdx) { } CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDialog2) DDX_Text(pDX, IDC_EDIT1, m_test1); //}}AFX_DATA_MAP DDX MFC Property Dialog DoDataExchange() DDP DDP_Text(pDX, IDC_CAPTION, Caption ) pdx IDCCAPTION IDCatpion ActiveX Caption DDP DDP_CBIndex combo box 24 34 MFC
DDP_CBString DDP_CBStringExact DDP_Check DDP_LBIndex DDP_LBString DDP_LBStringExact DDP_Radio DDP_Text combo box combo box Check Box list box list box list box Radio Button 25 34 MFC
DDP //{{AFX_DATA_MAP(CAlarmPropPage) DDP_Text(pDX, IDC_ALARM_HOUR, m_alarmhour, _T("AlarmHour") ); DDX_Text(pDX, IDC_ALARM_HOUR, m_alarmhour); DDV_MinMaxInt(pDX, m_alarmhour, 0, 23); DDP_Text(pDX, IDC_ALARM_MINUTE, m_alarmminute, _T("AlarmMinute") ); DDX_Text(pDX, IDC_ALARM_MINUTE, m_alarmminute); DDV_MinMaxInt(pDX, m_alarmminute, 0, 59); DDP_Text(pDX, IDC_ALARM_SOUND, m_alarmsound, _T("AlarmSound") ); DDX_Text(pDX, IDC_ALARM_SOUND, m_alarmsound); DDP_Text(pDX, IDC_ALARM_COMMAND, m_alarmcommand, _T("AlarmCommand") ); DDX_Text(pDX, IDC_ALARM_COMMAND, m_alarmcommand); DDP_Check(pDX, IDC_ALARM_SET, m_balarmset, _T("AlarmSet") ); DDX_Check(pDX, IDC_ALARM_SET, m_balarmset); DDP_CBIndex(pDX, IDC_ALARM_TYPE, m_alarmtype, _T("AlarmType") ); DDX_CBIndex(pDX, IDC_ALARM_TYPE, m_alarmtype); //}}AFX_DATA_MAP DDP_PostProcessing(pDX); MFC Font chooser Pricture chooser Color chooser Class ID CLSID_CFontPropPage CLSID_CPicturePropPage CLSID_CColorPropPage CXxxCtrl BEGIN_PROPPAGEIDS(CClockCtrl, 3) PROPPAGEID(CClockPropPage::guid) PROPPAGEID(CAlarmPropPage::guid) PROPPAGEID(CLSID_CColorPropPage) END_PROPPAGEIDS(CClockCtrl) 26 34 MFC
1. 2. 3. 1. Property Page 2. Property Page 3. Property Class 4. Property Page 1. 2. IDD_PROPPAGE_Xxxx(Xxxx ) 3. child window thin border 4. TitleBar Visible 5. 250 x 62 250 x 110 compiler i.. ClassWizard 27 34 MFC
ii. COlePropertyPage iii. OK 1. String Table ID IDS_Xxxx_PPG Xxxx Property Page 2. IDS_Xxxx_PPG_CAPTION Xxxx 3. UpdateRegistry() AfxOleRegisterPropertyPage BOOL CXxxx::CXxxxFactory::UpdateRegistry(BOOL bregister) { // TODO: Define string resource for page type; replace '0' below with ID. if (bregister) return AfxOleRegisterPropertyPageClass(AfxGetInstanceHandle(),m_clsid, IDS_Xxxx_PPG ); else return AfxOleUnregisterClass(m_clsid, NULL); } 4. COlePropertyPage() ID CXxxx::CXxxx() : { } COlePropertyPage(IDD, IDD_Xxxx_PPG_CAPTION) //{{AFX_DATA_INIT(CXxxx) // NOTE: ClassWizard will add member initialization here // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_DATA_INIT 28 34 MFC
5. map 29 34 MFC
1. ROC 2. 3. Class ClassWizard ROC m_broc BOOL 4. OK CLASSWIZARD EDIT CODE OnRocChanged() InvalidateControl() void CROCCtrl::OnROCChanged() { // TODO: Add notification handler code InvalidateControl(); SetModifiedFlag(); 30 34 MFC
} 5. CROCCtrl CString m_strroc ; CString m_strstd; 31 34 MFC
6. CROCCtrl::OnDraw // TODO: Replace the following code with your own drawing code. CBrush brush(rgb(128,128,0)) ; pdc->fillrect(rcbounds, &brush) ; int x = rcbounds.right / 3 ; int y = rcbounds.bottom / 4 ; if ( m_broc) pdc->textout(x, y, m_strroc) ; else pdc->textout(x, y, m_strstd) ; 7. CROCCtrl ClassWizard MESSAGE HANDLER WMCREATE OnCreate() WM_DESTROY OnDestroy(); 8. OnCreate() int CROCCtrl::OnCreate(LPCREATESTRUCT lpcreatestruct) { if (COleControl::OnCreate(lpCreateStruct) == -1) return -1; SetTimer(1, 1000, NULL) ; return 0; } 9. OnDestroy() void CROCCtrl::OnDestroy() { COleControl::OnDestroy(); KillTimer(1) ; } 10. ` TIMER ClassWizard WM_TIMER TIMER void CROCCtrl::OnTimer(UINT nidevent) { // TODO: Add your message handler code here and/or call default COleDateTime today = COleDateTime::GetCurrentTime() ; char year[5] ; char output[50] ; strcpy(output, " " ) ; itoa(today.getyear() - 1911, year, 10 ) ; 32 34 MFC
strcat(output, year ) ; strcat(output, "" ) ; m_strroc = output ; strcpy(output, " " ) ; itoa(today.getyear(), year, 10 ) ; strcat(output, year ) ; strcat(output, "" ) ; m_strstd = output ; } COleControl::OnTimer(nIDEvent); 11. Property Page 12. Resource Editor OlePropertyPage 13. IDD_PROPPAGE_SET String Table IDS_SET_PPG [ ] IDS_SET_PPG_CAPTION [ ] 14. ClassWizard CLASS CSetPropPage COlePropertyPage. UpdateRegistry() ID CROCCtrl Property Page Map 15. ClassWizard IDC_CHECK1 m_broc; BOOL 16. DDP CSetPropPage::OnDataExchange() void CSetPropPage::DoDataExchange(CDataExchange* pdx) { } // NOTE: ClassWizard will add DDP, DDX, and DDV calls here // DO NOT EDIT what you see in these blocks of generated code! //{{AFX_DATA_MAP(CSetPropPage) DDX_Check(pDX, IDC_CHECK1, m_broc); DDP_Check(pDX, IDC_CHECK1, m_broc, "ROC") ; //}}AFX_DATA_MAP DDP_PostProcessing(pDX); 33 34 MFC
34 34 MFC