• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

winform滚动条美化-Panel

武飞扬头像
张小磊QQ
帮助1

      查阅网上关于滚动条的美化资料,多数不符合我的要求;打算动手自己撸一个;最终实现效果如下图:

学新通

实现方式为隐藏系统滚动条,设置控件工作区域,把自定义滚动条放到对应的位置实现滚动操作;后面会一步步的实现该功能;

参考资料包含不限于:

C#程序员开发WinForm必须知道的 Window 消息大全-阿里云开发者社区

首先是自定义滚动条的实现

1.创建滚动条控件继承Control

2.添加一些必要的属性:滚动条最大值、背景色、滑块颜色等等

3.绘制自己想要的样式并添加行为事件

绘制滑块圆角代码

  1.  
    private void DrawSlider(Graphics g)
  2.  
    {
  3.  
    GraphicsPath path;
  4.  
    if (rectSlider.Width == 0 || rectSlider.Height == 0)
  5.  
    {
  6.  
    return;
  7.  
    }
  8.  
    if (rectSlider.Width > rectSlider.Height)
  9.  
    {
  10.  
    //水平
  11.  
    path = DrawHelper.GetHorizontal(this.rectSlider);
  12.  
    }
  13.  
    else
  14.  
    {
  15.  
    //垂直
  16.  
    path = DrawHelper.GetVertical(this.rectSlider);
  17.  
    }
  18.  
     
  19.  
    using (SolidBrush sb = new SolidBrush(SliderColor))
  20.  
    {
  21.  
    g.FillPath(sb, path);
  22.  
    }
  23.  
    path.Dispose();
  24.  
     
  25.  
    }
学新通

重新鼠标按下(OnMouseDown)抬起(OnMouseUp)和移动(OnMouseMove)的事件来实现我们需要完成的操作,需要记录鼠标按下的点处理移动距离;触发事件的时机可自行调整;

