我在VS2008中创建了一个MFC ActiveX控件。我的控件类如下:
标题文件:
#pragma once
#include "Background_Dialog.h"
// Dual_Color_TrackbarCtrl.h : Declaration of the CDual_Color_TrackbarCtrl ActiveX Control class.
// CDual_Color_TrackbarCtrl : See Dual_Color_TrackbarCtrl.cpp for implementation.
class CDual_Color_TrackbarCtrl : public COleControl
{
DECLARE_DYNCREATE(CDual_Color_TrackbarCtrl)
friend class Background_Dialog;
// Constructor
public:
CDual_Color_TrackbarCtrl();
// Overrides
public:
virtual void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid);
virtual void DoPropExchange(CPropExchange* pPX);
virtual void OnResetState();
virtual DWORD GetControlFlags();
Background_Dialog m_View;
// Implementation
protected:
~CDual_Color_TrackbarCtrl();
DECLARE_OLECREATE_EX(CDual_Color_TrackbarCtrl) // Class factory and guid
DECLARE_OLETYPELIB(CDual_Color_TrackbarCtrl) // GetTypeInfo
DECLARE_PROPPAGEIDS(CDual_Color_TrackbarCtrl) // Property page IDs
DECLARE_OLECTLTYPE(CDual_Color_TrackbarCtrl) // Type name and misc status
// Message maps
DECLARE_MESSAGE_MAP()
// Dispatch maps
DECLARE_DISPATCH_MAP()
// Event maps
DECLARE_EVENT_MAP()
// Dispatch and event IDs
public:
enum {
eventidTestEvent = 1L,
dispidGetHighValue = 6L,
dispidGetLowValue = 5L,
dispidSetColor = 4L,
dispidSetLowValueText = 3L,
dispidSetHighValueText = 2L,
dispidSetRange = 1L
};
protected:
void SetRange(LONG low, LONG high);
void SetHighValueText(CHAR* str);
void SetLowValueText(CHAR* str);
void SetColor(ULONG color);
DOUBLE GetLowValue(void);
DOUBLE GetHighValue(void);
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
void SendEvent(void);
void TestEvent(void)
{
FireEvent(eventidTestEvent, EVENT_PARAM(VTS_NONE));
}
};
.cpp文件:
// Dual_Color_TrackbarCtrl.cpp : Implementation of the CDual_Color_TrackbarCtrl ActiveX Control class.
#include "stdafx.h"
#include "Dual_Color_Trackbar.h"
#include "Dual_Color_TrackbarCtrl.h"
#include "Dual_Color_TrackbarPropPage.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNCREATE(CDual_Color_TrackbarCtrl, COleControl)
// Message map
BEGIN_MESSAGE_MAP(CDual_Color_TrackbarCtrl, COleControl)
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
ON_WM_CREATE()
END_MESSAGE_MAP()
// Dispatch map
BEGIN_DISPATCH_MAP(CDual_Color_TrackbarCtrl, COleControl)
DISP_FUNCTION_ID(CDual_Color_TrackbarCtrl, "SetRange", dispidSetRange, SetRange, VT_EMPTY, VTS_I4 VTS_I4)
DISP_FUNCTION_ID(CDual_Color_TrackbarCtrl, "SetHighValueText", dispidSetHighValueText, SetHighValueText, VT_EMPTY, VTS_PI1)
DISP_FUNCTION_ID(CDual_Color_TrackbarCtrl, "SetLowValueText", dispidSetLowValueText, SetLowValueText, VT_EMPTY, VTS_PI1)
DISP_FUNCTION_ID(CDual_Color_TrackbarCtrl, "SetColor", dispidSetColor, SetColor, VT_EMPTY, VTS_UI4)
DISP_FUNCTION_ID(CDual_Color_TrackbarCtrl, "GetLowValue", dispidGetLowValue, GetLowValue, VT_R8, VTS_NONE)
DISP_FUNCTION_ID(CDual_Color_TrackbarCtrl, "GetHighValue", dispidGetHighValue, GetHighValue, VT_R8, VTS_NONE)
END_DISPATCH_MAP()
// Event map
BEGIN_EVENT_MAP(CDual_Color_TrackbarCtrl, COleControl)
EVENT_CUSTOM_ID("TestEvent", eventidTestEvent, TestEvent, VTS_NONE)
END_EVENT_MAP()
// Property pages
// TODO: Add more property pages as needed. Remember to increase the count!
BEGIN_PROPPAGEIDS(CDual_Color_TrackbarCtrl, 1)
PROPPAGEID(CDual_Color_TrackbarPropPage::guid)
END_PROPPAGEIDS(CDual_Color_TrackbarCtrl)
// Initialize class factory and guid
IMPLEMENT_OLECREATE_EX(CDual_Color_TrackbarCtrl, "DUAL_COLOR_TRACK.Dual_Color_TrackCtrl.1",
0x14e49ebd, 0x62e0, 0x496e, 0x86, 0xc, 0xe, 0x1a, 0xea, 0xca, 0x86, 0xf3)
// Type library ID and version
IMPLEMENT_OLETYPELIB(CDual_Color_TrackbarCtrl, _tlid, _wVerMajor, _wVerMinor)
// Interface IDs
const IID BASED_CODE IID_DDual_Color_Trackbar =
{ 0xA0566CF1, 0x2C81, 0x4295, { 0xAF, 0x52, 0xD7, 0x26, 0x9E, 0x90, 0x7F, 0xA3 } };
const IID BASED_CODE IID_DDual_Color_TrackbarEvents =
{ 0xA1916D8D, 0x9135, 0x41D8, { 0x8C, 0x82, 0x42, 0x21, 0x75, 0xE0, 0xD7, 0x2F } };
// Control type information
static const DWORD BASED_CODE _dwDual_Color_TrackbarOleMisc =
OLEMISC_ACTIVATEWHENVISIBLE |
OLEMISC_SETCLIENTSITEFIRST |
OLEMISC_INSIDEOUT |
OLEMISC_CANTLINKINSIDE |
OLEMISC_RECOMPOSEONRESIZE;
IMPLEMENT_OLECTLTYPE(CDual_Color_TrackbarCtrl, IDS_DUAL_COLOR_TRACKBAR, _dwDual_Color_TrackbarOleMisc)
// CDual_Color_TrackbarCtrl::CDual_Color_TrackbarCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CDual_Color_TrackbarCtrl
BOOL CDual_Color_TrackbarCtrl::CDual_Color_TrackbarCtrlFactory::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_DUAL_COLOR_TRACKBAR,
IDB_DUAL_COLOR_TRACKBAR,
afxRegApartmentThreading,
_dwDual_Color_TrackbarOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}
// CDual_Color_TrackbarCtrl::CDual_Color_TrackbarCtrl - Constructor
CDual_Color_TrackbarCtrl::CDual_Color_TrackbarCtrl()
{
InitializeIIDs(&IID_DDual_Color_Trackbar, &IID_DDual_Color_TrackbarEvents);
// TODO: Initialize your control's instance data here.
}
// CDual_Color_TrackbarCtrl::~CDual_Color_TrackbarCtrl - Destructor
CDual_Color_TrackbarCtrl::~CDual_Color_TrackbarCtrl()
{
// TODO: Cleanup your control's instance data here.
}
// CDual_Color_TrackbarCtrl::OnDraw - Drawing function
void CDual_Color_TrackbarCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
if (!pdc)
return;
m_View.MoveWindow(rcBounds);
CBrush bkgndColor(TranslateColor(AmbientBackColor()));
FillRect(*pdc,rcBounds,bkgndColor);
DeleteObject(bkgndColor);
}
// CDual_Color_TrackbarCtrl::DoPropExchange - Persistence support
void CDual_Color_TrackbarCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);
// TODO: Call PX_ functions for each persistent custom property.
}
// CDual_Color_TrackbarCtrl::GetControlFlags -
// Flags to customize MFC's implementation of ActiveX controls.
//
DWORD CDual_Color_TrackbarCtrl::GetControlFlags()
{
DWORD dwFlags = COleControl::GetControlFlags();
return dwFlags;
}
// CDual_Color_TrackbarCtrl::OnResetState - Reset control to default state
void CDual_Color_TrackbarCtrl::OnResetState()
{
COleControl::OnResetState(); // Resets defaults found in DoPropExchange
// TODO: Reset any other control state here.
}
// CDual_Color_TrackbarCtrl message handlers
void CDual_Color_TrackbarCtrl::SetRange(LONG low, LONG high)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: Add your dispatch handler code here
}
void CDual_Color_TrackbarCtrl::SetHighValueText(CHAR* str)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: Add your dispatch handler code here
}
void CDual_Color_TrackbarCtrl::SetLowValueText(CHAR* str)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: Add your dispatch handler code here
}
void CDual_Color_TrackbarCtrl::SetColor(ULONG color)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: Add your dispatch handler code here
}
DOUBLE CDual_Color_TrackbarCtrl::GetLowValue(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: Add your dispatch handler code here
return 0;
}
DOUBLE CDual_Color_TrackbarCtrl::GetHighValue(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: Add your dispatch handler code here
return 0;
}
int CDual_Color_TrackbarCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
m_View.Create(IDD_BACKGROUND,this);
return 0;
}
void CDual_Color_TrackbarCtrl::SendEvent(void)
{
this->TestEvent();
}
.idl文件
// Dual_Color_Trackbar.idl : type library source for ActiveX Control project.
// This file will be processed by the MIDL compiler tool to
// produce the type library (Dual_Color_Trackbar.tlb) that will become a resource in
// Dual_Color_Trackbar.ocx.
#include <olectl.h>
#include <idispids.h>
[ uuid(96F872A0-1522-4A93-97E1-CFF4BE483A65), version(1.0),
helpfile("Dual_Color_Trackbar.hlp"),
helpstring("Dual_Color_Trackbar ActiveX Control module"),
control ]
library Dual_Color_TrackbarLib
{
importlib(STDOLE_TLB);
// Primary dispatch interface for CDual_Color_TrackbarCtrl
[ uuid(A0566CF1-2C81-4295-AF52-D7269E907FA3),
helpstring("Dispatch interface for Dual_Color_Trackbar Control")]
dispinterface _DDual_Color_Trackbar
{
properties:
methods:
[id(1), helpstring("Sets the bounds of the Trackbar")] void SetRange(LONG low, LONG high);
[id(2), helpstring("Sets the string of the high edit control")] void SetHighValueText(CHAR* str);
[id(3), helpstring("Sets the string of the low value edit box.")] void SetLowValueText(CHAR* str);
[id(4), helpstring("Sets the color of the trackbar")] void SetColor(ULONG color);
[id(5), helpstring("Get the low value of the trackbar")] DOUBLE GetLowValue(void);
[id(6), helpstring("Gets the high value of the trackbar")] DOUBLE GetHighValue(void);
};
// Event dispatch interface for CDual_Color_TrackbarCtrl
[ uuid(A1916D8D-9135-41D8-8C82-422175E0D72F),
helpstring("Event interface for Dual_Color_Trackbar Control") ]
dispinterface _DDual_Color_TrackbarEvents
{
properties:
// Event interface has no properties
methods:
[id(1)] void TestEvent(void);
};
// Class information for CDual_Color_TrackbarCtrl
[ uuid(14E49EBD-62E0-496E-860C-0E1AEACA86F3),
helpstring("Dual_Color_Trackbar Control"), control ]
coclass Dual_Color_Trackbar
{
[default] dispinterface _DDual_Color_Trackbar;
[default, source] dispinterface _DDual_Color_TrackbarEvents;
};
};
我使用以下声明解雇我的事件:
CDual_Color_TrackbarCtrl ds;
ds.SendEvent();
在控件的朋友类中。
在我放置控件的客户端对话框中,我接收了我的事件,但处理程序函数永远不会被调用。我在控件的实现中遗漏了什么吗?我需要在客户端对话框应用程序中采取哪些步骤来确保可以捕获事件?