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

文章插图
【C# Winform DatePicker 重绘】
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 calendarIcon = Properties.Resources.calendar_white_m;private RectangleF iconbuttonArea;private const int calendarIconWidth = 34;private const int arrowIconWidth = 17;[Category("BZ Advance")]public Color SkinColor{get{return skinColor;}set{skinColor = value;if (skinColor.GetBrightness() >= 0.8F){calendarIcon = Properties.Resources.calendar_white_m;}else{calendarIcon = Properties.Resources.calendar_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 - calendarIconWidth, 0, calendarIconWidth, 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(calendarIcon, this.Width - calendarIcon.Width-5, (this.Height - calendarIcon.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 = TextRenderer.MeasureText(this.Text, this.Font).Width;if (textWidth < this.Width - (calendarIconWidth + 20)){return calendarIconWidth;}return arrowIconWidth;}
推荐阅读
- 壁纸主题|6K分辨率!开发者重绘P3广色域Mac OS X经典壁纸:免费下载
- 童年|国外插画师将卡通角色重绘,这些童年回忆你还能认出来吗?
- 报表控件全新升级!支持WinForm、WPF,增强PDF导出
- 小暖男石头哥C# Winform使用iconfont等字体图标
- 薄情先生|UI开发框架,分享一款国内开源实用精美的WinForm
- 薄情先生|分享一款国内开源实用精美的WinForm UI开发框架
- 西红柿小生|Winform界面开发中的困境及解决方案
- 游龙战神|Winform界面开发:WinForms应用程序中的MDI、SDI和MFI接口类型
- 古玩趣谈|2020界面开发新纪元——Spreadsheet控件升级,Winforms
- 中国娱乐网|Choi重绘《小王子》,经典名著焕发全新生命力,陋室五月Steven