3

Using Delphi XE6, I am creating a TdateTimePicker-like control, but for a couple of reasons, I am using a TButtonedEdit which has a TMonthCalendar "embedded" within it. These are defined thus:

  TMyMonthCalendar = class(TMonthCalendar)
    procedure DoKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure DoCloseUp(Sender: TObject);
  private
    FDroppedDown: boolean;
    procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
  protected
  end;

  TMyDateEdit = class(TButtonedEdit)
  private
    FMonthCalendar: TMyMonthCalendar;

    procedure DoRightButtonClick(Sender: TObject);
  protected
    procedure CreateWnd; override;
  public
    constructor Create(AOwner:TComponent); override;
    destructor Destroy; override;
  end;

I have pretty much got it going as desired with the month calendar being SHOWn when the right button is clicked and I HIDE it when a selection is made, the user navigates away, ESCapes etc.

What is confounding me is this: With the calendar "dropped down", if the user clicks or double clicks on the title the calendar changes to a grid of months or years accordingly. If the user ESCapes, for example, at that point, when I next drop the calendar down it returns to that months/years state whereas I would like it to revert to showing the current month of its Date property (as a TDateTimePicker does).

How can I revert the display of the Month calendar to its single month state before showing it?

Edit: I believe I have found the answer: After hiding the MonthCalendar, I simply call RecreateWnd and the underlying screen object is destroyed and recreated from scratch when next called for. And as it comes up in the single month format, which is what I want, nothing further need be done.

TomB
  • 750
  • 4
  • 17
  • That is normal behavior. Put a stock monthcalendar on a form, click on the title (which the month on the title will even change its color hinting that something will occur when you click), and it will display months and so on... – Sertac Akyuz Dec 18 '16 at 00:15
  • Yes, but if you close it (by ESCaping or navigating to another control), when you drop the TdateTimePicker down again, it will not drop down in that previous "grid of months/years" state, but rather back to its "default" single month state. I want to know how to force it backj into that "default" state. – TomB Dec 18 '16 at 05:02
  • Ok, write a procedure to set the view, e.g. `procedure SetCalendarView(Calendar: TMonthCalendar; ViewType: TViewType);` and call it appropriately. E.g. `SetCalendarView(MyMonthCalendar1, vtMonth);`. – Sertac Akyuz Dec 18 '16 at 12:17
  • Recreating seems a bit overkill.. – Sertac Akyuz Dec 19 '16 at 11:55

1 Answers1

2

You can call MonthCal_SetCurrentView to set the view for a month calendar ( or send a MCM_SETCURRENTVIEW).

uses commctrl;

MonthCal_SetCurrentView(FMonthCalendar.Handle, MCMV_MONTH);

If you ever need to retrieve the current view, you can use MonthCal_GetCurrentView (or MCM_GETCURRENTVIEW).

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
  • Thanks for that. This does indeed do the trick. I'm kicking myself for not finding it though. – TomB Dec 19 '16 at 18:18