This sample logs the events of two date fields and a month calendar in an MFC dialog. The appointment field refuses Saturdays and Sundays in its ValueValidating event, and the due date field accepts "+7" or "-3" as a number of days from today in its TextParsing event. The events of the three controls are handled through ON_EVENT_RANGE.
The source code is located at C:\Program Files (x86)\Softelvdm\SftDate OCX 2.0\Samples\VC++\EventViewer\MainDlg.cpp.
/****************************************************************************/
/* SftDate/OCX 2.0 - ActiveX Date and Calendar Controls */
/* Copyright (C) 2026 Softel vdm, Inc. All Rights Reserved. */
/****************************************************************************/
/* SftDate/OCX 2.0 sample. Guide: https://softelvdm.com/Vault/Softelvdm.com/llms/SftDate-OCX-2.0.txt, API reference: https://softelvdm.com/Vault/Softelvdm.com/llms/SftDate-OCX-2.0-reference.txt - see AGENTS.md. */
#include "stdafx.h"
#include "MainDlg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
static CString FormatDate(const VARIANT& date)
{
CString s = _T("(none)");
SYSTEMTIME st;
if (date.vt == VT_DATE && VariantTimeToSystemTime(date.date, &st))
s.Format(_T("%04d-%02d-%02d %02d:%02d"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute);
return s;
}
static CString Bool(VARIANT_BOOL value)
{
return value != VARIANT_FALSE ? _T("True") : _T("False");
}
/////////////////////////////////////////////////////////////////////////////
// Application logic
// Appointments are only accepted on weekdays.
static void ValidateAppointment(const VARIANT* pNewValue, VARIANT_BOOL* pCancel)
{
SYSTEMTIME st;
if (pNewValue->vt == VT_DATE && VariantTimeToSystemTime(pNewValue->date, &st) && (st.wDayOfWeek == 0 || st.wDayOfWeek == 6))
*pCancel = VARIANT_TRUE;
}
// Besides dates in the culture's format, the due date accepts +n and -n days from today.
static void ParseDueDate(LPCOLESTR lpszText, VARIANT_BOOL* pHandled, VARIANT* pValue)
{
CString text = lpszText;
text.Remove(_T(' '));
if (text.GetLength() < 2 || text.GetLength() > 5 || (text[0] != _T('+') && text[0] != _T('-')))
return;
if (text.Mid(1).SpanIncluding(_T("0123456789")) != text.Mid(1))
return;
int days = _ttoi(text.Mid(1)) * (text[0] == _T('-') ? -1 : 1);
SYSTEMTIME today;
GetLocalTime(&today);
today.wHour = today.wMinute = today.wSecond = today.wMilliseconds = 0;
DATE date = 0;
SystemTimeToVariantTime(&today, &date);
VariantClear(pValue);
pValue->vt = VT_DATE;
pValue->date = date + days;
*pHandled = VARIANT_TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainDlg dialog
BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
END_MESSAGE_MAP()
// The fields and the calendar share their event dispids, so each event has one handler for all three controls; MFC passes the
// control ID first (the leading VTS_I4).
BEGIN_EVENTSINK_MAP(CMainDlg, CDialog)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 1 /* ValueChanged */, OnValueChanged, VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 2 /* RangeChanged */, OnRangeChanged, VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 3 /* ValueValidating */, OnValueValidating, VTS_I4 VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 4 /* TextParsing */, OnTextParsing, VTS_I4 VTS_BSTR VTS_VARIANT VTS_PBOOL VTS_PVARIANT)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 5 /* DropDownOpening */, OnDropDownOpening, VTS_I4 VTS_PBOOL)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 6 /* DropDownOpened */, OnDropDownOpened, VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 7 /* DropDownClosed */, OnDropDownClosed, VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 9 /* DateActivated */, OnDateActivated, VTS_I4 VTS_DATE)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 10 /* ViewChanged */, OnViewChanged, VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 11 /* DisplayChanged */, OnDisplayChanged, VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 12 /* TodayClicked */, OnTodayClicked, VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 13 /* ClearClicked */, OnClearClicked, VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 14 /* DarkModeChange */, OnDarkModeChange, VTS_I4 VTS_BOOL)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 15 /* HighContrastChange */, OnHighContrastChange, VTS_I4 VTS_BOOL)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 16 /* DPIChanged */, OnDPIChanged, VTS_I4 VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 20 /* KeyDown */, OnKeyDown, VTS_I4 VTS_PI2 VTS_I2)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 21 /* KeyPress */, OnKeyPress, VTS_I4 VTS_PI2)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 22 /* KeyUp */, OnKeyUp, VTS_I4 VTS_PI2 VTS_I2)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 23 /* MouseDblClk */, OnMouseDblClk, VTS_I4 VTS_I2 VTS_I2 VTS_I4 VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 24 /* MouseDown */, OnMouseDown, VTS_I4 VTS_I2 VTS_I2 VTS_I4 VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 26 /* MouseUp */, OnMouseUp, VTS_I4 VTS_I2 VTS_I2 VTS_I4 VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 27 /* FocusIn */, OnFocusIn, VTS_I4)
ON_EVENT_RANGE(CMainDlg, IDC_APPOINTMENT, IDC_CALENDAR, 28 /* FocusOut */, OnFocusOut, VTS_I4)
END_EVENTSINK_MAP()
CMainDlg::CMainDlg(CWnd* pParent) : CDialog(CMainDlg::IDD, pParent), m_fLastEventDup(FALSE)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EVENTS, m_Events);
}
BOOL CMainDlg::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
// Route through SftDarkMode first so it can handle WM_CTLCOLOR*, WM_ERASEBKGND, etc.
INT_PTR dmResult;
BOOL fHandled = SftDarkMode_HandleDialogMessage(m_hWnd, message, wParam, lParam, &dmResult);
if (message == WM_SETTINGCHANGE)
ApplyListColors();
if (fHandled) {
if (pResult) *pResult = dmResult;
return TRUE;
}
return CDialog::OnWndMsg(message, wParam, lParam, pResult);
}
/////////////////////////////////////////////////////////////////////////////
// Event log
void CMainDlg::AddEvent(const CString& title, const CString& data)
{
CString text = data;
if (m_lastEvent == title) {
if (m_fLastEventDup)
return;
m_fLastEventDup = TRUE;
text = _T("Additional ") + title + _T(" events ... now suppressed");
} else {
m_lastEvent = title;
m_fLastEventDup = FALSE;
}
int index = m_Events.InsertItem(m_Events.GetItemCount(), title);
m_Events.SetItemText(index, 1, text);
m_Events.SetItemState(-1, 0, LVIS_SELECTED);
m_Events.SetItemState(index, LVIS_SELECTED, LVIS_SELECTED);
m_Events.EnsureVisible(index, FALSE);
}
void CMainDlg::OnClear()
{
m_Events.DeleteAllItems();
m_lastEvent.Empty();
m_fLastEventDup = FALSE;
}
// Logs an event with its data; DayFormatting is left out, it is fired for every day shown, and so is MouseMove.
void CMainDlg::LogEvent(UINT id, LPCTSTR lpszEvent, const CString& data, const CString& value)
{
CString title = id == IDC_APPOINTMENT ? _T("Appointment: ") : id == IDC_DUEDATE ? _T("DueDate: ") : _T("Calendar: ");
title += lpszEvent;
if (!value.IsEmpty())
title += _T(" (") + value + _T(")");
AddEvent(title, data);
}
BOOL CMainDlg::OnValueChanged(UINT id)
{
if (id == IDC_CALENDAR)
LogEvent(id, _T("ValueChanged"), CString(), FormatDate(m_Calendar->Value));
else
LogEvent(id, _T("ValueChanged"), CString(), (LPCTSTR) (id == IDC_APPOINTMENT ? m_Appointment : m_DueDate)->Text);
return TRUE;
}
BOOL CMainDlg::OnRangeChanged(UINT id)
{
LogEvent(id, _T("RangeChanged"));
return TRUE;
}
BOOL CMainDlg::OnValueValidating(UINT id, VARIANT* pNewValue, VARIANT* pNewRangeEnd, VARIANT_BOOL* pCancel)
{
if (id == IDC_APPOINTMENT)
ValidateAppointment(pNewValue, pCancel);
CString data;
data.Format(_T("NewValue=%s NewRangeEnd=%s Cancel=%s"), (LPCTSTR) FormatDate(*pNewValue), (LPCTSTR) FormatDate(*pNewRangeEnd), (LPCTSTR) Bool(*pCancel));
LogEvent(id, *pCancel ? _T("ValueValidating (canceled, a weekend)") : _T("ValueValidating"), data);
return TRUE;
}
BOOL CMainDlg::OnTextParsing(UINT id, LPCOLESTR lpszText, const VARIANT& current, VARIANT_BOOL* pHandled, VARIANT* pValue)
{
if (id == IDC_DUEDATE)
ParseDueDate(lpszText, pHandled, pValue);
CString data;
data.Format(_T("Text=%.80s Current=%s Handled=%s Value=%s"), lpszText, (LPCTSTR) FormatDate(current), (LPCTSTR) Bool(*pHandled), (LPCTSTR) FormatDate(*pValue));
LogEvent(id, _T("TextParsing"), data);
return TRUE;
}
BOOL CMainDlg::OnDropDownOpening(UINT id, VARIANT_BOOL* pCancel)
{
// *pCancel = VARIANT_TRUE would keep the dropdown closed
LogEvent(id, _T("DropDownOpening"), _T("Cancel=") + Bool(*pCancel));
return TRUE;
}
BOOL CMainDlg::OnDropDownOpened(UINT id)
{
LogEvent(id, _T("DropDownOpened"));
return TRUE;
}
BOOL CMainDlg::OnDropDownClosed(UINT id)
{
LogEvent(id, _T("DropDownClosed"));
return TRUE;
}
BOOL CMainDlg::OnDateActivated(UINT id, DATE date)
{
LogEvent(id, _T("DateActivated"), _T("Date=") + FormatDate(_variant_t(date, VT_DATE)));
return TRUE;
}
BOOL CMainDlg::OnViewChanged(UINT id)
{
static LPCTSTR views[] = { _T("Month"), _T("Year"), _T("Decade"), _T("Century") };
int view = m_Calendar->View;
LogEvent(id, _T("ViewChanged"), CString(), view >= 0 && view < 4 ? views[view] : _T("?"));
return TRUE;
}
BOOL CMainDlg::OnDisplayChanged(UINT id)
{
LogEvent(id, _T("DisplayChanged"), CString(), FormatDate(_variant_t(m_Calendar->DisplayStart, VT_DATE)));
return TRUE;
}
BOOL CMainDlg::OnTodayClicked(UINT id)
{
LogEvent(id, _T("TodayClicked"));
return TRUE;
}
BOOL CMainDlg::OnClearClicked(UINT id)
{
LogEvent(id, _T("ClearClicked"));
return TRUE;
}
BOOL CMainDlg::OnDarkModeChange(UINT id, VARIANT_BOOL active)
{
LogEvent(id, _T("DarkModeChange"), _T("Active=") + Bool(active));
return TRUE;
}
BOOL CMainDlg::OnHighContrastChange(UINT id, VARIANT_BOOL active)
{
LogEvent(id, _T("HighContrastChange"), _T("Active=") + Bool(active));
return TRUE;
}
BOOL CMainDlg::OnDPIChanged(UINT id, long dpi)
{
CString data;
data.Format(_T("DPI=%d"), dpi);
LogEvent(id, _T("DPIChanged"), data);
return TRUE;
}
BOOL CMainDlg::OnKeyDown(UINT id, short* pKeyCode, short shift)
{
// *pKeyCode = 0 would mark the key as handled, so the control ignores it
CString data;
data.Format(_T("KeyCode=0x%02X Shift=%d"), *pKeyCode, shift);
LogEvent(id, _T("KeyDown"), data);
return TRUE;
}
BOOL CMainDlg::OnKeyPress(UINT id, short* pKeyAscii)
{
CString data;
data.Format(_T("KeyAscii=%d"), *pKeyAscii);
LogEvent(id, _T("KeyPress"), data);
return TRUE;
}
BOOL CMainDlg::OnKeyUp(UINT id, short* pKeyCode, short shift)
{
CString data;
data.Format(_T("KeyCode=0x%02X Shift=%d"), *pKeyCode, shift);
LogEvent(id, _T("KeyUp"), data);
return TRUE;
}
static CString MouseData(short button, short shift, long x, long y)
{
CString data;
data.Format(_T("Button=%d Shift=%d x=%d y=%d"), button, shift, x, y);
return data;
}
BOOL CMainDlg::OnMouseDown(UINT id, short button, short shift, long x, long y)
{
LogEvent(id, _T("MouseDown"), MouseData(button, shift, x, y));
return TRUE;
}
BOOL CMainDlg::OnMouseUp(UINT id, short button, short shift, long x, long y)
{
LogEvent(id, _T("MouseUp"), MouseData(button, shift, x, y));
return TRUE;
}
BOOL CMainDlg::OnMouseDblClk(UINT id, short button, short shift, long x, long y)
{
LogEvent(id, _T("MouseDblClk"), MouseData(button, shift, x, y));
return TRUE;
}
BOOL CMainDlg::OnFocusIn(UINT id)
{
LogEvent(id, _T("FocusIn"));
return TRUE;
}
BOOL CMainDlg::OnFocusOut(UINT id)
{
LogEvent(id, _T("FocusOut"));
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// Layout
static CRect ChildRect(CWnd* pDlg, int id)
{
CRect rect;
pDlg->GetDlgItem(id)->GetWindowRect(&rect);
pDlg->ScreenToClient(&rect);
return rect;
}
// The calendar sizes itself to its month, so the event list and the dialog follow it.
void CMainDlg::Layout()
{
CRect du(7, 7, 0, 0);
CRect cal = ChildRect(this, IDC_CALENDAR);
CRect clear = ChildRect(this, IDC_CLEAR);
CRect list = ChildRect(this, IDC_EVENTS);
MapDialogRect(&du);
int top = max(cal.bottom, clear.bottom) + du.top;
int right = max(cal.right, list.right) + du.left;
m_Events.SetWindowPos(NULL, du.left, top, right - 2 * du.left, list.Height(), SWP_NOZORDER | SWP_NOACTIVATE);
int bottom = top + list.Height() + du.top;
m_Events.SetColumnWidth(1, LVSCW_AUTOSIZE_USEHEADER);
CRect window, client;
GetWindowRect(&window);
GetClientRect(&client);
SetWindowPos(NULL, 0, 0, window.Width() - client.Width() + right, window.Height() - client.Height() + bottom,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}
void CMainDlg::ApplyListColors()
{
if (!m_Events.m_hWnd)
return;
BOOL fDark = SftDarkMode_IsActive();
COLORREF back = fDark ? SFTDARKMODE_BG_COLOR : GetSysColor(COLOR_WINDOW);
COLORREF text = fDark ? SFTDARKMODE_TEXT_COLOR : GetSysColor(COLOR_WINDOWTEXT);
m_Events.SetBkColor(back);
m_Events.SetTextBkColor(back);
m_Events.SetTextColor(text);
SetWindowTheme(m_Events.GetHeaderCtrl()->m_hWnd, fDark ? L"DarkMode_ItemsView" : NULL, NULL);
m_Events.Invalidate();
}
/////////////////////////////////////////////////////////////////////////////
// Initialization
BOOL CMainDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// the format, the free-form editing, the week numbers and the Clear button were set at design time (in the dialog resource)
m_Appointment = GetDlgItem(IDC_APPOINTMENT)->GetControlUnknown();
m_DueDate = GetDlgItem(IDC_DUEDATE)->GetControlUnknown();
m_Calendar = GetDlgItem(IDC_CALENDAR)->GetControlUnknown();
m_Appointment->DarkMode = darkSftDateAuto;
m_Appointment->HighContrastMode = highContrastSftDateAuto;
m_DueDate->DarkMode = darkSftDateAuto;
m_DueDate->HighContrastMode = highContrastSftDateAuto;
m_Calendar->DarkMode = darkSftDateAuto;
m_Calendar->HighContrastMode = highContrastSftDateAuto;
m_Events.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER);
CRect width(0, 0, 250, 0);
MapDialogRect(&width);
m_Events.InsertColumn(0, _T("Event"), LVCFMT_LEFT, width.right);
m_Events.InsertColumn(1, _T("Event Data"), LVCFMT_LEFT, width.right);
SftDarkMode_ApplyToDialog(m_hWnd);
ApplyListColors();
Layout();
CenterWindow();
return TRUE;
}
void CMainDlg::OnOK()
{
// Enter must not close the sample
}