C#如何實(shí)現(xiàn)自定義音樂播放器進(jìn)度條

這篇文章將為大家詳細(xì)講解有關(guān)C#如何實(shí)現(xiàn)自定義音樂播放器進(jìn)度條,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

從網(wǎng)站建設(shè)到定制行業(yè)解決方案,為提供網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)服務(wù)體系,各種行業(yè)企業(yè)客戶提供網(wǎng)站建設(shè)解決方案,助力業(yè)務(wù)快速發(fā)展。成都創(chuàng)新互聯(lián)公司將不斷加快創(chuàng)新步伐,提供優(yōu)質(zhì)的建站服務(wù)。

有些時(shí)候我們做的程序需要進(jìn)度條,而vs提供的控件不是我們想要的。先看效果圖:

C#如何實(shí)現(xiàn)自定義音樂播放器進(jìn)度條

進(jìn)度條閃爍動(dòng)畫,當(dāng)然背景可設(shè)為Transparent

之前想手繪進(jìn)度條線條的,結(jié)果控件運(yùn)行時(shí)會(huì)閃爍,所以直接用了panel控件

源碼:

[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("單擊時(shí)X的坐標(biāo)")]
    public int LocationX
    {
      get { return locationX; }
    }
  
    private int current = 0;
    [Description("當(dāng)前進(jìn)度")]
    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("點(diǎn)下鼠標(biāo)")]
    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, 給我們的位圖路徑計(jì)算使用  
      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座標(biāo)的顏色值 
      p0 = p[0];
      p1 = p[1];
      p2 = p[2];

      int start = -1;
      // 行座標(biāo) ( Y col )  
      for (int Y = 0; Y < height; Y++)
      {
        // 列座標(biāo) ( X row )  
        for (int X = 0; X < width; X++)
        {
          if (start == -1 && (p[0] != p0 || p[1] != p1 || p[2] != p2))   //如果 之前的點(diǎn)沒有不透明 且 不透明  
          {
            start = X;              //記錄這個(gè)點(diǎn) 
          }
          else if (start > -1 && (p[0] == p0 && p[1] == p1 && p[2] == p2))   //如果 之前的點(diǎn)是不透明 且 透明 
          {
            g.AddRectangle(new Rectangle(start, Y, X - start, 1));  //添加之前的矩形到 
            start = -1;
          }
          if (X == width - 1 && start > -1)    //如果 之前的點(diǎn)是不透明 且 是最后一個(gè)點(diǎn) 
          {
            g.AddRectangle(new Rectangle(start, Y, X - start + 1, 1));   //添加之前的矩形到 
            start = -1;
          }
          p += 3;                  //下一個(gè)內(nèi)存地址 
        }
        p += offset;
      } bitmap.UnlockBits(bmData);
      bitmap.Dispose();
      // 返回計(jì)算出來的不透明圖片路徑  
      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);
      }
    }
  }

用到的素材:

C#如何實(shí)現(xiàn)自定義音樂播放器進(jìn)度條C#如何實(shí)現(xiàn)自定義音樂播放器進(jìn)度條

直接右鍵另存為圖片,之所以用黑色背景是因?yàn)閳D片是白色的看不見,不用多說了。

提示:這里用到了unsafe關(guān)鍵字,需要設(shè)置項(xiàng)目的屬性-----允許運(yùn)行不安全的代碼,沒有設(shè)置的同學(xué)不要以為程序錯(cuò)了

關(guān)于“C#如何實(shí)現(xiàn)自定義音樂播放器進(jìn)度條”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

分享文章:C#如何實(shí)現(xiàn)自定義音樂播放器進(jìn)度條
轉(zhuǎn)載來于:http://muchs.cn/article42/iephhc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站制作、虛擬主機(jī)、網(wǎng)站建設(shè)、網(wǎng)站改版、營(yíng)銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司