加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Date日期选择美化控件----------WinForm控件开发系列

发布时间:2020-12-15 07:50:27 所属栏目:Java 来源:网络整理
导读:下面是控件显示界面源码,日期选择面板源码太多了3000行左右,没有贴出来,可以下载源码去看。你可以完善这个日期控件。 /// summary /// 日期选择美化控件 /// /summary [ToolboxItem( true )] [DefaultProperty( " DatePicker " )] [Description( " 日期选

下面是控件显示界面源码,日期选择面板源码太多了3000行左右,没有贴出来,可以下载源码去看。你可以完善这个日期控件。

 /// <summary>
  /// 日期选择美化控件
  /// </summary>
  [ToolboxItem(true)]
  [DefaultProperty("DatePicker")]
  [Description("日期选择美化控件")]
  public partial class DateExt : Control
  {
    #region

    private bool backBorderShow = true;
    /// <summary>
    /// 是否显示边框
    /// </summary>
    [DefaultValue(true)]
    [Description("是否显示边框")]
    public bool BackBorderShow
    {
      get { return this.backBorderShow; }
      set
      {
        if (this.backBorderShow == value)
          return;
        this.backBorderShow = value;
        this.Invalidate();
      }
    }

    private ImageAnchors imageAnchor = ImageAnchors.Right;
    /// <summary>
    /// 图片位置
    /// </summary>
    [DefaultValue(ImageAnchors.Right)]
    [Description("图片位置")]
    public ImageAnchors ImageAnchor
    {
      get { return this.imageAnchor; }
      set
      {
        if (this.imageAnchor == value)
          return;
        this.imageAnchor = value;
        this.Invalidate();
      }
    }

    private Color backBorderColor = Color.FromArgb(192,192,192);
    /// <summary>
    /// 边框颜色
    /// </summary>
    [DefaultValue(typeof(Color),"192,192,192")]
    [Description("边框颜色")]
    [Editor(typeof(ColorEditorExt),typeof(System.Drawing.Design.UITypeEditor))]
    public Color BackBorderColor
    {
      get { return this.backBorderColor; }
      set
      {
        if (this.backBorderColor == value)
          return;
        this.backBorderColor = value;
        this.Invalidate();
      }
    }

    private DatePickerExt datePicker = null;
    /// <summary>
    /// 日期选择面板
    /// </summary>
    [Browsable(true)]
    [Description("日期选择面板")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public DatePickerExt DatePicker
    {
      get { return this.datePicker; }
      set { this.datePicker = value; }
    }

    protected override Size DefaultSize
    {
      get
      {
        return new Size(130,23);
      }
    }

    protected override Cursor DefaultCursor
    {
      get
      {
        return Cursors.Hand;
      }
    }

    /// <summary>
    /// 日期面板显示状态
    /// </summary>
    private bool DisplayStatus = false;

    private ToolStripDropDown tsdd = null;

    private ToolStripControlHost tsch = null;

    private readonly StringFormat date_sf = new StringFormat() { Alignment = StringAlignment.Near,LineAlignment = StringAlignment.Center,Trimming = StringTrimming.None,FormatFlags = StringFormatFlags.NoWrap };

    #endregion

    public DateExt()
    {
      SetStyle(ControlStyles.UserPaint,true);
      SetStyle(ControlStyles.AllPaintingInWmPaint,true);
      SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
      SetStyle(ControlStyles.ResizeRedraw,true);
      SetStyle(ControlStyles.SupportsTransparentBackColor,true);
      InitializeComponent();

      this.BackColor = Color.FromArgb(255,255,255);
      this.ForeColor = Color.FromArgb(105,105,105);
      this.Font = new Font("微软雅黑",9);

      this.DatePicker = new DatePickerExt();
      this.tsdd = new ToolStripDropDown() { Padding = Padding.Empty };
      this.tsch = new ToolStripControlHost(this.DatePicker) { Margin = Padding.Empty,Padding = Padding.Empty };
      tsdd.Items.Add(this.tsch);


      this.tsdd.Closed += new ToolStripDropDownClosedEventHandler(this.tsdd_Closed);
      this.DatePicker.BottomBarConfirmClick += new DatePickerExt.BottomBarIiemClickEvent(this.DatePicker_BottomBarConfirmClick);

    }

    protected override void OnPaint(PaintEventArgs e)
    {
      base.OnPaint(e);
      Graphics g = e.Graphics;

      if (this.BackBorderShow)
      {
        Pen backborder_pen = new Pen(this.BackBorderColor,1);
        g.DrawRectangle(backborder_pen,new Rectangle(e.ClipRectangle.X,e.ClipRectangle.Y,e.ClipRectangle.Width - 1,e.ClipRectangle.Height - 1));
        backborder_pen.Dispose();
      }

      string date_format = this.DatePicker.DateDisplayType == DatePickerExt.DateDisplayTypes.Year ? "yyyy" : (this.DatePicker.DateDisplayType == DatePickerExt.DateDisplayTypes.YearMonth ? "yyyy-MM" : "yyyy-MM-dd");
      int image_width = global::AnimationLibrary.Properties.Resources.date.Width;
      int image_height = global::AnimationLibrary.Properties.Resources.date.Height;
      Rectangle image_rect = new Rectangle(e.ClipRectangle.Right - image_width - 2,(e.ClipRectangle.Y + e.ClipRectangle.Height - image_height) / 2,16,16);
      Rectangle date_rect = new Rectangle(e.ClipRectangle.X + 5,e.ClipRectangle.Width - image_width - 12,e.ClipRectangle.Height);
      if (this.ImageAnchor == ImageAnchors.Left)
      {
        image_rect = new Rectangle(e.ClipRectangle.X + 2,16);
        date_rect = new Rectangle(e.ClipRectangle.X + 2 + image_width + 5,e.ClipRectangle.Height);
      }
      SolidBrush date_sb = new SolidBrush(this.ForeColor);
      g.DrawImage(global::AnimationLibrary.Properties.Resources.date,image_rect);
      g.DrawString(this.DatePicker.Value.ToString(date_format),this.Font,date_sb,date_rect,date_sf);
      date_sb.Dispose();
    }

    protected override void OnClick(EventArgs e)
    {
      base.OnClick(e);
      if (!this.DisplayStatus)
      {
        tsdd.Show(this.PointToScreen(new Point(0,this.Height + 2)));
      }
    }

    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
        if (this.date_sf != null)
          this.date_sf.Dispose();
      }
      base.Dispose(disposing);
    }

    private void DatePicker_BottomBarConfirmClick(object sender,DatePickerExt.BottomBarIiemEventArgs e)
    {
      this.tsdd.Close();
      this.DisplayStatus = false;
      this.Invalidate();
    }

    private void tsdd_Closed(object sender,ToolStripDropDownClosedEventArgs e)
    {
      this.Invalidate();
      this.DisplayStatus = false;
    }

    /// <summary>
    /// 图片位置
    /// </summary>
    [Description("图片位置")]
    public enum ImageAnchors
    {
      /// <summary>
      /// 左边
      /// </summary>
      Left,/// <summary>
      /// 右边
      /// </summary>
      Right
    }

  }

源码下载:日期选择美化控件.zip

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读