Hide

SftOptions 1.0 - ActiveX Options Control

Display
Print

Using SftOptions with Visual C++ (Unmanaged C/C++)

This section describes how SftOptions is typically used with unmanaged C/C++ and Visual Studio .NET. For information on how to use this product with Visual C++ 6.0, please see Using SftOptions with Visual C++ 6.0. Information about using SftOptions with C#, VB.NET or other .NET languages, please see Using SftOptions with .NET.

All interfaces offered by SftOptions, such as ISftOptions, ISftOptionsEntry, etc. are implemented as dual interfaces. Using vtable binding is significantly more efficient than using the IDispatch interface. The approach presented here relies on the #import preprocessor directive available in Visual C++.

Adding The SftOptions Control To A Project
Adding The SftOptsIO Control To A Project

Adding The SftOptions Control To A Project

Typically, the control is used with an MFC based application (or ATL). For this example, a sample MFC based application is used, but any unmanaged C or C++ application developed using Visual C++ can use this approach. This sample is created using the MFC AppWizard. A dialog based application is generated. While it is possible to use SftOptions in non-dialog windows, this is rarely the case.

In order to use any ActiveX controls in an MFC based project, OLE control support has to be added to the project. This is accomplished automatically when using the Application Wizard (as long as ActiveX control support is selected), or can be added later in the CWinApp::InitInstance member function, by using the AfxEnableControlContainer() function. For more information on ActiveX controls and Visual C++ please see the Visual C++ documentation.

C++ Project

ActiveX controls are easily added to a dialog by right-clicking on the dialog.

From the displayed popup menu, select Insert ActiveX Control..., which will display the following dialog:

Insert ActiveX Control

Select the control "SftOptions 1.0 Options Control" by highlighting it and click OK. This adds the control to the dialog. If an error message is displayed once you click OK, the product SftOptions is not correctly installed or the control may have been unregistered accidentally. Quit Visual C++ and use the entry Register Design Time Control in the SftOptions 1.0 program group to register the control. Restart Visual C++ and try to add the control to the dialog again.

Resize the control to a suitable size.

Click on the control, then invoke the Add Member Variable Wizard using the menu command Project, Add Variable....

Add Member Variable Wizard

Enter a variable name, and optional comment and click Finish.

