• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi用ToolButton和MonthCalendar实现DateTimePicker的功能

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

效果图如下:

实现平台: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.

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi7学习开发控件(继承TGraphicControl只画一条线)发布时间:2022-07-18
下一篇:
DelphiInterfaces发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap