WinForm Custom(二) 滑块控件

农民  金牌会员 | 2022-6-20 10:42:25 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 871|帖子 871|积分 2613

  1. public class ZhmSlider : Control
  2.     {
  3.         private Rectangle foreRect;
  4.         private Rectangle backRect;
  5.         private Rectangle setRect;
  6.         private Color backgroundColor = Color.White;
  7.         private Color foregroundColor = Color.Gray;
  8.         private Color setRectColor = Color.Black;//滑块颜色
  9.         private Color fontColor = Color.Black;
  10.         private Color borderColor = Color.Black;
  11.         private int maximum = 100; //进度条最大值
  12.         private int minimum = 0;  //进度条最小值
  13.         private double _value = 0;//进度条当前值
  14.         private bool showPercent; //当前进度百分比
  15.         private float fontSize = 9;//
  16.         private FontFamily _fontFamily = new FontFamily("Segoe UI");
  17.         private Point originPoint;
  18.         private Point originsetRectPoint;
  19.         private bool setRectDown = false;
  20.         public ZhmSlider()
  21.         {
  22.             // 避免重绘时窗口闪烁
  23.             this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
  24.             this.MouseDown += ZhmSlider_MouseDown;
  25.             this.MouseMove += ZhmSlider_MouseMove;
  26.             this.MouseUp += ZhmSlider_MouseUp;
  27.         }
  28.         private void ZhmSlider_MouseUp(object sender, MouseEventArgs e)
  29.         {
  30.             setRectDown = false;
  31.         }
  32.         private void ZhmSlider_MouseMove(object sender, MouseEventArgs e)
  33.         {
  34.             if (setRectDown)
  35.             {
  36.                 int dd = e.Location.X - originPoint.X;
  37.                 double percent = (double)(originsetRectPoint.X + dd - this.backRect.X) / (this.backRect.Width - this.backRect.Height);
  38.                 if (percent < 0)
  39.                 {
  40.                     this.Value = minimum;
  41.                     this.foreRect.Width = 0;
  42.                     this.setRect.X = backRect.X;
  43.                 }
  44.                 else if (percent > 1)
  45.                 {
  46.                     this.Value = maximum;
  47.                     this.foreRect.Width = this.backRect.Width;
  48.                     this.setRect.X = backRect.X + backRect.Width - backRect.Height;
  49.                 }
  50.                 else
  51.                 {
  52.                     this.Value = percent * maximum;
  53.                     this.foreRect.Width = (int)(percent * this.backRect.Width);
  54.                     this.setRect.X = originsetRectPoint.X + dd;
  55.                 }
  56.                 Invalidate();
  57.             }
  58.         }
  59.         private void ZhmSlider_MouseDown(object sender, MouseEventArgs e)
  60.         {
  61.             if (setRect.Contains(e.Location))
  62.             {
  63.                 this.originPoint = e.Location;
  64.                 originsetRectPoint = this.setRect.Location;
  65.                 this.setRectDown = true;
  66.             }
  67.         }
  68.         [Category("扩展属性"), Browsable(true)]
  69.         public bool ShowPercentTag
  70.         {
  71.             get { return showPercent; }
  72.             set
  73.             {
  74.                 showPercent = value;
  75.                 Invalidate();
  76.             }
  77.         }
  78.         [Category("扩展属性"),  Browsable(true)]
  79.         public int Maximum
  80.         {
  81.             get { return maximum; }
  82.             set
  83.             {
  84.                 maximum = value;
  85.                 Invalidate();
  86.             }
  87.         }
  88.         [Category("扩展属性"),  Browsable(true)]
  89.         public int Minimum
  90.         {
  91.             get { return minimum; }
  92.             set
  93.             {
  94.                 minimum = value;
  95.                 Invalidate();
  96.             }
  97.         }
  98.         [Category("扩展属性"), Browsable(true)]
  99.         public float FontSize
  100.         {
  101.             get { return fontSize; }
  102.             set
  103.             {
  104.                 this.fontSize = value;
  105.                 Invalidate();
  106.             }
  107.         }
  108.         [Category("扩展属性"), Browsable(true)]
  109.         public FontFamily FontFamily
  110.         {
  111.             get { return _fontFamily; }
  112.             set
  113.             {
  114.                 this._fontFamily = value;
  115.                 Invalidate();
  116.             }
  117.         }
  118.         [Category("扩展属性"), Browsable(true)]
  119.         public Color BackgroundColor
  120.         {
  121.             get { return backgroundColor; }
  122.             set
  123.             {
  124.                 this.backgroundColor = value;
  125.                 Invalidate();
  126.             }
  127.         }
  128.         [Category("扩展属性"), Browsable(true)]
  129.         public Color ForegroundColor
  130.         {
  131.             get { return foregroundColor; }
  132.             set
  133.             {
  134.                 this.foregroundColor = value;
  135.                 Invalidate();
  136.             }
  137.         }
  138.         [Category("扩展属性"), Browsable(true)]
  139.         public Color SetRectColor
  140.         {
  141.             get { return setRectColor; }
  142.             set
  143.             {
  144.                 this.setRectColor = value;
  145.                 Invalidate();
  146.             }
  147.         }
  148.         [Category("扩展属性"), Browsable(true)]
  149.         public Color FontColor
  150.         {
  151.             get { return fontColor; }
  152.             set
  153.             {
  154.                 this.fontColor = value;
  155.                 Invalidate();
  156.             }
  157.         }
  158.         [Category("扩展属性"), Browsable(true)]
  159.         public Color BorderColor
  160.         {
  161.             get { return borderColor; }
  162.             set
  163.             {
  164.                 this.borderColor = value;
  165.                 Invalidate();
  166.             }
  167.         }
  168.         //确定控件的位置。 我们根据宽度和高度属性确定矩形的位置。 因为Control类也有这两个属性,所以我们在前面添加new来覆盖原来的属性
  169.         [Category("扩展属性"), Browsable(true)]
  170.         public new int Width
  171.         {
  172.             get { return base.Width; }
  173.             set
  174.             {
  175.                 base.Width = value;
  176.                 foreRect.X = backRect.X = base.Width / 20;
  177.                 backRect.Width = base.Width * 9 / 10;
  178.                 foreRect.Width = (int)(_value / maximum * backRect.Width);
  179.                 setRect.X = (int)(_value / maximum * (backRect.Width - backRect.Height) + foreRect.X);
  180.                 Invalidate();
  181.             }
  182.         }
  183.         [Category("扩展属性"), Browsable(true)]
  184.         public new int Height
  185.         {
  186.             get { return base.Height; }
  187.             set
  188.             {
  189.                 base.Height = value;
  190.                 foreRect.Height = backRect.Height = setRect.Height = setRect.Width = base.Height / 3;
  191.                 foreRect.Y = backRect.Y = setRect.Y = base.Height / 3;
  192.                 Invalidate();
  193.             }
  194.         }
  195.         //值发生变化的属性。 当向事件添加外部响应函数时,事件将生效,否则OnValueChanged方法的值为空
  196.         protected EventHandler OnValueChanged;
  197.         public event EventHandler ValueChanged
  198.         {
  199.             add
  200.             {
  201.                 if (OnValueChanged != null)
  202.                     foreach (Delegate d in OnValueChanged.GetInvocationList())
  203.                         if (object.ReferenceEquals(d, value)) return;
  204.                 OnValueChanged = (EventHandler)Delegate.Combine(OnValueChanged, value);
  205.             }
  206.             remove
  207.             {
  208.                 OnValueChanged = (EventHandler)Delegate.Remove(OnValueChanged, value);
  209.             }
  210.         }
  211.         //定义value的值。 当Value值发生变化时,重置矩形的进度,控制块的位置,并重新绘制控件(如果在value属性中修改进度条的值,请使用_value变量,在其他地方,请使Value属性 )
  212.         [Category("扩展属性"), Browsable(true)]
  213.         public double Value
  214.         {
  215.             get { return _value; }
  216.             set
  217.             {
  218.                 if (_value < Minimum)
  219.                     throw new ArgumentException("Less than minimum");
  220.                 if (_value > Maximum)
  221.                     throw new ArgumentException("Exceeds the maximum");
  222.                 _value = value;
  223.                 foreRect.Width = (int)(_value / maximum * backRect.Width);
  224.                 setRect.X = (int)(_value / maximum * (backRect.Width - backRect.Height) + backRect.X);
  225.                 if ((_value - maximum) > 0)
  226.                 {
  227.                     foreRect.Width = backRect.Width;
  228.                     setRect.X = backRect.Width - backRect.Height + backRect.X;
  229.                 }
  230.                 if (OnValueChanged != null)
  231.                     OnValueChanged(this, EventArgs.Empty);
  232.                 Invalidate();
  233.             }
  234.         }
  235.         //绘制控件,重载OnPaint方法来绘制控件
  236.         protected override void OnPaint(PaintEventArgs pe)
  237.         {
  238.             base.OnPaint(pe);
  239.             DrawRect(pe.Graphics);
  240.             DrawText(pe.Graphics);
  241.         }
  242.         //滑块区域
  243.         private void DrawRect(Graphics e)
  244.         {
  245.             Pen pen = new Pen(this.foregroundColor);
  246.             e.FillRectangle(new SolidBrush(this.backgroundColor), backRect);
  247.             e.DrawRectangle(new Pen(this.borderColor), backRect);
  248.             e.FillRectangle(new SolidBrush(this.foregroundColor), foreRect);
  249.             e.DrawRectangle(new Pen(this.borderColor), foreRect);
  250.             e.FillRectangle(new SolidBrush(this.setRectColor), setRect);
  251.             e.DrawRectangle(new Pen(this.borderColor), setRect);
  252.         }
  253.          // 绘制文本
  254.         private void DrawText(Graphics e)
  255.         {
  256.             Point point = new Point();
  257.             point.X = this.backRect.X + this.backRect.Width * 3 / 7;
  258.             point.Y = this.backRect.Y + this.backRect.Height / 3;
  259.             SolidBrush brush = new SolidBrush(fontColor);
  260.             Font font = new Font(_fontFamily, this.fontSize);
  261.             string percent = ((int)this._value).ToString() + "%";
  262.             //设置文本居中
  263.             StringFormat format = new StringFormat();
  264.             format.Alignment = StringAlignment.Center;
  265.             format.LineAlignment = StringAlignment.Center;
  266.             e.DrawString(percent, font, brush, backRect, format);
  267.         }
  268.         //在设计期间更改控件的大小时调用OnResize方法。 当您拖动边缘上的箭头来更改控件的大小时,当需要对控件进行相应更改时,可以重写此方法。如果没有重新加载,则只有在修改完成后才会更新控件。
  269.         protected override void OnResize(EventArgs e)
  270.         {
  271.             base.OnResize(e);
  272.             this.Width = Width;
  273.             this.Height = Height;
  274.             Invalidate();
  275.         }
  276.     }
复制代码
来源:https://www.cnblogs.com/zhuanghamiao/archive/2022/06/13/winform-custom-slider.html
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

农民

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表