semye-静心

积累,点点滴滴
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WinForm程序实现滚动字幕与背景音乐

Posted on 2007-04-03 15:36  semye  阅读(3565)  评论(5编辑  收藏  举报
1.滚动字幕
用一个LABEL放在PANEL中,用一个TIMER来动态改变LABEL的LOCATION
 1            int xx = this.label1.Location.X;
 2            int yy = this.label1.Location.Y;
 3        private void timer1_Tick(object sender, System.EventArgs e)
 4        {
 5            if(this.label1.Location.Y+this.label1.Size.Height>0)
 6            {
 7                this.label1.Location = new System.Drawing.Point(this.label1.Location.X,this.label1.Location.Y-1);
 8            }

 9            else
10            {
11                this.label1.Location = new System.Drawing.Point(xx,yy);
12            }

13        }

14
2.背景音乐
1        private void StartPlayMusic()
2        {
3            Music.PlayMusic("\".\\music\\菊花台.mp3\"");
4        }

5
Music.cs
 1using   System;   
 2using   System.Runtime.InteropServices;   
 3    
 4namespace   TestWinProj 
 5{   
 6    internal   class   Music   
 7    {   
 8        [DllImport("winmm.dll")] 
 9        public static extern long PlaySound(String fileName,long a,long b);
10
11        [DllImport("winmm.dll")]
12        public static extern long mciSendString(string lpstrCommand,string lpstrReturnString,long length,long hwndcallback);
13        
14        /// <summary>
15        /// 播放音乐文件
16        /// </summary>
17        /// <param name="p_FileName">音乐文件名称</param>

18        public static void PlayMusic(string p_FileName)
19        {
20            try
21            {
22                mciSendString(@"close " +p_FileName ,"                                  ",0,0);
23                mciSendString(@"open " + p_FileName,"                                  ",0,0);
24                mciSendString(@"play " + p_FileName ,"                                  ",0,0);
25            }

26            catch
27            {
28            }

29        }

30        
31        /// <summary>
32        /// 停止当前音乐播放
33        /// </summary>
34        /// <param name="p_FileName">音乐文件名称</param>

35        public static void StopMusic(string p_FileName)
36        {
37            try
38            {
39                mciSendString(@"close " + p_FileName,"                                  ",0,0);
40            }

41            catch{}
42        }

43
44
45    }
   
46}
   
47
48