国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#自定義音樂播放器進(jìn)度條

2020-01-24 00:34:23
字體:
供稿:網(wǎng)友

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

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

之前想手繪進(jìn)度條線條的,結(jié)果控件運(yùn)行時會閃爍,所以直接用了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("單擊時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;              //記錄這個點(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)是不透明 且 是最后一個點(diǎn)           {            g.AddRectangle(new Rectangle(start, Y, X - start + 1, 1));   //添加之前的矩形到             start = -1;          }          p += 3;                  //下一個內(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);      }    }  }

用到的素材:

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

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 龙岩市| 寿光市| 五莲县| 新源县| 福鼎市| 伊金霍洛旗| 宜章县| 台山市| 南雄市| 天等县| 桦川县| 安庆市| 九龙县| 友谊县| 广丰县| 松江区| 河津市| 昌宁县| 永泰县| 台前县| 平安县| 双城市| 开远市| 连城县| 舟曲县| 通化市| 新郑市| 英超| 桐庐县| 洱源县| 高陵县| 榆林市| 安丘市| 嘉兴市| 乌拉特前旗| 井陉县| 赣榆县| 康平县| 高州市| 韶关市| 中牟县|