HeaderPane
Main
Hide
Share Link
Print

Using SftDate/OCX with Visual Studio (Unmanaged C/C++)

This section describes how SftDate/OCX is typically used with unmanaged C/C++ and Visual Studio.

All interfaces offered by SftDate/OCX, such as ISftDate, ISftCalendar, ISftDateCalendar, 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++ and is the one the C++ samples use.

Adding SftDate/OCX To The Toolbox
Adding SftDate/OCX To A Project
Special Considerations
- Dates and Null Values
- Picture Properties
- Font Properties
- Color Properties

Adding SftDate/OCX To The Toolbox

The controls are automatically added to the Visual Studio Toolbox in the toolbox group "Softel vdm, Inc.". If you ever need to add them to the toolbox by hand, use the simple procedure outlined in "Adding Controls To The Visual Studio Toolbox".

Adding SftDate/OCX To A Project

Typically, the controls are used with an MFC based application (or ATL). For this example, an MFC based dialog application is used, but any unmanaged C or C++ application developed using Visual C++ can use this approach. While it is possible to use SftDate/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 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, as the samples do. For more information on ActiveX controls and Visual C++ please see the Visual C++ documentation.

Adding The Controls

The date field control (SftDate) and the month calendar control (SftCalendar) can be added to a dialog by locating them in the toolbox group "Softel vdm, Inc.". A control can be added to a dialog by clicking on its button in the toolbox, then clicking on the dialog, or by dragging the button of the toolbox to the dialog.

Adding SftDate/OCX

Resize the control to a suitable size. The dialog template holds the control under its CLSID, and the properties defined using the property pages are saved with the dialog resource (in its DLGINIT resource).

An ActiveX control doesn't draw where a sibling window that comes before it in the dialog template (above it in the z-order) overlaps it. A group box around date field and month calendar controls must therefore come after the controls in the dialog template, or the controls are not drawn inside it, as the DemoSample's dialog resource shows.

Accessing The Controls

The C++ wrapper classes Visual Studio can generate for an ActiveX control (with Add Variable...) are not needed. The controls are accessed through the smart pointers ISftDatePtr and ISftCalendarPtr, which the #import directive (see below) defines, initialized from the dialog's child window:

ISftDatePtr     m_Date;
ISftCalendarPtr m_Calendar;
. . .
m_Date = GetDlgItem(IDC_DATE)->GetControlUnknown();
m_Calendar = GetDlgItem(IDC_CALENDAR)->GetControlUnknown();

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

Using #import

The #import directive makes all interfaces and the associated methods and properties available to the application. Add the following statements to Stdafx.h:

#pragma warning(disable : 4192)   // automatically excluding 'Ixxx' while importing type library 'stdole2.tlb'
#ifdef _WIN64
#import <SftDate_x64_U_20.ocx> rename_namespace("SftDateNameSpace") rename("LoadImage", "LoadImageSftDate")
#else
#import <SftDate_IX86_U_20.ocx> rename_namespace("SftDateNameSpace") rename("LoadImage", "LoadImageSftDate")
#endif
#pragma comment(lib, "comsuppw.lib")
#pragma warning(default : 4192)

using namespace SftDateNameSpace;

The 64-bit SftDate/OCX control is installed in the Windows System32 directory and the 32-bit control in the SysWOW64 directory, so #import finds the control matching the platform being built. The LoadImage method is renamed to LoadImageSftDate to avoid a conflict with the Windows LoadImage macro.

The #import statement generates two files (SftDate_x64_U_20.tlh and SftDate_x64_U_20.tli for 64-bit builds, or SftDate_IX86_U_20.tlh and SftDate_IX86_U_20.tli for 32-bit builds), 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 SftDate/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 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:

m_Date->Enabled = VARIANT_FALSE;

or

m_Date->PutEnabled(VARIANT_FALSE);

The event and error codes are defined in the header files \Program Files (x86)\Softelvdm\SftDate OCX 2.0\Include\SftDateOCXEvents.h (the dispatch IDs of the events) and SftDateOCXErrors.h (see Error Messages).

Adding IntelliSense Help

IntelliSense information is available for SftDate/OCX 2.0, but it is not automatically added to your projects. To "activate" IntelliSense help, add the generated header files (SftDate_x64_U_20.tlh and SftDate_x64_U_20.tli for 64-bit builds, or SftDate_IX86_U_20.tlh and SftDate_IX86_U_20.tli for 32-bit builds) to your project. IntelliSense is only available for files that are part of your project. Using the Project, Add Existing Item... 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 SftDate/OCX.

Sample Initialization

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

SYSTEMTIME st;
GetLocalTime(&st);
st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
DATE today = 0;
SystemTimeToVariantTime(&st, &today);

m_Date = GetDlgItem(IDC_DATE)->GetControlUnknown();
m_Date->Format = formatSftDateLongDate;
m_Date->Value = _variant_t(today, VT_DATE);

m_Calendar = GetDlgItem(IDC_CALENDAR)->GetControlUnknown();
m_Calendar->SelectionMode = selectionSftDateRange;
m_Calendar->Calendar->MonthsAcross = 2;
m_Calendar->Calendar->ShowWeekNumbers = VARIANT_TRUE;
m_Calendar->SetRange(_variant_t(today, VT_DATE), _variant_t(today + 6, VT_DATE));