This will add the C++ wrapper classes needed to access SftOptions. A number of C++ classes are generated for each object class supported by SftOptions. The only wrapper class that is needed is the class CSftOptions (based on the control's IDispatch interface). It is only needed to define the options control. Any other C++ classes generated can be discarded and are not used.

Because the generated wrapper classes are not used, #import statements have to be added to STDAFX.H. The #import directive makes all interfaces and the associated methods and properties available to the application.

#import <SftOptions_IX86_A_10.ocx> no_namespace

SftOptions controls are installed in the Windows System(32) directory.

The #import statement generates two files, SftOptions_IX86_A_10.tlh and SftOptions_IX86_A_10.tli, which are automatically included into the source at the position of the #import directive. It does so by analyzing the type library which is built into the SftOptions control. These files contain the definition of the methods and properties as shown in the Syntax section of each method and property.

VARIANT_BOOL Boolean = object->Enabled;

In addition, lower level function calls are also defined.

VARIANT_BOOL Boolean = object->GetEnabled();
void object->PutEnabled(VARIANT_BOOL Boolean);

The generated header files SftOptions_IX86_A_10.tlh and SftOptions_IX86_A_10.tli should be used as a reference when using the functions and C++ properties. This documentation generally includes the higher level property variables and functions only. Some low-level functions are not shown and can only be found in the generated header files.

Which style is used depends on your preference. Both styles are identical in functionality but some styles are easier to use. For example, the Enabled property can be set using any of the following styles:

ISftOptionsPtr vOptions1 = m_Options1.GetControlUnknown();
vOptions1->Enabled = VARIANT_FALSE;

or

ISftOptionsPtr vOptions1 = m_Options1.GetControlUnknown();
vOptions1->PutEnabled(VARIANT_FALSE);

For more information on smart pointers such as ISftOptionsPtr used above, please see the Visual C++ product documentation.

All required steps have now been completed to use SftOptions.

For a complete example, also add two button controls to the form, a Close button (named IDCLOSE) and an Apply button (named IDAPPLY).

Adding Buttons

Add the following sample code to initialize the control:

BOOL CTest1Dlg::OnInitDialog()
{
     ... other initialization code ...

     GetDlgItem(IDAPPLY)->EnableWindow(FALSE);

     ISftOptionsPtr vOptions1 = m_Opts1.GetControlUnknown();
     _bstr_t nullString;
     vOptions1->Add(nullString, _bstr_t(_T("Main")), _bstr_t(_T("Your first sample")), entrySftOptionsTopic, nullString, nullString, NULL, nullString, nullString, nullString, nullString);
     vOptions1->Add(_bstr_t(_T("Main")), _bstr_t(_T("Rb1")), _bstr_t(_T("Radio Button 1")), entrySftOptionsRadioButton, nullString, nullString, NULL, nullString, nullString, nullString, nullString);
     vOptions1->Add(_bstr_t(_T("Main")), _bstr_t(_T("Rb2")), _bstr_t(_T("Radio Button 2")), entrySftOptionsRadioButton, nullString, nullString, NULL, nullString, nullString, nullString, nullString);
     vOptions1->Add(_bstr_t(_T("Main")), _bstr_t(_T("Rb3")), _bstr_t(_T("Radio Button 3")), entrySftOptionsRadioButton, nullString, nullString, NULL, nullString, nullString, nullString, nullString);
     vOptions1->Add(nullString, _bstr_t(_T("Cb1")), _bstr_t(_T("Check Box Option")), entrySftOptionsCheckBox, nullString, nullString, NULL, nullString, nullString, nullString, nullString);
     vOptions1->IO->Registry = _T("USR:Software\\Softelvdm\\TestData\\FirstSample");
     vOptions1->Load();
     vOptions1->InitializationComplete();

     return TRUE;
}

void CTest1Dlg::OnBnClickedClose()
{
    ISftOptionsPtr vOpts1 = m_Opts1.GetControlUnknown();
    vOpts1->Save();
    EndDialog(0);
}

void CTest1Dlg::OnBnClickedApply()
{
    ISftOptionsPtr vOpts1 = m_Opts1.GetControlUnknown();
    vOpts1->Save();
}

BEGIN_EVENTSINK_MAP(CTest1Dlg, CDialog)
 ON_EVENT(CTest1Dlg, IDC_SFTOPTIONS1, 1, ApplyStatusChangeSftoptions1, VTS_NONE)
END_EVENTSINK_MAP()

void CTest1Dlg::ApplyStatusChangeSftoptions1()
{
    ISftOptionsPtr vOpts1 = m_Opts1.GetControlUnknown();
    GetDlgItem(IDAPPLY)->EnableWindow(vOpts1->GetApplyStatus());
}

In this example, the control is initialized at run-time using code. Of course it is also possible (and much easier) to set up all option entries using the property pages. You can access the property pages by right-clicking on the control and select the Properties... entry of the popup menu.

You can run the sample application and it displays a simple Options dialog with a few entries. After modifying some of the entries, click the Apply Button. Using Regedit you can review the registry settings at HKEY_CURRENT_USER\Software\Softelvdm\TestData\FirstSample. For example, if you select the check box in this example, the registry key "Cb1" is set to the value "1".

Adding The SftOptsIO Control To A Project

The SftOptsIO control offers simplified Registry and INI file I/O to retrieve or set option values without a user interface.

The SftOptsIO control is added to a project in the same manner as the SftOptions control (see Adding The SftOptions Control To A Project).

The SftOptsIO control can be added to a form or it can be created at run-time This control is only visible at design-time. At run-time it is not visible to the end-user. It can only be used by the application.

The SftOptsIO control must be initialized by setting its Registry property (this could of course also be done using the property pages):

CWnd OptsIO;
#define ID_OPTIONS 100
OptsIO.CreateControl(_T("SftOptions10.SftOptsIO.1"), NULL, 0, CRect(0,0,0,0), this, ID_OPTIONS);

ISftOptsIOPtr vOptsIO = OptsIO.GetControlUnknown();
vOptsIO->PutRegistry(_bstr_t(_T("USR:Software\\Softelvdm\\TestData\\FirstSample")));

Retrieving an option value using the same name as used by the SftOptions control is very easy:

CString CheckBoxValue;
CheckBoxValue = (LPCTSTR) vOptsIO->GetOptionValue(_bstr_t(_T("Cb1")));

In this example, the CheckBoxValue variable would contain "1" if the option is selected.

The SftOptions control has many properties and methods which you can use. This is a very simple example and doesn't even begin to exploit the capabilities of this control. Please take a moment to familiarize yourself with the objects offered by the SftOptions control. Each object represents a specific area of the control and can be fully customized. Also make sure to run the demo which is included with this product and take a look at the included samples.


Last Updated 08/13/2020 - (email)
© 2024 Softel vdm, Inc.