HeaderPane
Main
Hide
Share Link
Print

DemoSample (C)

This sample shows ten date fields of every kind (short date, long date, time from a list and from a clock, date and time, range, optional date with a check box, empty date with a placeholder and a Clear button, free-form date and a custom format) and a month calendar that the dialog sizes itself around. It covers registering with SftDate/DLL, placing the controls from a dialog resource, formats, selection modes, special dates with pictures and tooltips (SFTDATE_SPECIALDATE, Sft_SetPictureColorSample), the NM_SFTDATE_DAYFORMATTING notification coloring weekends, RefreshDays, a color scheme with a variant for each mode (SetColor, NM_SFTDATE_DARKMODE_CHANGED), cultures and calendar systems (SetCulture with the locale's optional calendars, SetRightToLeft), and dark mode, high contrast and per-monitor DPI support.

Every option applies to all fields, their dropdown calendars and the month calendar.

DemoSample

The source code is located at C:\Program Files (x86)\Softelvdm\SftDate DLL 2.0\Samples\C\DemoSample\DemoSample.c.

/****************************************************************************/
/* SftDate/DLL 2.0 - Date and Calendar Controls for C/C++                   */
/* Copyright (C) 2026  Softel vdm, Inc. All Rights Reserved.                */
/****************************************************************************/
/* SftDate/DLL 2.0 sample. Guide: https://softelvdm.com/Vault/Softelvdm.com/llms/SftDate-DLL-2.0.txt, API reference: https://softelvdm.com/Vault/Softelvdm.com/llms/SftDate-DLL-2.0-reference.txt - see AGENTS.md. */

#include <windows.h>
#include <windowsx.h>

#include "SftDate.h"                    /* SftDate/DLL Header File */

#define SFTDARKMODE_IMPLEMENTATION
#include "SftDarkMode.h"                /* Dark mode support */

#include "resource.h"                   // application resources

#define WM_APP_LAYOUT   (WM_APP + 1)

HINSTANCE g_hInst;                      // App Instance Handle

static const int g_fieldIds[] = {
    IDC_DATE, IDC_LONGDATE, IDC_TIMELIST, IDC_TIMECLOCK, IDC_DATETIME,
    IDC_RANGE, IDC_OPTIONAL, IDC_EMPTY, IDC_FREEFORM, IDC_CUSTOM,
};
#define FIELDS      ((int) (sizeof(g_fieldIds) / sizeof(g_fieldIds[0])))
#define CONTROLS    (FIELDS + 1)        // the fields and the calendar

// A culture is a locale name (NULL for the Windows setting) and a calendar (0 for the locale's own, otherwise its alternate calendar)
static const struct {
    LPCTSTR lpszName;
    LPCWSTR lpszLocale;
    CALID calendar;
} g_cultures[] = {
    { TEXT("(Windows setting)"), NULL, 0 },
    { TEXT("English (United States)"), L"en-US", 0 },
    { TEXT("German (Germany)"), L"de-DE", 0 },
    { TEXT("French (France)"), L"fr-FR", 0 },
    { TEXT("Japanese (Japan), Japanese calendar"), L"ja-JP", CAL_JAPAN },
    { TEXT("Arabic (Saudi Arabia), Um Al Qura calendar"), L"ar-SA", 0 },
    { TEXT("Hebrew (Israel), Hebrew calendar"), L"he-IL", CAL_HEBREW },
    { TEXT("Thai (Thailand), Buddhist calendar"), L"th-TH", 0 },
    { TEXT("Persian (Iran), Persian calendar"), L"fa-IR", 0 },
};

static LPCTSTR g_modes[] = { TEXT("Off"), TEXT("On"), TEXT("Auto") };
static LPCTSTR g_buttons[] = { TEXT("Dropdown"), TEXT("Up/down"), TEXT("Dropdown, up/down"), TEXT("None") };
static LPCTSTR g_borders[] = { TEXT("Single"), TEXT("3D"), TEXT("None") };
static const int g_borderStyles[] = { SFTDATE_BORDER_FIXEDSINGLE, SFTDATE_BORDER_FIXED3D, SFTDATE_BORDER_NONE };
static LPCTSTR g_alignments[] = { TEXT("Near"), TEXT("Far") };
static LPCTSTR g_across[] = { TEXT("1"), TEXT("2"), TEXT("3") };
static LPCTSTR g_down[] = { TEXT("1"), TEXT("2") };
static LPCTSTR g_weekNumbers[] = { TEXT("None"), TEXT("Culture rule"), TEXT("ISO 8601") };
static LPCTSTR g_firstDays[] = {
    TEXT("(Culture)"), TEXT("Sunday"), TEXT("Monday"), TEXT("Tuesday"), TEXT("Wednesday"), TEXT("Thursday"), TEXT("Friday"), TEXT("Saturday"),
};
static LPCTSTR g_views[] = { TEXT("All"), TEXT("Month and year"), TEXT("Month only") };
static const int g_viewSets[] = { SFTDATE_VIEWS_ALL, SFTDATE_VIEWS_MONTH | SFTDATE_VIEWS_YEAR, SFTDATE_VIEWS_MONTH };

// Colors set by the application are used as given in dark mode too, so the scheme has a variant for each mode.
static const struct {
    int index;
    COLORREF light, dark;
} g_colors[] = {
    { SFTDATE_COLOR_TITLEBACK, RGB(0, 95, 115), RGB(0, 95, 115) },
    { SFTDATE_COLOR_TITLEFORE, RGB(255, 255, 255), RGB(255, 255, 255) },
    { SFTDATE_COLOR_DAYOFWEEKFORE, RGB(0, 95, 115), RGB(110, 190, 205) },
    { SFTDATE_COLOR_WEEKNUMBERFORE, RGB(0, 95, 115), RGB(110, 190, 205) },
    { SFTDATE_COLOR_TODAY, RGB(225, 140, 30), RGB(225, 140, 30) },
    { SFTDATE_COLOR_SELECTEDBACK, RGB(225, 140, 30), RGB(225, 140, 30) },
    { SFTDATE_COLOR_SELECTEDFORE, RGB(255, 255, 255), RGB(255, 255, 255) },
    { SFTDATE_COLOR_RANGEBACK, RGB(250, 225, 190), RGB(95, 60, 20) },
    { SFTDATE_COLOR_SEGMENTBACK, RGB(225, 140, 30), RGB(225, 140, 30) },
    { SFTDATE_COLOR_SEGMENTFORE, RGB(255, 255, 255), RGB(255, 255, 255) },
};

static SYSTEMTIME g_minDate, g_maxDate;  // the controls' own limits

/**********************************************************************/
/*                              Helpers                               */
/**********************************************************************/

static void GetControls(HWND hwndDlg, HWND controls[CONTROLS])
{
    int i;
    for (i = 0; i < FIELDS; ++i)
        controls[i] = GetDlgItem(hwndDlg, g_fieldIds[i]);
    controls[FIELDS] = GetDlgItem(hwndDlg, IDC_CALENDAR);
}

static SYSTEMTIME MakeDate(int year, int month, int day)
{
    SYSTEMTIME st;
    memset(&st, 0, sizeof(st));
    st.wYear = (WORD) year;
    st.wMonth = (WORD) month;
    st.wDay = (WORD) day;
    return st;
}

static SYSTEMTIME AddDays(const SYSTEMTIME* lpDate, int days)
{
    FILETIME ft;
    LARGE_INTEGER li;
    SYSTEMTIME st;

    SystemTimeToFileTime(lpDate, &ft);
    li.LowPart = ft.dwLowDateTime;
    li.HighPart = (LONG) ft.dwHighDateTime;
    li.QuadPart += (LONGLONG) days * 24 * 60 * 60 * 10000000;
    ft.dwLowDateTime = li.LowPart;
    ft.dwHighDateTime = (DWORD) li.HighPart;
    FileTimeToSystemTime(&ft, &st);
    return st;
}

static void FillCombo(HWND hwndDlg, int id, LPCTSTR* lpszItems, int count, int sel)
{
    HWND hwndCombo = GetDlgItem(hwndDlg, id);
    int i;
    for (i = 0; i < count; ++i)
        ComboBox_AddString(hwndCombo, lpszItems[i]);
    ComboBox_SetCurSel(hwndCombo, sel >= 0 && sel < count ? sel : 0);
}

static int IndexOf(const int* values, int count, int value)
{
    int i;
    for (i = 0; i < count; ++i)
        if (values[i] == value)
            return i;
    return 0;
}

static int CurSel(HWND hwndDlg, int id)
{
    int sel = ComboBox_GetCurSel(GetDlgItem(hwndDlg, id));
    return sel < 0 ? 0 : sel;
}

/**********************************************************************/
/*                         Selected Dates                             */
/**********************************************************************/

static void FormatLongDate(HWND hwndDlg, const SYSTEMTIME* lpDate, LPWSTR lpszText, int cchMax)
{
    int i = CurSel(hwndDlg, IDC_CULTURE);
    DWORD flags = DATE_LONGDATE;
    if (g_cultures[i].calendar != 0)
        flags |= DATE_USE_ALT_CALENDAR;
    if (!GetDateFormatEx(g_cultures[i].lpszLocale, flags, lpDate, NULL, lpszText, cchMax, NULL))
        lpszText[0] = 0;
}

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);
}

