This section describes how to use SftDate/DLL in an application written using C++ and the Microsoft Foundation Class library (MFC). The code shown is taken from the MFC DemoSample (see Samples).
Please see "Building Applications" to prepare a project for development with SftDate/DLL.
| A) | Every source program making use of a SftDate/DLL control must include the required header file SftDate.h by using the #include directive. In an MFC application, SftDate.h automatically includes SftDateM.h, which defines the classes CSftDate (date field control), CSftCalendar (month calendar control) and their base class CSftDateBase. |
|---|
#include "SftDate.h" /* SftDate/DLL Header File */
This include statement should appear after the #include <stdafx.h> statement or can be included at the end of stdafx.h (after the MFC header files).
| B) | The source program SftDateM.cpp must be added to your project. It is added to the project, without making any modifications to the file. Instead of adding it to the project, you can include the file using the #include directive. However, it must be included in a source file (*.CPP) as it is not a header file, and only one source file can #include the file SftDateM.cpp. SftDateM.cpp provides the run-time class information of the classes in debug builds. |
|---|
#include "SftDateM.cpp"
| C) | In order to use SftDate/DLL controls, an application must call the CSftDate::RegisterApp function. The preferred location is the InitInstance member function of your CWinApp-based application object: |
|---|
CSftDate::RegisterApp(); /* Register with SftDate/DLL */| D) | Once SftDate/DLL controls are no longer needed, an application must call the CSftDate::UnregisterApp function. The preferred location is the ExitInstance member function of your CWinApp-based application object: |
|---|
CSftDate::UnregisterApp(); /* Unregister from SftDate/DLL */| E) | The application's executable (Exe or Dll) must be linked with the correct Lib file, depending on the target environment. Please see "Building Applications" for more information. |
|---|
ClassWizard does not support new classes such as CSftDate and CSftCalendar, so any instance variables, notification handlers, message map entries, etc., have to be added manually.
There are two methods to add a control to an application:
Adding a control using dialog resources is accomplished by using a resource editor to design a dialog. For more information, see Creating a Dialog Resource. Once a control is created, it is attached to its object by using DDX_Control in DoDataExchange or using SubclassDlgItem:
CSftDate m_Fields[FIELDS]; CSftCalendar m_Calendar; void CMainDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); for (int i = 0; i < FIELDS; ++i) DDX_Control(pDX, g_fieldIds[i], m_Fields[i]); DDX_Control(pDX, IDC_CALENDAR, m_Calendar); }
or
CSftCalendar m_Calendar;
m_Calendar.SubclassDlgItem(IDC_CALENDAR, this);Another method to create a control is by using the Create member function.
CSftDate m_Date;
m_Date.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP, CRect(10, 10, 210, 34), pParentWnd, IDC_DATE);
m_Date.SetFont(pParentWnd->GetFont());The member functions of CSftDate and CSftCalendar correspond to the SftDate_xxx functions without the window handle. The functions that apply to both controls are members of CSftDateBase:
SYSTEMTIME now;
GetLocalTime(&now);
SYSTEMTIME weekLater = AddDays(now, 6);
for (int i = 0; i < CONTROLS; ++i) {
Control(i).SetDarkMode(SFTDATE_DARKMODE_AUTO);
Control(i).SetHighContrastMode(SFTDATE_HIGHCONTRAST_AUTO);
Control(i).SetValue(&now);
}
m_Fields[1].SetFormat(SFTDATE_FORMAT_LONGDATE);
m_Fields[2].SetFormat(SFTDATE_FORMAT_SHORTTIME);
m_Fields[2].SetTimeInterval(15);
m_Fields[5].SetSelectionMode(SFTDATE_SELECTION_RANGE);
m_Fields[5].SetRange(&now, &weekLater);
m_Calendar.SetMonthsAcross(2);Control(i) returns a field or the calendar as a CSftDateBase reference, so the functions both controls share are called the same way.
Text functions have a CString overload in addition to the buffer form, for example CString CSftDate::GetCustomFormat() const.
The controls send WM_NOTIFY messages to their parent window, which are handled using ON_NOTIFY and ON_NOTIFY_RANGE message-map entries. See MFC and Notifications.