描述:
我刚做了一个控件,遇到了一个麻烦:就是在IDE设计时设定的值在运行时却不存在了(明明在设计时指定了相关属性,可程序一运行,又回到了控件的最初状态)。不知如何解决,请大家帮帮忙~!
解决方案1:
Persistence.
COleObject uses DoPropExchange instead of Serialize.
DoPropExchange uses Macros such as PX_STRING and PX_FLOAT to store and retrieve data from a source. This is fine for simple data types, but is far less powerful than the Serialize method, which allows us to delegate persistence down to self-contained units. Clearly this presents a problem if our ActiveX control contains child windows as described above.
In theory we should be able to use the PX_BLOB macro to persist these objects as a stream of binary data. There is what appears to be an example of this at Article ID: Q141274, in the Microsoft Knowledge Base, but the example does not work and after much fiddling I have not been able to fix it.
The alternative is to extract the properties from the Child Windows and persist those properties. In some more complicated situations this may be unsatisfactory, in which case you should try as I have to persist using normal serialization. Please contact me if you succeed.
Persisting Child Window Properties:
You must extract the properties into simple data types in order to use them in the PX macro. For this purpose you should add some member variables to your control's class. Don't forget to set default values for these variables in the constructor.
Because the DoPropExchange method is called before the OnCreate method, you must make some changes to ensure that you do not attempt to extract or set these properties before the Child Windows have been Created. Do this by adding a Boolean flag member variable which is set to false in the constructor, tested before Creating child windows, and set to true after Creating child windows.
You should create a member function which sets the child window properties then sets the boolean to true.
You should call this function in the OnCreate function so that, when OnCreate is called after DoPropExchange has loaded a new Control, the loaded values will be used.
You should also test this Boolean flag in DoPropExchange before extracting the child window properties and before setting them.
e.g.
void CExampleCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);
if(m_bChildWindowsCreated) {
m_Edit.GetWindowText(m_EditPXString);
}
PX_String(pPX, _T("EditText"), m_EditPXString, _T(""));
if(m_bChildWindowsCreated) {
SetChildWindowsProperties();
}
}