/**********************************************************************/
/*                              Layout                                */
/**********************************************************************/

static RECT ChildRect(HWND hwndDlg, int id)
{
    RECT rect;
    GetWindowRect(GetDlgItem(hwndDlg, id), &rect);
    MapWindowPoints(HWND_DESKTOP, hwndDlg, (LPPOINT) &rect, 2);
    return rect;
}

// The calendar sizes itself to its months, so its group box, the selection text, the Close button and the dialog follow it.
static void Layout(HWND hwndDlg)
{
    RECT du = { 7, 12, 0, 4 };          // margin, group box title, -, gap
    RECT options = ChildRect(hwndDlg, IDC_GROUP_OPTIONS);
    RECT group = ChildRect(hwndDlg, IDC_GROUP_CALENDAR);
    RECT cal = ChildRect(hwndDlg, IDC_CALENDAR);
    RECT label = ChildRect(hwndDlg, IDC_SELECTION);
    RECT help = ChildRect(hwndDlg, IDC_HELPTEXT);
    RECT close = ChildRect(hwndDlg, IDCANCEL);
    RECT window, client;
    int x, y, width, right, bottom;

    MapDialogRect(hwndDlg, &du);
    x = group.left + du.left;
    y = group.top + du.top;
    SetWindowPos(GetDlgItem(hwndDlg, IDC_CALENDAR), NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
    y += cal.bottom - cal.top + du.bottom;
    width = max(options.right - options.left, cal.right - cal.left + 2 * du.left);
    SetWindowPos(GetDlgItem(hwndDlg, IDC_SELECTION), NULL, x, y, width - 2 * du.left, label.bottom - label.top, SWP_NOZORDER | SWP_NOACTIVATE);
    y += label.bottom - label.top + du.bottom * 2;
    SetWindowPos(GetDlgItem(hwndDlg, IDC_GROUP_CALENDAR), NULL, 0, 0, width, y - group.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);

    right = group.left + width + du.left;
    bottom = max(y, help.bottom) + du.bottom;
    SetWindowPos(GetDlgItem(hwndDlg, IDCANCEL), NULL, right - du.left - (close.right - close.left), bottom, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
    bottom += close.bottom - close.top + du.bottom * 2;

    GetWindowRect(hwndDlg, &window);
    GetClientRect(hwndDlg, &client);
    SetWindowPos(hwndDlg, NULL, 0, 0, window.right - window.left - client.right + right, window.bottom - window.top - client.bottom + bottom,
        SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
    InvalidateRect(hwndDlg, NULL, TRUE);
}

static void CenterWindow(HWND hwnd)
{
    RECT rc;
    RECT rcOwner;
    int x, y;

    GetWindowRect(hwnd, &rc);
    GetWindowRect(GetDesktopWindow(), &rcOwner);

    x = rcOwner.left + (((rcOwner.right - rcOwner.left) - (rc.right - rc.left)) / 2);
    y = rcOwner.top + (((rcOwner.bottom - rcOwner.top) - (rc.bottom - rc.top)) / 2);

    SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
}

/**********************************************************************/
/*                             Options                                */
/**********************************************************************/

// Special dates repeat every year, month or week, and can be bold, colored, disabled, show a picture and a tooltip.
static void SetHolidays(HWND controls[CONTROLS], BOOL fShow)
{
    SFTDATE_SPECIALDATE days[5];
    SYSTEMTIME today, monday;
    int i, d;

    GetLocalTime(&today);
    monday = AddDays(&today, -((today.wDayOfWeek + 6) % 7));

    memset(days, 0, sizeof(days));
    for (d = 0; d < 5; ++d) {
        days[d].cbSize = sizeof(SFTDATE_SPECIALDATE);
        days[d].recurrence = SFTDATE_RECURRENCE_ANNUALLY;
        days[d].fBold = TRUE;
        days[d].colorBg = SFTDATE_NOCOLOR;
        days[d].colorFg = RGB(178, 34, 34);
        days[d].fEnabled = TRUE;
    }
    days[0].date = MakeDate(today.wYear, 1, 1);
    days[0].lpszToolTip = TEXT("New Year's Day");
    days[1].date = MakeDate(today.wYear, 7, 4);
    days[1].lpszToolTip = TEXT("Independence Day");
    days[2].date = MakeDate(today.wYear, 12, 25);
    days[2].colorBg = RGB(255, 228, 225);
    days[2].lpszToolTip = TEXT("Christmas Day");
    days[3].date = MakeDate(today.wYear, 1, 15);
    days[3].recurrence = SFTDATE_RECURRENCE_MONTHLY;
    days[3].fBold = FALSE;
    days[3].colorFg = SFTDATE_NOCOLOR;
    days[3].fEnabled = FALSE;
    days[3].lpszToolTip = TEXT("Closed for inventory");
    days[4].date = MakeDate(monday.wYear, monday.wMonth, monday.wDay);
    days[4].recurrence = SFTDATE_RECURRENCE_WEEKLY;
    days[4].fBold = FALSE;
    days[4].colorFg = RGB(65, 105, 225);
    Sft_SetPictureColorSample(&days[4].Picture, 6, 6, RGB(65, 105, 225), RGB(65, 105, 225));
    days[4].lpszToolTip = TEXT("Team meeting");

    for (i = 0; i < CONTROLS; ++i) {
        SftDate_ClearSpecialDates(controls[i]);
        if (fShow)
            for (d = 0; d < 5; ++d)
                SftDate_AddSpecialDate(controls[i], &days[d]);
    }
}

static void SetColorScheme(HWND hwndCtl, BOOL fOn)
{
    BOOL fDark = SftDate_IsDarkModeActive(hwndCtl);
    int k;
    for (k = 0; k < (int) (sizeof(g_colors) / sizeof(g_colors[0])); ++k)
        SftDate_SetColor(hwndCtl, g_colors[k].index, !fOn ? SFTDATE_NOCOLOR : fDark ? g_colors[k].dark : g_colors[k].light);
}

static void ApplyOption(HWND hwndDlg, int id)
{
    HWND c[CONTROLS];
    HWND hwndCal;
    int sel = CurSel(hwndDlg, id);
    BOOL fChecked = IsDlgButtonChecked(hwndDlg, id) == BST_CHECKED;
    int i;

    GetControls(hwndDlg, c);
    hwndCal = c[FIELDS];

    switch (id) {
    case IDC_CULTURE: {
        DWORD rtl = 0;
        GetLocaleInfoEx(g_cultures[sel].lpszLocale ? g_cultures[sel].lpszLocale : LOCALE_NAME_USER_DEFAULT,
            LOCALE_IREADINGLAYOUT | LOCALE_RETURN_NUMBER, (LPWSTR) &rtl, sizeof(rtl) / sizeof(WCHAR));
        for (i = 0; i < CONTROLS; ++i) {
            SftDate_SetCulture_W(c[i], g_cultures[sel].lpszLocale, g_cultures[sel].calendar);
            SftDate_SetRightToLeft(c[i], rtl == 1);
        }
        ShowSelection(hwndDlg);
        break;
     }
    case IDC_DARKMODE:
        for (i = 0; i < CONTROLS; ++i) {
            SftDate_SetDarkMode(c[i], sel);
            SetColorScheme(c[i], IsDlgButtonChecked(hwndDlg, IDC_COLORS) == BST_CHECKED);
        }
        break;
    case IDC_HIGHCONTRAST:
        for (i = 0; i < CONTROLS; ++i)
            SftDate_SetHighContrastMode(c[i], sel);
        break;
    case IDC_BUTTONS:
        for (i = 0; i < FIELDS; ++i)
            SftDate_SetButtons(c[i], sel);
        break;
    case IDC_BORDER:
        for (i = 0; i < FIELDS; ++i)
            SftDate_SetBorderStyle(c[i], g_borderStyles[sel]);
        break;
    case IDC_ALIGNMENT:
        for (i = 0; i < FIELDS; ++i)
            SftDate_SetDropDownAlignment(c[i], sel);
        break;
    case IDC_ACROSS:
        SftDate_SetMonthsAcross(hwndCal, sel + 1);
        break;
    case IDC_DOWN:
        SftDate_SetMonthsDown(hwndCal, sel + 1);
        break;
    case IDC_WEEKNUMBERS:
        for (i = 0; i < CONTROLS; ++i) {
            SftDate_SetShowWeekNumbers(c[i], sel > 0);
            SftDate_SetWeekNumberRule(c[i], sel == 2 ? SFTDATE_WEEKRULE_ISO8601 : SFTDATE_WEEKRULE_CULTURE);
        }
        break;
    case IDC_FIRSTDAY:
        for (i = 0; i < CONTROLS; ++i)
            SftDate_SetFirstDayOfWeek(c[i], sel);
        break;
    case IDC_VIEWS:
        for (i = 0; i < CONTROLS; ++i)
            SftDate_SetAllowedViews(c[i], g_viewSets[sel]);
        break;
    case IDC_TRAILING:
        for (i = 0; i < CONTROLS; ++i)
            SftDate_SetShowTrailingDates(c[i], fChecked);
        break;
    case IDC_TODAYBUTTON:
        for (i = 0; i < CONTROLS; ++i)
            SftDate_SetShowTodayButton(c[i], fChecked);
        break;
    case IDC_HIGHLIGHTTODAY:
        for (i = 0; i < CONTROLS; ++i)
            SftDate_SetHighlightToday(c[i], fChecked);
        break;
    case IDC_CLEARBUTTON:
        for (i = 0; i < CONTROLS; ++i)
            SftDate_SetCalendarClearButton(c[i], fChecked);
        break;
    case IDC_HOLIDAYS:
        SetHolidays(c, fChecked);
        break;
    case IDC_WEEKENDS:
        // NM_SFTDATE_DAYFORMATTING colors the days; RefreshDays asks for them again
        for (i = 0; i < CONTROLS; ++i)
            SftDate_RefreshDays(c[i]);
        break;
    case IDC_DISABLEWEEKENDS:
        for (i = 0; i < CONTROLS; ++i)
            SftDate_SetDisabledDaysOfWeek(c[i], fChecked ? SFTDATE_DAYS_WEEKEND : SFTDATE_DAYS_NONE);
        break;
    case IDC_RANGESELECT:
        SftDate_SetSelectionMode(hwndCal, fChecked ? SFTDATE_SELECTION_RANGE : SFTDATE_SELECTION_SINGLE);
        ShowSelection(hwndDlg);
        break;
    case IDC_THISYEAR: {
        SYSTEMTIME today, first, last;
        GetLocalTime(&today);
        first = MakeDate(today.wYear, 1, 1);
        last = MakeDate(today.wYear, 12, 31);
        for (i = 0; i < CONTROLS; ++i) {
            SftDate_SetMinDate(c[i], fChecked ? &first : &g_minDate);
            SftDate_SetMaxDate(c[i], fChecked ? &last : &g_maxDate);
        }
        ShowSelection(hwndDlg);
        break;
     }
    case IDC_DROPONFOCUS:
        for (i = 0; i < FIELDS; ++i)
            SftDate_SetDropOnFocus(c[i], fChecked);
        break;
    case IDC_AUTOADVANCE:
        for (i = 0; i < FIELDS; ++i)
            SftDate_SetAutoAdvance(c[i], fChecked);
        break;
    case IDC_COLORS:
        for (i = 0; i < CONTROLS; ++i)
            SetColorScheme(c[i], fChecked);
        break;
    case IDC_THEMES:
        for (i = 0; i < CONTROLS; ++i)
            SftDate_SetUseThemes(c[i], fChecked);
        break;
    }
    Layout(hwndDlg);
}

/**********************************************************************/
/*                          Initialization                            */
/**********************************************************************/

static void InitControls(HWND hwndDlg)
{
    HWND c[CONTROLS];
    HWND hwndCal;
    SYSTEMTIME now, weekLater;
    int i;

    GetControls(hwndDlg, c);
    hwndCal = c[FIELDS];
    GetLocalTime(&now);
    weekLater = AddDays(&now, 6);

    for (i = 0; i < CONTROLS; ++i) {
        SftDate_SetDarkMode(c[i], SFTDATE_DARKMODE_AUTO);
        SftDate_SetHighContrastMode(c[i], SFTDATE_HIGHCONTRAST_AUTO);
        SftDate_SetImageScaling(c[i], SFTDATE_IMAGESCALING_STRETCH);
        SftDate_SetValue(c[i], &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_SetFormat(GetDlgItem(hwndDlg, IDC_TIMECLOCK), SFTDATE_FORMAT_SHORTTIME);
    SftDate_SetTimeDropDownStyle(GetDlgItem(hwndDlg, IDC_TIMECLOCK), SFTDATE_TIMESTYLE_CLOCK);

    SftDate_SetFormat(GetDlgItem(hwndDlg, IDC_DATETIME), SFTDATE_FORMAT_SHORTDATETIME);

    SftDate_SetSelectionMode(GetDlgItem(hwndDlg, IDC_RANGE), SFTDATE_SELECTION_RANGE);
    SftDate_SetRange(GetDlgItem(hwndDlg, IDC_RANGE), &now, &weekLater);

    SftDate_SetShowCheckBox(GetDlgItem(hwndDlg, IDC_OPTIONAL), TRUE);
    SftDate_SetChecked(GetDlgItem(hwndDlg, IDC_OPTIONAL), FALSE);

    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_SetEditStyle(GetDlgItem(hwndDlg, IDC_FREEFORM), SFTDATE_EDIT_FREEFORM);

    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_GetMinDate(hwndCal, &g_minDate);
    SftDate_GetMaxDate(hwndCal, &g_maxDate);

    SetDlgItemText(hwndDlg, IDC_HELPTEXT,
        TEXT("Type into the parts of a field, change a part with the Up and Down keys or the mouse wheel, and open the ")
        TEXT("dropdown with F4 or Alt+Down. In the dropdown of the date and time field, Tab switches between the calendar ")
        TEXT("and the time. The free-form field parses what you type in the selected culture.\r\n\r\n")
        TEXT("The options apply to all fields and calendars; the month counts apply to the SftCalendar control."));

    // the options start out with the controls' settings
    {
        HWND hwndCulture = GetDlgItem(hwndDlg, IDC_CULTURE);
        RECT width = { 0, 0, 170, 0 };
        for (i = 0; i < (int) (sizeof(g_cultures) / sizeof(g_cultures[0])); ++i)
            ComboBox_AddString(hwndCulture, g_cultures[i].lpszName);
        ComboBox_SetCurSel(hwndCulture, 0);
        MapDialogRect(hwndDlg, &width);
        SendMessage(hwndCulture, CB_SETDROPPEDWIDTH, width.right, 0);
    }
    FillCombo(hwndDlg, IDC_DARKMODE, g_modes, 3, SftDate_GetDarkMode(hwndCal));
    FillCombo(hwndDlg, IDC_HIGHCONTRAST, g_modes, 3, SftDate_GetHighContrastMode(hwndCal));
    FillCombo(hwndDlg, IDC_BUTTONS, g_buttons, 4, SftDate_GetButtons(c[0]));
    FillCombo(hwndDlg, IDC_BORDER, g_borders, 3, IndexOf(g_borderStyles, 3, SftDate_GetBorderStyle(c[0])));
    FillCombo(hwndDlg, IDC_ALIGNMENT, g_alignments, 2, SftDate_GetDropDownAlignment(c[0]));
    FillCombo(hwndDlg, IDC_ACROSS, g_across, 3, SftDate_GetMonthsAcross(hwndCal) - 1);
    FillCombo(hwndDlg, IDC_DOWN, g_down, 2, SftDate_GetMonthsDown(hwndCal) - 1);
    FillCombo(hwndDlg, IDC_WEEKNUMBERS, g_weekNumbers, 3,
        !SftDate_GetShowWeekNumbers(hwndCal) ? 0 : SftDate_GetWeekNumberRule(hwndCal) == SFTDATE_WEEKRULE_ISO8601 ? 2 : 1);
    FillCombo(hwndDlg, IDC_FIRSTDAY, g_firstDays, 8, SftDate_GetFirstDayOfWeek(hwndCal));
    FillCombo(hwndDlg, IDC_VIEWS, g_views, 3, IndexOf(g_viewSets, 3, SftDate_GetAllowedViews(hwndCal)));
    CheckDlgButton(hwndDlg, IDC_TRAILING, SftDate_GetShowTrailingDates(hwndCal) ? BST_CHECKED : BST_UNCHECKED);
    CheckDlgButton(hwndDlg, IDC_TODAYBUTTON, SftDate_GetShowTodayButton(hwndCal) ? BST_CHECKED : BST_UNCHECKED);
    CheckDlgButton(hwndDlg, IDC_HIGHLIGHTTODAY, SftDate_GetHighlightToday(hwndCal) ? BST_CHECKED : BST_UNCHECKED);
    CheckDlgButton(hwndDlg, IDC_CLEARBUTTON, SftDate_GetCalendarClearButton(hwndCal) ? BST_CHECKED : BST_UNCHECKED);
    CheckDlgButton(hwndDlg, IDC_DROPONFOCUS, SftDate_GetDropOnFocus(c[0]) ? BST_CHECKED : BST_UNCHECKED);
    CheckDlgButton(hwndDlg, IDC_AUTOADVANCE, SftDate_GetAutoAdvance(c[0]) ? BST_CHECKED : BST_UNCHECKED);
    CheckDlgButton(hwndDlg, IDC_THEMES, SftDate_GetUseThemes(hwndCal) ? BST_CHECKED : BST_UNCHECKED);

    ShowSelection(hwndDlg);
}

/**********************************************************************/
/*                           Dialog Proc                              */
/**********************************************************************/

INT_PTR CALLBACK MainDialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg) {
    case WM_INITDIALOG:
        SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_APP)));
        InitControls(hwndDlg);
        SftDarkMode_ApplyToDialog(hwndDlg);
        Layout(hwndDlg);
        CenterWindow(hwndDlg);
        return TRUE;

    case WM_DPICHANGED:
        // the dialog scales itself; the calendar's new size is known afterwards
        PostMessage(hwndDlg, WM_APP_LAYOUT, 0, 0);
        break;

    case WM_APP_LAYOUT:
        Layout(hwndDlg);
        return TRUE;

    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;
        case NM_SFTDATE_DARKMODE_CHANGED:
            SetColorScheme(lpnmhdr->hwndFrom, IsDlgButtonChecked(hwndDlg, IDC_COLORS) == BST_CHECKED);
            return TRUE;
        case NM_SFTDATE_DPI_CHANGED:
            PostMessage(hwndDlg, WM_APP_LAYOUT, 0, 0);
            return TRUE;
        }
        break;
     }

    case WM_COMMAND: {
        int id = LOWORD(wParam);
        int code = HIWORD(wParam);
        if (id == IDCANCEL) {
            EndDialog(hwndDlg, 0);
            return TRUE;
        }
        if ((code == CBN_SELCHANGE && id >= IDC_CULTURE && id <= IDC_VIEWS) ||
                (code == BN_CLICKED && id >= IDC_TRAILING && id <= IDC_THEMES)) {
            ApplyOption(hwndDlg, id);
            return TRUE;
        }
        break;
     }
    }

    {
        INT_PTR dmResult;
        if (SftDarkMode_HandleDialogMessage(hwndDlg, msg, wParam, lParam, &dmResult))
            return dmResult;
    }

    return FALSE;
}

/**********************************************************************/
/*                              WinMain                               */
/**********************************************************************/

int PASCAL WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int cmdShow)
{
    g_hInst = hinst;

    SftDarkMode_Init();                 /* Dark mode support */
    SftDate_RegisterApp(hinst);         /* Register with SftDate/DLL */

    // A Unicode dialog, so the selected dates show in every culture's script, also in ANSI builds
    DialogBoxParamW(g_hInst, MAKEINTRESOURCEW(IDD_MAIN), NULL, MainDialogProc, 0);

    SftDate_UnregisterApp(hinst);       /* Unregister from SftDate/DLL */

    return 0;
}

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