如何使用C#自定义音乐播放器进度条的实例分析


本文摘自PHP中文网,作者黄舟,侵删。

这篇文章主要为大家详细介绍了C#自定义音乐播放器进度条效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

有些时候我们做的程序需要进度条,而vs提供的控件不是我们想要的。先看效果图:

进度条闪烁动画,当然背景可设为Transparent

之前想手绘进度条线条的,结果控件运行时会闪烁,所以直接用了panel控件

源码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

[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);

      }

    }

  }

用到的素材:

阅读剩余部分

相关阅读 >>

c# 一些面试试题的实例教程

c#中关于表达式树的简单介绍

c#中sql参数传入空值出错误和如何解决办法

关于.net(c#)正确读取中文编码文件的实例教程

分享一些平时收藏和应用的开源代码

c#中关于minutes与totalminutes的区别

.net下如何使用quartz.net的代码图文教程

c#中强制转换与尝试转换的实现方法

关于c#如何实现的udp收发请求工具类的示例代码分析

c#中匿名对象与var以及动态类型 dynamic的详解

更多相关阅读请进入《csharp》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...