描述:
我用VC++.Net 写的ATL控件想要创建一个属性。这个属性的类型我想使用我自己定义的一个类,为什么编译总不能通过。
我想知道,用ATL写的控件的属性是否只能使用向导提供的那些类型?
但是我看别的控件,比如MapX控件,他的属性就是他自己定义的类型。
或者还有其他的控件,比如有个Picture 属性。可以如下使用
MyCtl.Picture = LoadPicture("C:\temp\a.bmp") 。
而这个 Picture 属性就是MyCtl 中自己定义的类型,专门保存位图。
这个问题一直没解决。谢谢各位。
解决方案1:
当然可以,不过这个class必须是实现IDispatch
作为参数的数据应该是这个class的接口,而不是class本身.
同时要在指定的该类和接口的定义位置.
比如在idl中我有定义
[
uuid(39A50DE5-86E3-40D2-BCBB-8F192A788756),
version(1.0)
]
coclass Person {
[default] interface _Person;
};
就可以在方法中有如下定义
[id(0x60030000)]
HRESULT GetPerson([out, retval] _Person** );
传递类也可以实现
方法
1.直接使用COM对象作为传送的类,只不过使用接口方式传送(就是我前面说的一种方式,也是标准的)
2.使用序列化或其的方法把对象变为字节流,以BYTES方式传送
一般是在接口定义文件里修改,如把enum写到typelib定义文件中去
// HoverButton.odl : type library source for ActiveX Control project.
// This file will be processed by the Make Type Library (mktyplib) tool to
// produce the type library (HoverButton.tlb) that will become a resource in
// HoverButton.ocx.
#include <olectl.h>
#include <idispids.h>
[ uuid(A6B9C705-C1B5-11D2-AB5E-0000E82A9730), version(1.0),
helpfile("HoverButton.hlp"),
helpstring("HoverButton ActiveX Control module"),
control ]
library HOVERBUTTONLib
{
importlib(STDOLE_TLB);
importlib(STDTYPE_TLB);
// Primary dispatch interface for CHoverButtonCtrl
typedef
[ uuid(CD470FA1-CB0F-11d2-AB5E-0000E82A9730),
helpstring("Dispatch interface for HoverButton Control Style") ]
enum tagBtnStyle
{
[helpstring("Simple")] BS_NONE = 0,
[helpstring("Flat Button")] BS_FLAT = 1,
[helpstring("Spot Light")] BS_SPOT = 2,
}BTNSTYLE;
[ uuid(A6B9C706-C1B5-11D2-AB5E-0000E82A9730),
helpstring("Dispatch interface for HoverButton Control"), hidden ]
dispinterface _DHoverButton
{
properties:
// NOTE - ClassWizard will maintain property information here.
// Use extreme caution when editing this section.
//{{AFX_ODL_PROP(CHoverButtonCtrl)
[id(DISPID_CAPTION), bindable, requestedit] BSTR Caption;
[id(DISPID_FONT), bindable] IFontDisp* Font;
[id(DISPID_BACKCOLOR), bindable, requestedit] OLE_COLOR BackColor;
[id(DISPID_FORECOLOR), bindable, requestedit] OLE_COLOR ForeColor;
[id(3)] IPictureDisp* Picture;
[id(1)] OLE_COLOR SpotBackColor;
[id(2)] OLE_COLOR SpotForeColor;
[id(4)] BTNSTYLE Style; ////////////////////////////////////
//}}AFX_ODL_PROP
methods:
// NOTE - ClassWizard will maintain method information here.
// Use extreme caution when editing this section.
//{{AFX_ODL_METHOD(CHoverButtonCtrl)
//}}AFX_ODL_METHOD
[id(DISPID_ABOUTBOX)] void AboutBox();
};
// Event dispatch interface for CHoverButtonCtrl
[ uuid(A6B9C707-C1B5-11D2-AB5E-0000E82A9730),
helpstring("Event interface for HoverButton Control") ]
dispinterface _DHoverButtonEvents
{
properties:
// Event interface has no properties
methods:
// NOTE - ClassWizard will maintain event information here.
// Use extreme caution when editing this section.
//{{AFX_ODL_EVENT(CHoverButtonCtrl)
[id(DISPID_CLICK)] void Click();
[id(DISPID_DBLCLICK)] void DblClick();
//}}AFX_ODL_EVENT
};
// Class information for CHoverButtonCtrl
[ uuid(A6B9C708-C1B5-11D2-AB5E-0000E82A9730),
helpstring("HoverButton Control"), control ]
coclass HoverButton
{
[default] dispinterface _DHoverButton;
[default, source] dispinterface _DHoverButtonEvents;
};
//{{AFX_APPEND_ODL}}
//}}AFX_APPEND_ODL}}
};