Well, it turned out to be much easier than expected. I had the impression that the MCN_VIEWCHANGE
notification is sent to the wndproc of the child handle. Instead it is sent to the DateTimePicker wndproc, but with the window handle of the MonthCalender child (that's why I didn't catch it in my first tries). So implementing a suitable handling of that notification turned out to be straight forward. Here is my implementation in Delphi extending the built-in TDateTimePicker
class:
const
MCN_VIEWCHANGE = MCN_FIRST - 4; // -750
type
tagNMVIEWCHANGE = record
nmhdr: TNmHdr;
dwOldView: DWORD;
dwNewView: DWORD;
end;
PNMNMVIEWCHANGE = ^TNMNMVIEWCHANGE;
TNMNMVIEWCHANGE = tagNMVIEWCHANGE;
type
{$SCOPEDENUMS ON}
TViewKind = (Month, Year, Decade, Century);
{$SCOPEDENUMS OFF}
TViewChange = procedure(Sender: TObject; OldView, NewView: TViewKind) of object;
type
TDateTimePicker = class(Vcl.ComCtrls.TDateTimePicker)
private
FOnViewChange: TViewChange;
procedure WMNotify(var Message: TWMNotify); message WM_NOTIFY;
protected
procedure ViewChange(OldView, NewView: TViewKind);
public
published
property OnViewChange: TViewChange read FOnViewChange write FOnViewChange;
end;
procedure TDateTimePicker.ViewChange(OldView, NewView: TViewKind);
begin
if Assigned(FOnViewChange) then FOnViewChange(Self, OldView, NewView);
end;
procedure TDateTimePicker.WMNotify(var Message: TWMNotify);
var
vwchg: PNMNMVIEWCHANGE;
begin
if Message.Msg = WM_NOTIFY then begin
vwchg := PNMNMVIEWCHANGE(Message.NMHdr);
if vwchg.nmhdr.code = MCN_VIEWCHANGE then begin
ViewChange(TViewKind(vwchg.dwOldView), TViewKind(vwchg.dwNewView));
end;
end;
inherited;
end;