展示代码为demo示例,存在很大优化空间

  1.  
    public class VSliderControl : Control
  2.  
    {
  3.  
    protected override CreateParams CreateParams
  4.  
    {
  5.  
    get
  6.  
    {
  7.  
    CreateParams cp = base.CreateParams;
  8.  
    cp.ExStyle &= ~0x02000000;
  9.  
    return cp;
  10.  
    }
  11.  
    }
  12.  
     
  13.  
    private int yLocation;
  14.  
    private bool isMouseDown = false;
  15.  
    private float movePix = 0;
  16.  
    //最大值
  17.  
    private double maxPos = 100;
  18.  
    //当前位置
  19.  
    private int pos = 1;
  20.  
    public int Pos
  21.  
    {
  22.  
    get { return pos; }
  23.  
    set
  24.  
    {
  25.  
    if (value > maxPos||value<0)
  26.  
    { return; }
  27.  
    pos = value;
  28.  
    SetMovePix();
  29.  
    this.Invalidate();
  30.  
    }
  31.  
    }
  32.  
    //平均1 Pos多少像素
  33.  
    private double avePos = 1;
  34.  
     
  35.  
    public new event EventHandler Scroll = null;
  36.  
     
  37.  
    //滑块大小
  38.  
    private RectangleF rectSlider;
  39.  
     
  40.  
    private Color sliderColor;
  41.  
    public Color SliderColor { get => sliderColor; set => sliderColor = value; }
  42.  
     
  43.  
     
  44.  
     
  45.  
    public double MaxPos
  46.  
    {
  47.  
    get { return maxPos; }
  48.  
    set
  49.  
    {
  50.  
    if (maxPos != value)
  51.  
    {
  52.  
    maxPos = value;
  53.  
    this.AutoCalcSize();
  54.  
    }
  55.  
    }
  56.  
    }
  57.  
     
  58.  
     
  59.  
    public VSliderControl()
  60.  
    {
  61.  
    this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  62.  
    this.SetStyle(ControlStyles.DoubleBuffer, true);
  63.  
    this.SetStyle(ControlStyles.ResizeRedraw, true);
  64.  
    this.SetStyle(ControlStyles.Selectable, true);
  65.  
    this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  66.  
    this.SetStyle(ControlStyles.UserPaint, true);
  67.  
    }
  68.  
    protected override void OnPaint(PaintEventArgs e)
  69.  
    {
  70.  
     
  71.  
    base.OnPaint(e);
  72.  
     
  73.  
    Graphics g = e.Graphics;
  74.  
    g.SetGDIHigh();
  75.  
     
  76.  
    DrawSlider(g);
  77.  
     
  78.  
     
  79.  
    }
  80.  
    protected override void OnMouseDown(MouseEventArgs e)
  81.  
    {
  82.  
    base.OnMouseDown(e);
  83.  
    if (rectSlider.Contains(e.Location))
  84.  
    {
  85.  
    isMouseDown = true;
  86.  
    yLocation = e.Y;
  87.  
    }
  88.  
    }
  89.  
    protected override void OnMouseMove(MouseEventArgs e)
  90.  
    {
  91.  
    base.OnMouseMove(e);
  92.  
    if (isMouseDown)
  93.  
    {
  94.  
    int mY = (e.Y - yLocation);
  95.  
    if (mY rectSlider.Height rectSlider.Y > this.Height)
  96.  
    {
  97.  
    movePix = this.Height - rectSlider.Height - rectSlider.Y;
  98.  
     
  99.  
    }
  100.  
    else if (mY rectSlider.Y < -1)
  101.  
    {
  102.  
    movePix = - rectSlider.Y;
  103.  
    }
  104.  
    else
  105.  
    if (mY != 0)
  106.  
    {
  107.  
    yLocation = e.Y;
  108.  
    movePix = mY;
  109.  
    }
  110.  
    PixToPos();
  111.  
    AutoCalcSize();
  112.  
    Scroll?.Invoke(this, new EventArgs());
  113.  
    }
  114.  
    }
  115.  
    protected override void OnMouseUp(MouseEventArgs e)
  116.  
    {
  117.  
    base.OnMouseUp(e);
  118.  
    isMouseDown = false;
  119.  
     
  120.  
    }
  121.  
     
  122.  
    protected override void OnClientSizeChanged(EventArgs e)
  123.  
    {
  124.  
    base.OnClientSizeChanged(e);
  125.  
    AutoCalcSize();
  126.  
    }
  127.  
    private void AutoCalcSize()
  128.  
    {
  129.  
    double sliderL = this.Height / maxPos * this.Height;
  130.  
    double pix = (this.Height ) / (maxPos - this.Height);
  131.  
    if (double.IsNaN( sliderL )|| sliderL == 0)
  132.  
    return;
  133.  
     
  134.  
     
  135.  
    float y = movePix rectSlider.Y;
  136.  
    int h = Convert.ToInt32(sliderL);
  137.  
    rectSlider.X = -0.5f;
  138.  
    rectSlider.Y = y;
  139.  
    rectSlider.Width = this.Width ;
  140.  
    rectSlider.Height = h;
  141.  
    avePos = pix;
  142.  
    this.Invalidate();
  143.  
    movePix = 0;
  144.  
    }
  145.  
     
  146.  
    private void SetMovePix()
  147.  
    {
  148.  
    double sliderL = this.Height / maxPos * this.Height;
  149.  
    double pix = (this.Height- sliderL) *pos/ (maxPos - this.Height);
  150.  
    rectSlider.Y = float.Parse( pix.ToString("0.00"));
  151.  
    movePix = 0;
  152.  
    }
  153.  
    private void PixToPos()
  154.  
    {
  155.  
    double sliderL = this.Height / maxPos * this.Height;
  156.  
    double pix = (maxPos - this.Height)/(this.Height - sliderL) *rectSlider.Y;
  157.  
    pos = Convert.ToInt32(Math.Ceiling(pix));
  158.  
    }
  159.  
    private void DrawSlider(Graphics g)
  160.  
    {
  161.  
    GraphicsPath path;
  162.  
    if (rectSlider.Width == 0 || rectSlider.Height == 0)
  163.  
    {
  164.  
    return;
  165.  
    }
  166.  
    if (rectSlider.Width > rectSlider.Height)
  167.  
    {
  168.  
    //水平
  169.  
    path = DrawHelper.GetHorizontal(this.rectSlider);
  170.  
    }
  171.  
    else
  172.  
    {
  173.  
    //垂直
  174.  
    path = DrawHelper.GetVertical(this.rectSlider);
  175.  
    }
  176.  
     
  177.  
    using (SolidBrush sb = new SolidBrush(SliderColor))
  178.  
    {
  179.  
    g.FillPath(sb, path);
  180.  
    }
  181.  
    path.Dispose();
  182.  
     
  183.  
    }
  184.  
     
  185.  
    }
学新通

未完成待续

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhigjgeg
系列文章
更多 icon
同类精品
更多 icon
继续加载