Hide

SftTree/OCX 7.5 - ActiveX Tree Control

Display
Print

Using SftTree/OCX with Visual C++ 6.0

Adding SftTree/OCX To A Project
Special Considerations
- Picture Properties
- Font Properties
- Color Properties

This section describes how SftTree/OCX is typically used with Visual C++ 6.0. For information on how to use this product with unmanaged C/C++ with Visual Studio .NET, please see Using SftTree/OCX with Visual Studio (Unmanaged C/C++).

All interfaces offered by SftTree/OCX, such as ISftTree, ISftTreeItem, 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 SftTree/OCX 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 SftTree/OCX 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 AppWizard (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.

Adding The Control

Project & Form

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:

Select SftTree/OCX

The following SftTree/OCX controls will be shown in the list, one for most development tools, one for Internet Explorer and one for other development tools.

  • SftTree/OCX 7.5 Tree Control - for use with Visual C++ (and other development tools)
  • SftTree/OCX 7.5 Tree Control (IE) - for use with Internet Explorer, VBScript
  • SftTree/OCX 7.5 Tree Control (NoContainer*) - for use with other development tools

Select the control "SftTree/OCX 7.5 Tree 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 SftTree/OCX is not correctly installed or the control may have been unregistered accidentally. Quit Visual C++ and use the entry Maintenance, Register SftTree OCX in Windows Registry in the SftTree/OCX 7.5 program group to register the control. Restart Visual C++ and try to add the control to the dialog again.

SftTree/OCX Added

Resize the control to a suitable size.

Adding A Member Variable

Member variables for the control can be added using ClassWizard, or simply by adding the required definitions to your dialog implementation.

"Manually" Adding A Member Variable

Add a variable for the control to your dialog's header file. The control class is always CWnd:

public:
 CWnd m_Tree1; <<<<<<<

Add a DDX_Control entry to your dialog's DoDataExchange function. Make sure the ID used matches the ID used in the dialog resource (IDC_SFTTREE in this example).

void CYourDialog::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 DDX_Control(pDX, IDC_SFTTREE1, m_Tree1); <<<<<<<<
}

Adding A Member Variable Using ClassWizard

Click on the control, then invoke ClassWizard using the menu command View, ClassWizard... The Message Maps tab shows all events supported by SftTree/OCX.

Class Wizard

Click on the Member Variables tab and then on the Add Variable... button of the displayed page.

Add Variable

