The ActiveX wrapper class implements properties in its interface using read and write access methods. That is, the wrapper class has COM properties, which appear on an interface as getter and/or setter methods. Unlike VCL properties, you do not see a "property" declaration on the interface for COM properties. Rather, you see methods that are flagged as property access methods. When you add a property to the ActiveX control's default interface, the wrapper class definition (which appears in the _TLB unit that is updated by the Type Library editor) gains one or two new methods (a getter and/or setter) that you must implement, just as when you add a method to the interface, the wrapper class gains a corresponding method for you to implement. Thus, adding properties to the wrapper class's interface is essentially the same as adding methods: the wrapper class definition gains new skeletal method implementations for you to complete.
property Caption: TCaption read Get_Caption write Set_Caption;
Delphi adds the following declarations to the wrapper class:
function Get_Caption: WideString; safecall; procedure Set_Caption(const Value: WideString); safecall;
STDMETHOD(get_Caption(BSTR* Value)); STDMETHOD(set_Caption(BSTR Value));
In addition, it adds skeletal method implementations for you to complete:
function TButtonX.Get_Caption: WideString; begin end; procedure TButtonX.Set_Caption(Value: WideString); begin end;
STDMETHODIMP TButtonXImpl::get_Caption(BSTR* Value) { try { } catch(Exception &e) { return Error(e.Message.c_str(), IID_IButtonX); } return S_OK; }; STDMETHODIMP TButtonXImpl::set_Caption(BSTR Value) { try { } catch(Exception &e) { return Error(e.Message.c_str(), IID_IButtonX); } return S_OK; };
Typically, you can implement these methods by simply delegating to the associated VCL control, which can be accessed using the FDelphiControl member of the wrapper class:
function TButtonX.Get_Caption: WideString; begin Result := WideString(FDelphiControl.Caption); end; procedure TButtonX.Set_Caption(const Value: WideString); begin FDelphiControl.Caption := TCaption(Value); end;
STDMETHODIMP TButtonXImpl::get_Caption(BSTR* Value) { try { *Value = WideString(m_VclCtl->Caption).Copy(); } catch(Exception &e) { return Error(e.Message.c_str(), IID_IButtonX); } return S_OK; }; STDMETHODIMP TButtonXImpl::set_Caption(BSTR Value) { try { m_VclCtl->Caption = AnsiString(Value); } catch(Exception &e) { return Error(e.Message.c_str(), IID_IButtonX); } return S_OK; };
In some cases, you may need to add code to convert the COM data types to native Delphi types. The preceding example manages this with typecasting.
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|