Home > Backend Development > C#.Net Tutorial > Example analysis of how to use C# to customize the progress bar of the music player

Example analysis of how to use C# to customize the progress bar of the music player

黄舟
Release: 2017-07-20 15:46:07
Original
2832 people have browsed it

This article mainly introduces the C# custom music player progress bar effect in detail, which has certain reference value. Interested friends can refer to it

Sometimes the programs we make need Progress bar, and the control provided by vs is not what we want. Let’s look at the renderings first:

The progress bar flashes animation, of course the background can be set to Transparent

If you wanted to hand-draw the progress bar lines before, the result will be when the control is running Flashing, so I used the panel control directly

Source code:


[DefaultEvent("ProgressClick")]
  [ToolboxBitmap(typeof(TrackBar))]
  public partial class ProcessBar : UserControl
  {
    public ProcessBar()
    {
      //InitializeComponent();
      //this.SetStyle(ControlStyles.UserPaint, true);
      //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
      //this.SetStyle(ControlStyles.DoubleBuffer, true);
    }

    private int locationX=0;
    [Description("单击时X的坐标")]
    public int LocationX
    {
      get { return locationX; }
    }
  
    private int current = 0;
    [Description("当前进度")]
    public int Current
    {
      get { return current; }
      set
      {
        if (value > 232 || value < 0)
          return;
        current = value;
        panelCurrent.Size = new Size(value, 1);
        picture.Location = new Point(value - 4, -3);
        Invalidate();
      }
    }

    private bool isPlay = false;
    [Description("是否播放")]
    public bool IsPlay
    {
      get { return isPlay; }
      set { isPlay = value; tmrCurrent.Enabled = isPlay; Invalidate(); }
    }

    public delegate void MouseHandle(object sender,EventArgs e);
    [Description("点下鼠标")]
    public event MouseHandle BarMouseDown;

    int picturetype = 0;
    private void tmrCurrent_Tick(object sender, EventArgs e)
    {
      if (picturetype == 0)
      { picture.Image = Properties.Resources.play_slider_thumb; picturetype = 1; }
      else
      { picture.Image = Properties.Resources.play_slider_thumb_animate; picturetype = 0; }
      GraphicsPath g = subGraphicsPath(picture.Image);
      if (g == null) return;
      picture.Region = new Region(g);
    }

    private unsafe static GraphicsPath subGraphicsPath(Image img)
    {
      if (img == null) return null;
      // 建立GraphicsPath, 给我们的位图路径计算使用  
      GraphicsPath g = new GraphicsPath(FillMode.Alternate);
      Bitmap bitmap = new Bitmap(img);
      int width = bitmap.Width;
      int height = bitmap.Height;
      BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
      byte* p = (byte*)bmData.Scan0;
      int offset = bmData.Stride - width * 3;
      int p0, p1, p2;     // 记录左上角0,0座标的颜色值 
      p0 = p[0];
      p1 = p[1];
      p2 = p[2];

      int start = -1;
      // 行座标 ( Y col )  
      for (int Y = 0; Y < height; Y++)
      {
        // 列座标 ( X row )  
        for (int X = 0; X < width; X++)
        {
          if (start == -1 && (p[0] != p0 || p[1] != p1 || p[2] != p2))   //如果 之前的点没有不透明 且 不透明  
          {
            start = X;              //记录这个点 
          }
          else if (start > -1 && (p[0] == p0 && p[1] == p1 && p[2] == p2))   //如果 之前的点是不透明 且 透明 
          {
            g.AddRectangle(new Rectangle(start, Y, X - start, 1));  //添加之前的矩形到 
            start = -1;
          }
          if (X == width - 1 && start > -1)    //如果 之前的点是不透明 且 是最后一个点 
          {
            g.AddRectangle(new Rectangle(start, Y, X - start + 1, 1));   //添加之前的矩形到 
            start = -1;
          }
          p += 3;                  //下一个内存地址 
        }
        p += offset;
      } bitmap.UnlockBits(bmData);
      bitmap.Dispose();
      // 返回计算出来的不透明图片路径  
      return g;
    }

    private void panelTotal_MouseDown(object sender, MouseEventArgs e)
    {
      Current = e.Location.X;
      locationX = e.Location.X;
      if (BarMouseDown != null)
      {
        BarMouseDown.Invoke(sender, e);
      }
    }

    private void panelCurrent_MouseDown(object sender, MouseEventArgs e)
    {
      Current = e.Location.X;
      locationX = e.Location.X;
      if (BarMouseDown != null)
      {
        BarMouseDown.Invoke(sender, e);
      }
    }
  }
Copy after login

Materials used:

Right-click and save as picture. The reason why the black background is used is because the picture is white and cannot be seen. Needless to say.

Tips: The unsafe keyword is used here, and the project properties need to be set ----- allow unsafe code to be run. Students who have not set it should not think that the program is wrong

The above is the detailed content of Example analysis of how to use C# to customize the progress bar of the music player. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template