This section describes how to use SftDate/DLL in an application written using the C programming language. The code shown is taken from the 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. |
|---|
#include "SftDate.h" /* SftDate/DLL Header File */
This include statement should appear after the #include <windows.h> statement. The file is located in the directory \Program Files (x86)\Softelvdm\SftDate DLL 2.0\Include (unless changed during the installation). The project settings may need to be updated so the #include file can be located (see "Building Applications" for more information).
| B) | In order to use SftDate/DLL controls, an application must call the SftDate_RegisterApp function. This call has to be made before any SftDate/DLL controls are created. Add the following statement to your source code, where your application registers its window classes (normally during application initialization): |
|---|
SftDate_RegisterApp(hinst); /* Register with SftDate/DLL */| C) | Once SftDate/DLL controls are no longer needed, an application must call the SftDate_UnregisterApp function, so cleanup processing can take place. This call has to be made after all SftDate/DLL controls have been destroyed (normally during application termination). |
|---|
SftDate_UnregisterApp(hinst); /* Unregister from SftDate/DLL */| D) | 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. |
|---|
There are two methods to add a date field control or a month calendar control to an application:
Adding a control using dialog resources is accomplished by using a resource editor to design a dialog. Once a control is created, its window handle can be obtained by using the Windows GetDlgItem function. For more information, see Creating a Dialog Resource.
HWND hwndDate = GetDlgItem(hwndDlg, IDC_DATE); HWND hwndCal = GetDlgItem(hwndDlg, IDC_CALENDAR);
Another method to create a control is by using the CreateWindow(Ex) Windows call:
hwndDate = CreateWindow(TEXT(SFTDATE_CLASS), NULL, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 10, 10, 200, 24, hwndMain, (HMENU) IDC_DATE, hinst, NULL); hwndCal = CreateWindow(TEXT(SFTCALENDAR_CLASS), NULL, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 10, 40, 0, 0, hwndMain, (HMENU) IDC_CALENDAR, hinst, NULL); SendMessage(hwndDate, WM_SETFONT, (WPARAM) hFont, TRUE); SendMessage(hwndCal, WM_SETFONT, (WPARAM) hFont, TRUE);
For more information on the various parameters used, see the Windows API documentation. The controls size themselves (SetAutoSize): the month calendar control takes the size of the months it shows, the date field control the height its font requires.
The controls are defined at run-time using the SftDate_xxx functions. The same functions are used for both controls; the calendar settings apply to a month calendar control and to the dropdown calendar of a date field control:
SYSTEMTIME now; GetLocalTime(&now); SftDate_SetFormat(GetDlgItem(hwndDlg, IDC_LONGDATE), SFTDATE_FORMAT_LONGDATE); SftDate_SetFormat(GetDlgItem(hwndDlg, IDC_TIMELIST), SFTDATE_FORMAT_SHORTTIME); SftDate_SetTimeInterval(GetDlgItem(hwndDlg, IDC_TIMELIST), 15); SftDate_SetAllowEmpty(GetDlgItem(hwndDlg, IDC_EMPTY), TRUE); SftDate_SetPlaceholderText(GetDlgItem(hwndDlg, IDC_EMPTY), TEXT("No date")); SftDate_SetShowClearButton(GetDlgItem(hwndDlg, IDC_EMPTY), TRUE); SftDate_SetValue(GetDlgItem(hwndDlg, IDC_EMPTY), NULL); SftDate_SetCustomFormat(GetDlgItem(hwndDlg, IDC_CUSTOM), TEXT("ddd, MMM d, yyyy 'at' HH:mm")); SftDate_SetFormat(GetDlgItem(hwndDlg, IDC_CUSTOM), SFTDATE_FORMAT_CUSTOM); SftDate_SetMonthsAcross(hwndCal, 2); SftDate_SetDarkMode(hwndCal, SFTDATE_DARKMODE_AUTO); SftDate_SetValue(hwndCal, &now);
Values are SYSTEMTIME structures, always in the Gregorian calendar, whatever calendar system the culture of a control uses (SetCulture).
As with standard Windows controls, applications respond to notifications to react to user actions. The controls send their notifications to the parent window (or the window defined using SetNotifyWindow) as WM_NOTIFY messages; the NMHDR structure's code member holds the notification code. For additional information, see Notifications.
case WM_NOTIFY: {
LPNMHDR lpnmhdr = (LPNMHDR) lParam;
switch (lpnmhdr->code) {
case NM_SFTDATE_DAYFORMATTING: {
// any day of any calendar can be changed here; this colors weekends that aren't special dates
LPSFTDATE_NMDAYFORMATTING lpDay = (LPSFTDATE_NMDAYFORMATTING) lParam;
if (IsDlgButtonChecked(hwndDlg, IDC_WEEKENDS) == BST_CHECKED && lpDay->fWeekend && lpDay->specialDate < 0)
lpDay->colorFg = RGB(205, 92, 92);
return TRUE;
}
case NM_SFTDATE_VALUECHANGED:
case NM_SFTDATE_RANGECHANGED:
if (lpnmhdr->idFrom == IDC_CALENDAR)
ShowSelection(hwndDlg);
return TRUE;
}
break;
}The value is retrieved using SftDate_GetValue, which returns FALSE if the control has no value:
static void ShowSelection(HWND hwndDlg)
{
HWND hwndCal = GetDlgItem(hwndDlg, IDC_CALENDAR);
WCHAR szText[200], szFirst[80], szLast[80];
SYSTEMTIME start, end;
lstrcpyW(szText, L"Selected: (none)");
if (SftDate_GetSelectionMode(hwndCal) == SFTDATE_SELECTION_RANGE) {
if (SftDate_GetRangeStart(hwndCal, &start)) {
if (!SftDate_GetRangeEnd(hwndCal, &end))
end = start;
FormatLongDate(hwndDlg, &start, szFirst, 80);
FormatLongDate(hwndDlg, &end, szLast, 80);
wsprintfW(szText, L"Selected: %s - %s", szFirst, szLast);
}
} else if (SftDate_GetValue(hwndCal, &start)) {
FormatLongDate(hwndDlg, &start, szFirst, 80);
wsprintfW(szText, L"Selected: %s", szFirst);
}
SetDlgItemTextW(hwndDlg, IDC_SELECTION, szText);
}A dialog that supports dark mode forwards WM_SETTINGCHANGE to its controls, so controls using SFTDATE_DARKMODE_AUTO or SFTDATE_HIGHCONTRAST_AUTO follow the Windows settings. The samples use SftDarkMode_HandleDialogMessage from SftDarkMode.h, which also draws the dialog itself dark. See Dark Mode.