// $control_impl$ : $control_class$ ActiveX 控件类的实现。 #include "pch.h" #include "framework.h" #include "$projectname$.h" #include "$control_header$" #include "$property_page_header$" #include "afxdialogex.h" #ifdef _DEBUG #define new DEBUG_NEW #endif IMPLEMENT_DYNCREATE($control_class$, COleControl) // 消息映射 BEGIN_MESSAGE_MAP($control_class$, COleControl) [!if SUBCLASS_WINDOW] ON_MESSAGE(OCM_COMMAND, &$control_class$::OnOcmCommand) [!endif] [!if INSERTABLE] ON_OLEVERB(AFX_IDS_VERB_EDIT, OnEdit) [!endif] ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties) END_MESSAGE_MAP() // 调度映射 BEGIN_DISPATCH_MAP($control_class$, COleControl) [!if ASYNC_PROPERTY_LOAD] DISP_STOCKPROP_READYSTATE() [!endif] [!if ABOUT_BOX] DISP_FUNCTION_ID($control_class$, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE) [!endif] END_DISPATCH_MAP() // 事件映射 BEGIN_EVENT_MAP($control_class$, COleControl) [!if ASYNC_PROPERTY_LOAD] EVENT_STOCK_READYSTATECHANGE() [!endif] END_EVENT_MAP() // 属性页 // TODO: 根据需要添加更多属性页。请记住增加计数! BEGIN_PROPPAGEIDS($control_class$, 1) PROPPAGEID($property_page_class$::guid) END_PROPPAGEIDS($control_class$) // 初始化类工厂和 guid [!if CONTROL_TYPE_ID_SET] IMPLEMENT_OLECREATE_EX($control_class$, "$control_type_id$", $guid_control_clsid_olecreate$) [!else] IMPLEMENT_OLECREATE_NOREGNAME($control_class$, $guid_control_clsid_olecreate$) [!endif] // 键入库 ID 和版本 IMPLEMENT_OLETYPELIB($control_class$, _tlid, _wVerMajor, _wVerMinor) // 接口 ID const IID IID_D$safeprojectname$ = $guid_primary_iid_staticconst$; const IID IID_D$safeprojectname$Events = $guid_event_iid_staticconst$; // 控件类型信息 static const DWORD _dw$safeprojectname$OleMisc = [!if SIMPLE_FRAME] OLEMISC_SIMPLEFRAME | [!endif] [!if INVISIBLE_AT_RUNTIME] OLEMISC_INVISIBLEATRUNTIME | [!endif] [!if ACTIVATE_WHEN_VISIBLE] OLEMISC_ACTIVATEWHENVISIBLE | [!if MOUSE_NOTIFICATIONS] OLEMISC_IGNOREACTIVATEWHENVISIBLE | [!endif] [!endif] OLEMISC_SETCLIENTSITEFIRST | OLEMISC_INSIDEOUT | OLEMISC_CANTLINKINSIDE | OLEMISC_RECOMPOSEONRESIZE; IMPLEMENT_OLECTLTYPE($control_class$, IDS_$safercprojectname$, _dw$safeprojectname$OleMisc) // $control_class$::$control_class$Factory::UpdateRegistry - // 添加或移除 $control_class$ 的系统注册表项 BOOL $control_class$::$control_class$Factory::UpdateRegistry(BOOL bRegister) { // TODO: 验证您的控件是否符合单元模型线程处理规则。 // 有关更多信息,请参考 MFC 技术说明 64。 // 如果您的控件不符合单元模型规则,则 // 必须修改如下代码,将第六个参数从 [!if INSERTABLE] // afxRegInsertable | afxRegApartmentThreading 改为 afxRegInsertable。 [!else] // afxRegApartmentThreading 改为 0。 [!endif] if (bRegister) return AfxOleRegisterControlClass( AfxGetInstanceHandle(), m_clsid, m_lpszProgID, IDS_$safercprojectname$, IDB_$safercprojectname$, [!if INSERTABLE] afxRegInsertable | afxRegApartmentThreading, [!else] afxRegApartmentThreading, [!endif] _dw$safeprojectname$OleMisc, _tlid, _wVerMajor, _wVerMinor); else return AfxOleUnregisterClass(m_clsid, m_lpszProgID); } [!if RUNTIME_LICENSE] // 授权字符串 static const TCHAR _szLicFileName[] = _T("$projectname$.lic"); static const WCHAR _szLicString[] = L"Copyright (c) $year$ $registeredorganization$"; // $control_class$::$control_class$Factory::VerifyUserLicense - // 检查是否存在用户许可证 BOOL $control_class$::$control_class$Factory::VerifyUserLicense() { return AfxVerifyLicFile(AfxGetInstanceHandle(), _szLicFileName, _szLicString); } // $control_class$::$control_class$Factory::GetLicenseKey - // 返回运行时授权密钥 BOOL $control_class$::$control_class$Factory::GetLicenseKey(DWORD dwReserved, BSTR *pbstrKey) { if (pbstrKey == nullptr) return FALSE; *pbstrKey = SysAllocString(_szLicString); return (*pbstrKey != nullptr); } [!endif] // $control_class$::$control_class$ - 构造函数 $control_class$::$control_class$() { InitializeIIDs(&IID_D$safeprojectname$, &IID_D$safeprojectname$Events); [!if SIMPLE_FRAME] EnableSimpleFrame(); [!endif] [!if ASYNC_PROPERTY_LOAD] m_lReadyState = READYSTATE_LOADING; // TODO: 当 readystate 更改时调用 InternalSetReadyState。 [!endif] // TODO: 在此初始化控件的实例数据。 } // $control_class$::~$control_class$ - 析构函数 $control_class$::~$control_class$() { // TODO: 在此清理控件的实例数据。 } // $control_class$::OnDraw - 绘图函数 void $control_class$::OnDraw( CDC* pdc, const CRect& rcBounds, const CRect& /* rcInvalid */) { if (!pdc) return; [!if SUBCLASS_WINDOW] DoSuperclassPaint(pdc, rcBounds); [!else] // TODO: 用您自己的绘图代码替换下面的代码。 pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH))); pdc->Ellipse(rcBounds); [!endif] [!if OPTIMIZED_DRAW] if (!IsOptimizedDraw()) { // 容器不支持优化绘图。 // TODO: 如果将任何 GDI 对象选入到设备上下文 *pdc 中, // 请在此处恢复先前选定的对象。 } [!endif] } // $control_class$::DoPropExchange - 持久性支持 void $control_class$::DoPropExchange(CPropExchange* pPX) { ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor)); COleControl::DoPropExchange(pPX); // TODO: 为每个持久的自定义属性调用 PX_ 函数。 } [!if WINDOWLESS || UNCLIPPED_DEVICE_CONTEXT || FLICKER_FREE || MOUSE_NOTIFICATIONS || OPTIMIZED_DRAW] // $control_class$::GetControlFlags - // 自定义 MFC 的 ActiveX 控件实现的标志。 // DWORD $control_class$::GetControlFlags() { DWORD dwFlags = COleControl::GetControlFlags(); [!if UNCLIPPED_DEVICE_CONTEXT] // 当前未剪辑控件的输出。 // 控件保证它不会绘制到它的 // 矩形工作区之外。 dwFlags &= ~clipPaintDC; [!endif] [!if WINDOWLESS] // 不用创建窗口即可激活控件。 // TODO: 编写控件的消息处理程序时,在使用 // m_hWnd 成员变量之前应首先检查它的值是否 // 值为非 null。 dwFlags |= windowlessActivate; [!endif] [!if FLICKER_FREE] // 在活动和不活动状态之间进行转换时, // 不会重新绘制控件。 dwFlags |= noFlickerActivate; [!endif] [!if MOUSE_NOTIFICATIONS] // 控件在不活动时可以接收鼠标通知。 // TODO: 如果编写 WM_SETCURSOR 和 WM_MOUSEMOVE 的处理程序, // 在使用 m_hWnd 成员变量之前应首先检查它的值 // 检查其值是否为非 NULL。 dwFlags |= pointerInactive; [!endif] [!if OPTIMIZED_DRAW] // 控件通过不还原设备上下文中的 // 原始 GDI 对象,可以优化它的 OnDraw 方法。 dwFlags |= canOptimizeDraw; [!endif] return dwFlags; } [!endif] // $control_class$::OnResetState - 将控件重置为默认状态 void $control_class$::OnResetState() { COleControl::OnResetState(); // 重置 DoPropExchange 中找到的默认值 // TODO: 在此重置任意其他控件状态。 } [!if ABOUT_BOX] // $control_class$::AboutBox - 向用户显示“关于”框 void $control_class$::AboutBox() { CDialogEx dlgAbout(IDD_ABOUTBOX_$safercprojectname$); dlgAbout.DoModal(); } [!endif] [!if SUBCLASS_WINDOW] // $control_class$::PreCreateWindow - 修改 CreateWindowEx 的参数 BOOL $control_class$::PreCreateWindow(CREATESTRUCT& cs) { [!if SUBCLASS_WINDOW] cs.lpszClass = _T("$window_class$"); [!else] // TODO: 填充要为其创建子类的窗口类的名称。 cs.lpszClass = _T(""); [!endif] BOOL bRet = COleControl::PreCreateWindow(cs); cs.hMenu = nullptr; return bRet; } // $control_class$::IsSubclassedControl - 这是一个子类控件 BOOL $control_class$::IsSubclassedControl() { return TRUE; } // $control_class$::OnOcmCommand - 处理命令消息 LRESULT $control_class$::OnOcmCommand(WPARAM wParam, LPARAM lParam) { WORD wNotifyCode = HIWORD(wParam); // TODO: 在此接通 wNotifyCode。 return 0; } [!endif] // $control_class$ 消息处理程序