• 用mciSendString做音乐播放器


    音乐操作类

      1 public class clsMCI
      2     {
      3         public clsMCI()
      4         {
      5             //
      6             // TODO: 在此处添加构造函数逻辑
      7             //
      8         }
      9 
     10         //定义API函数使用的字符串变量 
     11         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
     12         private string Name = "";
     13         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
     14         private string durLength = "";
     15         [MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
     16         private string TemStr = "";
     17         int ilong;
     18         //定义播放状态枚举变量
     19         public enum State
     20         {
     21             mPlaying = 1,
     22             mPuase = 2,
     23             mStop = 3
     24         };
     25         //结构变量
     26         public struct structMCI
     27         {
     28             public bool bMut;
     29             public int iDur;
     30             public int iPos;
     31             public int iVol;
     32             public int iBal;
     33             public string iName;
     34             public State state;
     35         };
     36 
     37         public structMCI mc = new structMCI();
     38 
     39         //取得播放文件属性
     40         public string FileName
     41         {
     42             get
     43             {
     44                 return mc.iName;
     45             }
     46 
     47             set
     48             {
     49                 //ASCIIEncoding asc = new ASCIIEncoding(); 
     50                 try
     51                 {
     52                     TemStr = "";
     53                     TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
     54                     Name = Name.PadLeft(260, Convert.ToChar(" "));
     55                     mc.iName = value;
     56                     ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
     57                     Name = GetCurrPath(Name);
     58                     //Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
     59                     Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
     60                     ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
     61                     ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0);
     62                     ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0);
     63                     mc.state = State.mStop;
     64                 }
     65                 catch
     66                 {
     67                     MessageBox.Show("出错错误!");
     68                 }
     69             }
     70         }
     71 
     72         //播放
     73         public void play()
     74         {
     75             TemStr = "";
     76             TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
     77             APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
     78             mc.state = State.mPlaying;
     79         }
     80         //停止
     81         public void StopT()
     82         {
     83             TemStr = "";
     84             TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
     85             ilong = APIClass.mciSendString("close media", TemStr, 128, 0);
     86             ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
     87             mc.state = State.mStop;
     88 
     89         }
     90 
     91         public void Puase()
     92         {
     93             TemStr = "";
     94             TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
     95             ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
     96             mc.state = State.mPuase;
     97         }
     98         private string GetCurrPath(string name)
     99         {
    100             if (name.Length < 1) return "";
    101             name = name.Trim();
    102             name = name.Substring(0, name.Length - 1);
    103             return name;
    104         }
    105         //总时间
    106         public int Duration
    107         {
    108             get
    109             {
    110                 durLength = "";
    111                 durLength = durLength.PadLeft(128, Convert.ToChar(" "));
    112                 APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
    113                 durLength = durLength.Trim();
    114                 if (durLength == "") return 0;
    115                 return (int)(Convert.ToDouble(durLength) / 1000f);
    116             }
    117         }
    118 
    119         //当前时间
    120         public int CurrentPosition
    121         {
    122             get
    123             {
    124                 durLength = "";
    125                 durLength = durLength.PadLeft(128, Convert.ToChar(" "));
    126                 APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
    127                 durLength = durLength.Trim();
    128                 if (durLength == "") return 0;
    129                 mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
    130                 return mc.iPos;
    131             }
    132             set
    133             {
    134                 string Command = String.Format("Seek Media to {0}", value * 1000);
    135                 APIClass.mciSendString(Command, null, 0, 0);//使播放停止  
    136                 mc.state = State.mStop;
    137                 mc.iPos = value * 1000;
    138                 play(); 
    139             }
    140         }
    141 
    142 
    143 
    144     }
    145 
    146     public class APIClass
    147     {
    148 
    149         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    150         public static extern int GetShortPathName(
    151         string lpszLongPath,
    152         string shortFile,
    153         int cchBuffer
    154         );
    155 
    156         [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
    157         public static extern int mciSendString(
    158         string lpstrCommand,
    159         string lpstrReturnString,
    160         int uReturnLength,
    161         int hwndCallback
    162         );
    163 
    164     }
    clsMCI

     进度条控件

    public class MyProgressBar : Control
        {
            public MyProgressBar()
            {
                ///在构造函数中设置控件Style,ControlStyles枚举可以参考
                ///http://technet.microsoft.com/zh-cn/subscriptions/system.windows.forms.controlstyles.aspx
                this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
    
                //通过三个鼠标事件函数,让鼠标可以拖动控制条
                this.MouseDown += MyProgressBar_MouseDown;
                this.MouseMove += MyProgressBar_MouseMove;
                this.MouseUp += MyProgressBar_MouseUp;
    
                this.BackColor = Color.Transparent;
            }
    
            #region 定义变量,并对一些变量进行一些默认设置
            //已加载长度(灰色矩形)
            Rectangle foreRect;
            Color foregroundColor = Color.Gray;
            //总长度(底部白色矩形)
            Rectangle backRect;
            Color backgroundColor = Color.White;
            //控制块(黑色矩形)
            Rectangle setRect;
            Color setRectColor = Color.Black;
    
            //最大值
            int maximum = 100;
            //最小值
            int minimum = 0;
            //当前值
            double myValue = 0;
            //是否显示百分比
            bool showPercent;
    
            //进度条百分比的颜色 
            Color fontColor = Color.Black;
            //进度条百分比的字体大小
            float fontSize = 9;
            //进度条百分比的字体
            FontFamily myFontFamily = new FontFamily("宋体");
            #endregion
    
            #region 设置属性值
            [Category("进度条"), Description("显示百分比"), Browsable(true)]
            public bool ShowPercentTag
            {
                get { return showPercent; }
                set
                {
                    showPercent = value;
                    Invalidate();
                }
            }
    
            [Category("进度条"), Description("进度条的最大值"), Browsable(true)]
            public int Maximum
            {
                get { return maximum; }
                set
                {
                    maximum = value;
                    Invalidate();
                }
            }
    
            [Category("进度条"), Description("进度条的最小值"), Browsable(true)]
            public int Minimum
            {
                get { return minimum; }
                set
                {
                    minimum = value;
                    Invalidate();
                }
            }
    
            [Category("进度条"), Description("进度条的背景色,总长度(底部白色矩形)"), Browsable(true)]
            public Color BackgroundColor
            {
                get { return backgroundColor; }
                set
                {
                    this.backgroundColor = value;
                    Invalidate();
                }
            }
    
            [Category("进度条"), Description("进度条的前景色,已加载长度(灰色矩形)"), Browsable(true)]
            public Color ForegroundColor
            {
                get { return foregroundColor; }
                set
                {
                    this.foregroundColor = value;
                    Invalidate();
                }
            }
    
            [Category("进度条"), Description("进度条控制块的颜色(黑色矩形)"), Browsable(true)]
            public Color SetRectColor
            {
                get { return setRectColor; }
                set
                {
                    this.setRectColor = value;
                    Invalidate();
                }
            }
    
            [Category("进度条"), Description("进度条的百分比字体大小"), Browsable(true)]
            public float FontSize
            {
                get { return fontSize; }
                set
                {
                    this.fontSize = value;
                    Invalidate();
                }
            }
    
            [Category("进度条"), Description("进度条的百分比字体"), Browsable(true)]
            public FontFamily MyFontFamily
            {
                get { return myFontFamily; }
                set
                {
                    this.myFontFamily = value;
                    Invalidate();
                }
            }
    
            [Category("进度条"), Description("进度条的百分比字体颜色"), Browsable(true)]
            public Color FontColor
            {
                get { return fontColor; }
                set
                {
                    this.fontColor = value;
                    Invalidate();
                }
            }
            #endregion
    
            #region 确定控件的位置
            [Category("General"), Description("控件的宽度"), Browsable(true)]
            public new int Width
            {
                get { return base.Width; }
                set
                {
                    base.Width = value;
                    foreRect.X = backRect.X = base.Width / 20;
                    backRect.Width = base.Width * 9 / 10;
                    foreRect.Width = (int)(myValue / maximum * backRect.Width);
                    setRect.X = (int)(foreRect.X + foreRect.Width - setRect.Width / 2);
    
                    Invalidate();
                }
            }
    
            [Category("General"), Description("控件的高度"), Browsable(true)]
            public new int Height
            {
                get { return base.Height; }
                set
                {
                    base.Height = value;
                    setRect.Height = setRect.Width = base.Height * 2 / 3;
                    foreRect.Height = backRect.Height = base.Height / 3;
                    setRect.Y = base.Height / 6;
                    foreRect.Y = backRect.Y = base.Height / 3;
                    Invalidate();
                }
            }
            #endregion
    
            #region value值改变事件
    
            protected EventHandler OnValueChanged;
            //当外部为该事件添加了响应函数时,事件就会生效,否则为OnValueChanged的值为null
            public event EventHandler ValueChanged
            {
                add
                {
                    if (OnValueChanged != null)
                    {
                        foreach (Delegate d in OnValueChanged.GetInvocationList())
                        {
                            if (object.ReferenceEquals(d, value)) { return; }
                        }
                    }
                    OnValueChanged = (EventHandler)Delegate.Combine(OnValueChanged, value);
                }
                remove
                {
                    OnValueChanged = (EventHandler)Delegate.Remove(OnValueChanged, value);
                }
            }
            #endregion
    
            #region 定义Value属性
    
            //当Value值改变的时候,重新设置矩形的进度,控制块的位置,并且重绘控件
            //Value属性内如果对进度条的值进行修改,使用myValue变量,而在其他地方,则用Value属性
            [Category("进度条"), Description("进度条的值"), Browsable(true)]
            public double Value
            {
                get { return myValue; }
                set
                {
                    if (myValue < Minimum)
                        throw new ArgumentException("小于最小值");
                    if (myValue > Maximum)
                        throw new ArgumentException("超过最大值");
    
                    myValue = value;
                    foreRect.Width = (int)(myValue / maximum * backRect.Width);
    
                    if ((myValue - maximum) > 0)
                    {
                        foreRect.Width = backRect.Width;
                    }
    
                    setRect.X = (int)(foreRect.X + foreRect.Width - setRect.Width / 2);
    
                    //如果添加了响应函数,则执行该函数
                    if (OnValueChanged != null)
                    {
                        OnValueChanged(this, EventArgs.Empty);
                    }
    
                    Invalidate();
                }
            }
            #endregion
    
            #region 绘制控件
            //重载OnPaint方法对控件进行绘制
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                DrawRect(e.Graphics);
                if (ShowPercentTag)
                {
                    DrawText(e.Graphics);
                }
            }
    
            //画进度条
            private void DrawRect(Graphics e)
            {
                Pen pen = new Pen(this.foregroundColor);
    
                e.FillRectangle(new SolidBrush(this.backgroundColor), backRect);
                e.DrawRectangle(new Pen(Color.Black), backRect);
    
                e.FillRectangle(new SolidBrush(this.foregroundColor), foreRect);
                e.DrawRectangle(new Pen(Color.Black), foreRect);
    
                e.FillEllipse(new SolidBrush(this.setRectColor), setRect.X, setRect.Y, setRect.Width, setRect.Height);
                e.DrawEllipse(new Pen(Color.Black), setRect.X, setRect.Y, setRect.Width, setRect.Height);
                //e.FillRectangle(new SolidBrush(this.setRectColor), setRect);
                //e.DrawRectangle(new Pen(Color.Black), setRect);
            }
    
            //画进度值
            private void DrawText(Graphics e)
            {
                Point point = new Point();
                point.X = this.backRect.X + this.backRect.Width * 3 / 7;
                point.Y = this.backRect.Y + this.backRect.Height / 3;
    
                SolidBrush brush = new SolidBrush(fontColor);
                Font font = new Font(myFontFamily, this.fontSize);
                string percent = ((int)this.myValue).ToString() + "%";
    
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
    
                e.DrawString(percent, font, brush, backRect, format);
            }
            #endregion
    
            #region 修改控件的大小
            //拖动边缘的箭头改变控件的大小时,控件也要做相应的改变时,就可以重载该方法,如果没有重载,就只有在修改完成后才更新控件
            protected override void OnResize(EventArgs e)
            {
                base.OnResize(e);
                this.Width = Width;
                this.Height = Height;
                Invalidate();
            }
            #endregion
    
            #region 拖动控制条
            //添加三个辅助变量,添加响应函数
            //当前鼠标位置
            Point originPoint;
            //当前控制块位置
            Point originsetRectPoint;
            //控制块是否点下
            bool setRectDown = false;
    
            //放开鼠标
            void MyProgressBar_MouseUp(object sender, MouseEventArgs e)
            {
                setRectDown = false;
            }
    
            //移动鼠标
            void MyProgressBar_MouseMove(object sender, MouseEventArgs e)
            {
                if (setRectDown)
                {
                    //移动长度
                    int dd = e.Location.X - originPoint.X;
                    //百分比
                    double percent = (double)(originsetRectPoint.X + dd - this.backRect.X + setRect.Width / 2) / this.backRect.Width;
                    if (percent < 0)
                    {
                        percent = 0;
                    }
                    else if (percent > 1)
                    {
    
                        percent = 1;
                    }
                    this.Value = percent * maximum;
                    this.foreRect.Width = (int)(percent * this.backRect.Width);
                    //this.setRect.X = originsetRectPoint.X + dd;
                    this.setRect.X = (int)(foreRect.X + foreRect.Width - setRect.Width / 2);
                    Invalidate();
                }
            }
    
            //点下鼠标
            void MyProgressBar_MouseDown(object sender, MouseEventArgs e)
            {
                if (setRect.Contains(e.Location))
                {
                    this.originPoint = e.Location;
                    originsetRectPoint = this.setRect.Location;
                    this.setRectDown = true;
                }
            }
            #endregion
        }
    MyProgressBar

    操作

    1 clsMCI mp = new clsMCI();
    2 mp.FileName = path;//路径
    3 mp.Duration//总时间
    4 mp.CurrentPosition//当前时间

    进度条控制时间

    nowtime = mySlider1.Value / mySlider1.Maximum * alltime;
    mp.CurrentPosition = (int)nowtime;

    音乐播放进度

    nowtime = Convert.ToDouble(mp.CurrentPosition);
    mySlider1.Value = nowtime / alltime * mySlider1.Maximum;

     实现界面

  • 相关阅读:
    PCA与LDA
    SVM--交叉验证
    git的基本使用
    MySQL的操作
    MySQL安装和远程连接
    javaScript进阶
    javaScript基础入门篇
    javaScript运动
    可变对象和不可变对象
    基本数据类型
  • 原文地址:https://www.cnblogs.com/margin-gu/p/5260977.html
Copyright © 2020-2023  润新知