Changing the state/format of a TMonthCalendar in Delphi

956 views Asked by At

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.

1

There are 1 answers

1
Sertac Akyuz On BEST ANSWER

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).