This article discusses how to serialize ActiveX controls. Serialization refers to the process of reading or writing data from persistent storage media (such as disk files. Microsoft basic class (MFC) Library inCobjectClass provides built-in support for serialization.ColecontrolYou can use the property exchange mechanism to extend this support to ActiveX controls.
You can rewrite colecontrol: dopropexchange to serialize ActiveX controls. This function is called when the control object is loaded and saved to store all attributes implemented with member variables or with member variables with change notifications. You can serialize controls to record the status of controls.
The following topics describe the main issues related to serialization of ActiveX controls:
- Implementation
Dopropexchange
Function to serialize the control object
- Custom serialization Process
- Supported versions
Implement the dopropexchange Function
When you use the ActiveX control Wizard to generate a control project, several default functions are automatically added to the control class, including the default Implementation of colecontrol: dopropexchange. The following example showsCode:
Void csamplectrl: dopropexchange (cpropexchange * ppx) {exchangeversion (ppx, makelong (_ wverminor, _ wvermajor); colecontrol: dopropexchange (ppx); // todo: call PX _ functions for each persistent custom property .}
If you want to make the property persistent, you can add a call to the property exchange function to modify it.Dopropexchange
. The following example shows the serialization of the custom Boolean circleshape attribute. The default value of the circleshape attribute isTrue:
Void csamplectrl: dopropexchange (cpropexchange * ppx) {exchangeversion (ppx, makelong (_ wverminor, _ partition); colecontrol: dopropexchange (ppx); px_bool (ppx, "circleshape", m_bcircleshape, true );}
Px_bool (ppx, "circleshape", m_bcircleshape, true );
First parameter: pointer to cpropexchange Type Variable
Second parameter: attribute name
Third parameter: Corresponding member variable
Fourth parameter: Default Value, optional parameter.
The following table lists the possible property exchange functions that can be used to serialize control attributes:
Property exchange function |
Purpose |
Px_blob () |
Serialize Binary Large Object (BLOB) data attributes. |
Px_bool () |
Serialize Boolean attributes. |
Px_color () |
Serialize the color type attribute. |
Px_currency () |
SerializationCy(Currency) type attribute. |
Px_double () |
SerializationDoubleType attribute. |
Px_font () |
Serialize font type attributes. |
Px_float () |
SerializationFloatType attribute. |
Px_iunknown () |
SerializationLpunknownType attribute. |
Px_long () |
SerializationLongType attribute. |
Px_picture () |
Serialize the attributes of the picture type. |
Px_short () |
SerializationShortType attribute. |
Px_string () |
SerializationCstringType attribute. |
Px_ulong () |
SerializationUlongType attribute. |
Px_ushort () |
SerializationUshortType attribute. |
For more information about these property exchange functions, see the persistence of the OLE Control in the "MFC reference.
Customize dopropexchange default behavior
Dopropertyexchange(As shown in the previous topic ).ColecontrolBase class. Serialization of this operationColecontrolAutomatically supported attribute sets use more buckets than custom attributes of only serialized controls. Removing this call allows your object to serialize only the attributes that are important to you. When saving or loading control objects, unless you explicitly add the status of any common properties implemented by the ControlPx _Otherwise, they will not be serialized.
Supported versions
The version allows you to add new persistence attributes to the modified ActiveX Control and detect and load the persistence status created by the control of earlier versions. To make the control version available as part of its persistent dataDopropexchange
The function calls colecontrol: exchangeversion. If the ActiveX control is created using the ActiveX control wizard, the call is automatically inserted. If you do not need version support, you can remove this call. However, the cost of the control size is very small (4 bytes) for the additional flexibility provided by the version support ).
If the control is not created using the ActiveX control wizardDopropexchange
Start of the FunctionColecontrol: dopropexchangeBefore) insert downstream to add a pairColecontrol: exchangeversion:
Void csamplectrl: dopropexchange (cpropexchange * ppx) {exchangeversion (ppx, makelong (_ wverminor, _ wvermajor); colecontrol: dopropexchange (ppx );...}
You can use anyDWORDAs the version number. Use the project generated by the ActiveX control wizard_ WverminorAnd_ WvermajorAs the default value._ WverminorAnd_ WvermajorIs the global constant defined in the project's ActiveX control class implementation file. InDopropexchange
You can call cpropexchange: getversion to retrieve the version that is being saved or retrieved at any time.
In the following example, Version 1 of the control in this example only has the "releasedate" attribute. Version 2 adds the originaldate attribute. If it indicates that the control is loaded from an earlier version, the control initializes the member variables of the new property to the default value.
Void csamplectrl: dopropexchange (cpropexchange * ppx) {exchangeversion (ppx, makelong (_ wverminor, _ wvermajor); colecontrol: dopropexchange (ppx); px_long (ppx, "releasedate", m_releasedate); If (ppx-> getversion ()> = makelong (0, 2) {px_long (ppx, "originaldate", m_originaldate );} else {If (ppx-> isloading () m_originaldate = 0 ;}}
By default, the control converts the old data to the latest format. For example, if version 2 of the control loads data saved by version 1, the data is saved in version 2 format when it is saved again. If you want the control to save data in the format read more than onceExchangeversionSaveFalseIt is passed as the third parameter. The third parameter is optional and the default value isTrue.
In addition, the property Page Swap Function
The control variables on the property page are connected to the properties of the ActiveX control. Use the DDP _ function.
For example, ddp_text (PDX, idc_code, m_code, _ T ("codestring") is used to establish a connection between m_code and the attribute codestring.
Common property page switching functions:
function name |
function |
ddp_cbindex |
index and control attributes of the selected string in the linked combo box |
ddp_cbstring |
select strings and control attributes in the linked combo box. The selected string can start with a letter that is the same as the property value, but does not have to match it completely |
ddp_cbstringexact |
select strings and control attributes in the linked combo box. The string values of the selected string and attribute must match exactly |
ddp_check |
link check boxes and control properties |
ddp_lbindex |
index and control properties of the selected string in the link list box |
ddp_lbstring |
selected strings and control attributes in the link list box. The selected string can start with a letter that is the same as the property value, but does not have to match it completely |
ddp_lbstringexact |
selected strings and control attributes in the link list box. The string values of the selected string and attribute must match exactly |
Ddp_radio |
Link radio button and Control Properties |
Ddp_text |