<返回更多

C# Winform DatePicker 重绘

2022-04-02    斜杠亮子
加入收藏

常设置属性、事件

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;
        }
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>