Use the persistence control property of your own files during runtime

Source: Internet
Author: User
Http://www.codeguru.com/cpp/com-tech/activex/controls/article.php/c5519/

Persist ActiveX Controls At Runtime
At first glance, I was shocked by this title. How was it possible to persist the control during runtime? after reading the content, I found that it was only a code segment with the CArchive parameter, it is estimated that they should use their own files for persistence, which is purely misleading. However, this content is quite interesting, so we used an example to complete it.

1. Create a project tdlghello in the dialog box, insert the ActiveX control (Microsoft ListView), generate a bunch of files, and press "no table. Pull a control in the dialog box template and associate the CListView1 m_lv variable;

2. Add three buttons (load, save, and set the background color). Add the following code:

Void CTdlghelloDlg: OnButtonColor ()
{
// TODO: Add your control notification handler code here
CColorDialog dlg (m_lv.GetBackColor ());
If (dlg. DoModal () = IDOK ){
M_lv.SetBackColor (dlg. GetColor ());
}
}

Void CTdlghelloDlg: OnButtonSave ()
{
// TODO: Add your control notification handler code here
CString str = _ T ("mypersist. dat ");
CFile file;
If (file. Open (str, CFile: typeBinary | CFile: modeWrite | CFile: modeCreate )){
CArchive ar (& file, CArchive: store );
PersistActiveX (ar, & m_lv );
Ar. Close ();
File. Close ();
}
}

Void CTdlghelloDlg: OnButtonLoad ()
{
// TODO: Add your control notification handler code here
CString str = _ T ("mypersist. dat ");
CFile file;
If (file. Open (str, CFile: typeBinary | CFile: modeRead )){
CArchive ar (& file, CArchive: load );
PersistActiveX (ar, & m_lv );
Ar. Close ();
File. Close ();
}
}

Mypersist. dat is the file used to save control properties.

3. The above PersistActiveX is the function segment in the original text. Simply add this function and copy the code in the function.

BOOL CTdlghelloDlg: PersistActiveX (CArchive & ar, CWnd * pActiveX)
{
ASSERT (pActiveX );

HRESULT hr = E_FAIL;

// Create an archive stream
CArchiveStream stm (& ar );
LPPERSISTSTREAMINIT pPersStm = NULL;
LPUNKNOWN lpUnk = pActiveX-> GetControlUnknown ();

If (lpUnk)
{
// We have the IUnknown interface, so
// Get the IPersistStreamInit
LpUnk-> QueryInterface (IID_IPersistStreamInit,
(LPVOID *) & pPersStm );

If (pPersStm)
{
// We have the relevant interface so load or save the data
// Based on the type of archive we have.
If (ar. IsLoading ())
{
Hr = pPersStm-> Load (& stm );
}
Else
{
Hr = pPersStm-> Save (& stm, FALSE );
}
}

// Must release the interface
PPersStm-> Release ();
}

Return SUCCEEDED (hr );
}

Here, CArchiveStream needs # include "afxpriv2.h"

4. Compile, run, click the button to set the background color, set a background color, and then click the Save button. Exit the program, run and click load. You can find that the background color of ListView is changed to the saved background color.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.