Delphi 用ToolButton和MonthCalendar实现DateTimePicker的功能

效果图如下:

实现平台:xp xe2,其中以上功能的实现,核心主要是参考了万一老师的资料,连接:http://www.cnblogs.com/del/archive/2011/05/12/2044112.html

完整代码如下:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ImgList, RzButton,
  Vcl.ExtCtrls, RzPanel, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    ImageList1: TImageList;
    RzToolbar1: TRzToolbar;
    RzToolButton1: TRzToolButton;
    MonthCalendar1: TMonthCalendar;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure RzToolButton1Click(Sender: TObject);
    procedure MonthCalendar1Click(Sender: TObject);
    procedure MonthCalendar1GetMonthBoldInfo(Sender: TObject; Month,
      Year: Cardinal; var MonthBoldInfo: Cardinal);
  private
    { Private declarations }
    procedure MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer);
    procedure MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  yy,mm,dd: string;  //存年月日
  y1,m1,d1: Word;  //从DecordDate函数解析年月日,然后传值给yy,mm,dd

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  DecodeDate(MonthCalendar1.Date,y1,m1,d1);
  yy := IntToStr(y1);
  mm := IntToStr(m1);
  dd := IntToStr(d1);
  MonthCalendar1.Visible := False;
  RzToolButton1.Caption := DateToStr(MonthCalendar1.Date);
  //TFrom和TMonthCalendar都是由TControl的派生来的,TMonthCalendar通过TForm间接继承OnMouseUp、OnMouseDown属性
  TForm1(MonthCalendar1).OnMouseUp := MouseUp;
  TForm1(MonthCalendar1).OnMouseDown := MouseDown;
end;

procedure TForm1.RzToolButton1Click(Sender: TObject);
begin
  MonthCalendar1.Visible := not MonthCalendar1.Visible;
  if MonthCalendar1.Visible then
  begin
    MonthCalendar1.BringToFront;
    Memo1.Align := alRight;
    Memo1.Width := 100;
  end else Memo1.Align := alClient;
end;

procedure TForm1.MonthCalendar1Click(Sender: TObject);
begin
  RzToolButton1.Caption := DateToStr(MonthCalendar1.Date);
end;

procedure TForm1.MonthCalendar1GetMonthBoldInfo(Sender: TObject; Month,
  Year: Cardinal; var MonthBoldInfo: Cardinal);
begin
  yy := Format('%u',[Year]);
  RzToolButton1.Caption := yy + '-' + mm + '-' + dd;
end;

procedure TForm1.MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecodeDate(MonthCalendar1.Date,y1,m1,d1);
  dd := IntToStr(d1);
  if Y >= 48 then  //测量所得,单击日历横线下面的区域日历隐藏
  begin
    MonthCalendar1.Visible := False;
    Memo1.Align := alClient;
  end;
end;

procedure TForm1.MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer);
begin
  DecodeDate(MonthCalendar1.Date,y1,m1,d1);
  mm := IntToStr(m1);
  //dd := IntToStr(d1);
  RzToolButton1.Caption := DateToStr(MonthCalendar1.Date);
end;

end.

 

转载于:https://www.cnblogs.com/go-jzg/p/4120011.html

制作一个简易的万年历 string intmonth = monthCalendar1.TodayDate.Month.ToString(); string intday = monthCalendar1.TodayDate.Day.ToString(); if (monthCalendar1.TodayDate.Month < 10) { intmonth = "0" + monthCalendar1.TodayDate.Month.ToString(); } if (monthCalendar1.TodayDate.Day < 10) { intday = "0" + monthCalendar1.TodayDate.Day.ToString(); } string s = String.Format("{0}年{1}月{2}", GetStemBranch(monthCalendar1.TodayDate), GetMonth(monthCalendar1.TodayDate), GetDay(monthCalendar1.TodayDate)); label1.Text = monthCalendar1.TodayDate + "年" + intmonth + "月" + intday + "日" + " " + s + " " + getReturnYear(monthCalendar1.TodayDate)+"年"; label1.ForeColor = Color.Green; } public string GetStemBranch(DateTime time) { string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥"; string CelestialStem = "甲乙丙丁戊己庚辛壬癸"; int sexagenaryYear = cc.GetSexagenaryYear(time);//计算与指定日期对应的甲子(60年)循环中的年,此方法返回甲子循环中的一个从1到60的数字,它与date参数对应 string stemBranch = CelestialStem.Substring(cc.GetCelestialStem(sexagenaryYear) - 1, 1) + TerrestrialBranch.Substring(cc.GetTerrestrialBranch(sexagenaryYear) - 1, 1);//substring() 方法返回的子串包括 start 处的字符,但不包括 stop 处的字符 return stemBranch; } public string GetMonth(DateTime time) { string[] ChineseMonthName = new string[] { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" }; int month =cc.GetMonth(time);//获取当前日期的月份 int year = cc.GetYear(time);//获取当前日期的年份 int leap = 0; for (int i = 3; i <= month; i++) { if (cc.IsLeapMonth(year,i))//判断是否是闰年 { leap = i;//一年中最多有一个闰月 break; } } if (leap > 0) month--; return (leap == month + 1 ? "闰" : "") +ChineseMonthName[month - 1];//返回农历月份
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值