In this example, the controls are 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 selecting the Properties... entry of the popup menu.

You can run the application: it shows a date field with today's date in the long date format and a month calendar showing two months with week numbers, with the seven days starting today selected.

Handling Events

Events are handled through the dialog's event sink map, declared with DECLARE_EVENTSINK_MAP() in the dialog class. ON_EVENT connects an event of one control with a handler; ON_EVENT_RANGE connects the event of a range of control IDs with one handler, and MFC then passes the control ID as the first argument, so the parameter list starts with VTS_I4. The samples use the dispatch IDs of the events (ValueChanged is 1, ValueValidating is 3):

BEGIN_EVENTSINK_MAP(CMainDlg, CDialog)
    ON_EVENT(CMainDlg, IDC_CALENDAR, 1 /* ValueChanged */, OnCalendarChanged, VTS_NONE)
    ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_DUEDATE, 3 /* ValueValidating */, OnValueValidating, VTS_I4 VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
END_EVENTSINK_MAP()

BOOL CMainDlg::OnValueValidating(UINT id, VARIANT* pNewValue, VARIANT* pNewRangeEnd, VARIANT_BOOL* pCancel)
{
    // appointments are only accepted on weekdays
    SYSTEMTIME st;
    if (pNewValue->vt == VT_DATE && VariantTimeToSystemTime(pNewValue->date, &st) && (st.wDayOfWeek == 0 || st.wDayOfWeek == 6))
        *pCancel = VARIANT_TRUE;
    return TRUE;
}

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.

These controls have many properties and methods which you can use. This is a very simple example and doesn't even begin to exploit the capabilities of the controls. Please take a moment to familiarize yourself with the objects offered by SftDate/OCX. 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 (SftDate_x64_U_20.tlh and SftDate_x64_U_20.tli for 64-bit builds, or SftDate_IX86_U_20.tlh and SftDate_IX86_U_20.tli for 32-bit builds), which are automatically included into the source at the position of the #import directive. These files contain the definition of the methods and properties as shown in the Syntax section of each method and property.

Dates and Null Values

Dates are OLE dates (DATE), which the Windows API functions SystemTimeToVariantTime and VariantTimeToSystemTime convert. The properties that can hold no date, Value, RangeStart and RangeEnd of both controls and TodayDate of the SftDateCalendar object, are VARIANT values: VT_DATE for a date, VT_NULL when there is no date (see Values).

_variant_t value = m_Date->Value;
if (value.vt == VT_DATE) {
    SYSTEMTIME st;
    VariantTimeToSystemTime(value.date, &st);
    . . .
}

_variant_t empty;
empty.vt = VT_NULL;
m_Date->AllowEmpty = VARIANT_TRUE;
m_Date->Value = empty;

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. SftDate/OCX has full support for Windows handles and OLE Picture objects, so a C/C++ application can use its preferred picture representation. In addition, SftDate/OCX also supports GDI+, so an application can use GDI+ images directly using properties and methods such as SftPictureObject.Image and SftPictureObject.LoadImage.

The pictures of special dates (SftDateSpecialDate.Picture) and of the days formatted in the DayFormatting event (SftDateDayFormatting.Picture) are SftPictureObject objects, which also offer color samples (SetColorSample).

Using Handles

The BitmapHandle and IconHandle properties accept picture handles. A special date's picture (for example) can be defined as follows:

CBitmap m_Holiday;
. . .
m_Holiday.LoadBitmap(IDB_HOLIDAY);
. . .
ISftDateSpecialDatePtr day = m_Calendar->Calendar->SpecialDates->Add(date, recurrenceSftDateAnnually);
day->Picture->BitmapHandle = (OLE_HANDLE)(INT_PTR)(HBITMAP) m_Holiday;
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));
. . .
day->Picture->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 TitleFont property of the calendar (for example) can be defined as follows:

FONTDESC fd;
memset(&fd, 0, sizeof(fd));
fd.cbSizeofstruct = sizeof(FONTDESC);
fd.lpstrName = const_cast<LPOLESTR>(L"Segoe UI");
fd.cySize.int64 = 120000;   // point size * 10000
fd.sWeight = FW_BOLD;
fd.sCharset = DEFAULT_CHARSET;
IFontDispPtr pFont;
if (SUCCEEDED(OleCreateFontIndirect(&fd, __uuidof(IFontDisp), (void**)&pFont)))
    m_Calendar->Calendar->PutRefTitleFont(pFont);

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

Colors are OLE_COLOR values. 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 bit is set (0x80000000 plus the index used with the Windows API GetSysColor), the system colors as defined in the Windows settings are used.

Every color property defaults to colorSftDateDefault (-1), a color that follows the system colors, dark mode and high contrast (see Colors).

m_Calendar->Calendar->TitleBackColor = RGB(0, 95, 115);
m_Calendar->Calendar->TitleForeColor = 0x80000000 | COLOR_HIGHLIGHTTEXT;
m_Date->BackColor = (OLE_COLOR) colorSftDateDefault;

Last Updated 11/29/2026 - (email)
© 2026 Softel vdm, Inc.