This will add the C++ wrapper classes needed to access SftTree/OCX. You are prompted to confirm this by clicking OK. A number of C++ classes are generated for each object class supported by SftTree/OCX. The only wrapper class that is needed is the class CSftTree/OCX (based on the control's IDispatch interface). It is only needed to define the SftTree/OCX control. Any other C++ classes generated can be discarded and are not used.

Select Classes

Finally, you will be prompted for the member variable name. Enter m_Tree1.

Member Variable

Using #import

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.

#pragma warning(disable : 4192) // automatically excluding 'Ixxx' while importing type library 'stdole2.tlb'
// For information about the following construct, please see Microsoft's
// KnowledgeBase entry Q224610
#import <stdole2.tlb> rename_namespace("SftTreeNameSpace") exclude("OLE_HANDLE", "OLE_COLOR", "IFontDisp", "IPictureDisp", "FONTSIZE")
#import <SftTree_IX86_U_75.OCX> rename_namespace("SftTreeNameSpace") rename("LoadImage", "LoadImageSftTree") \
 exclude("LONG_PTR")
#if _MSC_VER >= 1400 // need at least Visual Studio 2005
# pragma comment(lib, "comsuppw.lib") // avoid link error in VS2005
#endif
#pragma warning(default : 4192)

using namespace SftTreeNameSpace;

SftTree/OCX controls are installed in the Windows System(32) directory.

The #import statement generates two files, SftTree_IX86_U_75.tlh and SftTree_IX86_U_75.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 SftTree/OCX 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 SftTree_IX86_U_75.tlh and SftTree_IX86_U_75.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:

ISftTreePtr vTree1 = m_Tree1.GetControlUnknown();
vTree1->Enabled = VARIANT_FALSE;

or

ISftTreePtr vTree1 = m_Tree1.GetControlUnknown();
vTree1->PutEnabled(VARIANT_FALSE);

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

Adding IntelliSense Help

IntelliSense information is available for SftTree/OCX 7.5, but it is not automatically added to your projects. To "activate" IntelliSense help, add the generated header files SftTree_IX86_U_75.tlh and SftTree_IX86_U_75.tli to your project. IntelliSense is only available for files that are part of your project. Using the Project, Add to Project, Files... menu command, locate the above header files in your project's directory or subdirectories and add these to your project. These files exist only after the project has been compiled at least once.

All required steps have now been completed to use SftTree/OCX.

Sample Initialization

Add the following sample code to initialize the control in the OnInitDialog member function of the dialog:

ISftTreePtr vTree = m_Tree1.GetControlUnknown();
long itemIndex;
itemIndex = vTree->Items->Add(_T("Item 1"));
itemIndex = vTree->Items->Add(_T("Item 2"));
 vTree->Item[itemIndex]->Level = 1;
itemIndex = vTree->Items->Add(_T("Item 3"));

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 properties 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 SftTree/OCX control with three items.

After adding the control to the form, right click on the control and select the Properties... entry of the popup menu. This displays the Property Dialog for the control.

Please note that you can right-click on a property in a Property Dialog or double-click on the description of a property to access its complete help information.

This 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 SftTree/OCX 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.

Special Considerations

The #import statement generates two files, SftTree_IX86_U_75.tlh and SftTree_IX86_U_75.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 SftTree/OCX control. These files contain the definition of the methods and properties as shown in the Syntax section of each method and property.

Picture Properties

Applications written using C/C++ have direct access to Windows APIs and generally can manipulate bitmaps, icons and metafiles using Windows handles. ActiveX controls manipulate all pictures using the OLE Picture object represented by the IPicture and IPictureDisp interfaces. SftTree/OCX has full support for Windows handles and OLE Picture objects, so a C/C++ application can use its preferred picture representation. In addition, SftTree/OCX also support GDI+, so an application can use GDI+ images directly using properties and methods such as SftPictureObject.Image as SftPictureObject.LoadImage.

Using Handles

Most properties and methods have forms which directly accept (bitmap) handles as parameters.

The SftTreeCell.Image property (for example) can be used as follows:

CBitmap m_Up;
. . .
m_Up.LoadBitmap(IDB_UP);
. . .
vTree->GetCell(0,0)->Image->PutPictureH((OLE_HANDLE)(HBITMAP) m_Up);
Using OLE Picture Objects

OLE Picture objects can be created using Windows API functions, such as OleCreatePictureIndirect.

IPictureDispPtr pPic;
PICTDESC PictDesc;
PictDesc.cbSizeofstruct = sizeof(PICTDESC);
PictDesc.picType = PICTYPE_BITMAP;
PictDesc.bmp.hbitmap = (HBITMAP) bitmap_handle;
PictDesc.bmp.hpal = NULL;
HRESULT hr = OleCreatePictureIndirect(&PictDesc, __uuidof(pPic)/*IID_IPictureDisp*/, FALSE, (void**)&pPic);
ASSERT(SUCCEEDED(hr));
. . .
vTree->GetCell(0,0)->Image->PutRefPicture(pPic);

The Syntax section of picture properties typically shows Get, Put and PutRef forms. If both Put and PutRef are available, PutRef is the preferred form as it conserves resources and assigns a picture object reference to the control. Put causes the control to completely copy the picture object.

Font Properties

Fonts are represented using the OLE Font object and the IFontDisp interface. OLE Font objects can be created using the Windows API function OleCreateFontIndirect.

The SftTreeCell.Font property (for example) can be used as follows:

FontPtr pFont = vTree->GetCell(0,0)->GetFont();
pFont->Name = _T("MS Sans Serif");
pFont->Weight = FW_BOLD;
pFont->Italic = VARIANT_FALSE;
CY size;
size.int64 = 100000;// point size * 10000
pFont->Size = size;

The Syntax section of font properties typically shows Get, Put and PutRef forms. If both Put and PutRef are available, PutRef is the preferred form as it conserves resources and assigns a font object reference to the control. Any change to the font object is reflected in the control also. Put on the other hand causes the control to completely copy the font object.

Color Properties

The valid range for a color value is 0 to 16,777,215 (0xffffff). The high order byte of a number in this range equals 0; the lower 3 bytes, from least to most significant byte, determine the amount of red, green, and blue, respectively. The red, green, and blue components are each represented by a number between 0 and 255 (0xff). If the high byte is not 0, the system colors as defined in Control Panel's settings are used. The Windows API GetSysColor defines all valid constants. Please see your development environment's documentation for applicable color constants.

vTree->Items->BackColor = 0x80000000L | COLOR_HIGHLIGHT;
vTree->Items->BackColor = 0x000000ffL;

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


Spring Break!

Our offices will be closed this week (March 18 through March 22).

We'll be back March 24 to address any pending sales and support issues.