C# Winform DatePicker 重绘

站UI设计

  常设置属性、事件

  CustomFormat:当Format属性设置为自定义类型时可自定义控件时间的显示格式;Enabled:指示是否启用该控件,true为启用状态可编辑,false为禁用状态不可编辑;MaxDate:设置控件可选择或输入的最大日期;MinDate:设置控件可选择或输入的最小日期;Name:指示代码中用来标识该对象的名称;ShowUpDown:是否使用下拉日历修改日期,false为下拉日历模式,true为区域数字增减模式;Text:与控件关联的文本,显示给用户看的内容说明;ValueChanged事件:控件值更改时发生;

   public class BzDatePicker : DateTimePicker { private Color skinColor = Color.MediumSlateBlue; private Color textColor = Color.White; private Color borderColor = Color.PaleVioletRed; private int borderSize = 0; private bool drppedDown = false; private Image calarIcon = Properties.Resources.calar_white_m; private RectangleF iconbuttonArea; private const int calarIconWidth = 34; private const int arrowIconWidth = 17; [Category("BZ Advance")] public Color SkinColor { get { return skinColor; } set { skinColor = value; if (skinColor.GetBrightness() >= 0.8F) { calarIcon = Properties.Resources.calar_white_m; } else { calarIcon = Properties.Resources.calar_drak_m; } this.Invalidate(); } } [Category("BZ Advance")] public Color TextColor { get { return textColor; } set { textColor = value; this.Invalidate(); } } [Category("BZ Advance")] public Color BorderColor { get { return borderColor; } set { borderColor = value; this.Invalidate(); } } [Category("BZ Advance")] public int BorderSize { get { return borderSize; } set { borderSize = value; this.Invalidate(); } } public BzDatePicker() { this.SetStyle(ControlStyles.UserPaint, true); this.MinimumSize = new Size(0, 35); this.Font = new Font(this.Font.Name, 9.5F); } protected override void OnDropDown(EventArgs eventargs) { base.OnDropDown(eventargs); drppedDown = true; } protected override void OnCloseUp(EventArgs eventargs) { base.OnCloseUp(eventargs); drppedDown = false; } protected override void OnKeyPress(KeyPressEventArgs e) { base.OnKeyPress(e); e.Handled = true; } /// <summary> /// 重绘 /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { using (Graphics g = this.CreateGraphics()) using (Pen penBorder = new Pen(borderColor, BorderSize)) using (SolidBrush skinBrush = new SolidBrush(skinColor)) using (SolidBrush openIocnBrush = new SolidBrush(Color.FromArgb(50, 64, 64, 64))) using (SolidBrush textBrush = new SolidBrush(textColor)) using (StringFormat textFormat = new StringFormat()) { RectangleF clientArea = new RectangleF(0, 0, this.Width - 0.5F, this.Height - 0.5F); RectangleF iconArea = new RectangleF(clientArea.Width - calarIconWidth, 0, calarIconWidth, clientArea.Height); penBorder.Alignment = PenAlignment.Inset; textFormat.LineAlignment = StringAlignment.Center; g.FillRectangle(skinBrush, clientArea); g.DrawString(" " + this.Text, this.Font, textBrush, clientArea, textFormat); if (drppedDown) { g.FillRectangle(openIocnBrush, iconArea); } if (borderSize >= 1) { g.DrawRectangle(penBorder, clientArea.X, clientArea.Y, clientArea.Width, clientArea.Height); } g.DrawImage(calarIcon, this.Width - calarIcon.Width-5, (this.Height - calarIcon.Height) / 2); } } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); int iconWidth = GetIconButtonWidth(); iconbuttonArea= new RectangleF(this.Width - iconWidth, 0, iconWidth, this.Height); } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (iconbuttonArea.Contains(e.Location)) { this.Cursor = Cursors.Hand; } else { this.Cursor = Cursors.Default; } } /// <summary> /// 设置图标 /// </summary> /// <returns></returns> private int GetIconButtonWidth() { int textWidth = TextRerer.MeasureText(this.Text, this.Font).Width; if (textWidth < this.Width - (calarIconWidth + 20)) { return calarIconWidth; } return arrowIconWidth; }

标签: 站UI设计