在一个GameObject上进行多个AudioSource的控制
- http://www.verydemo.com/demo_c161_i105215.html?qqdrsign=03f8c
- using UnityEngine;
- using System.Collections;
- public class SoundSwitch : MonoBehaviour {
- public AudioSource as1;
- public AudioSource as2;
- public AudioClip[] a1;
- // Use this for initialization
- void Start () {
- //代码关键点1(可选):增加两个AudioSource到GameObject
- this.gameObject.AddComponent("AudioSource");
- this.gameObject.AddComponent("AudioSource");
- //代码关键点2:GetComponents方法获得所有该GameObj上的AudioSource对象。这样就可以分别进行控制了。
- var as_array=this.gameObject.GetComponents(typeof(AudioSource));
- as1=(AudioSource)as_array[0];
- as2=(AudioSource)as_array[1];
- as1.clip=a1[1];
- as2.clip=a1[2];
- AudioPlay();
- }
- // Update is called once per frame
- void Update () {
- }
- void AudioPlay()
- {
- as2.Play();
- as1.Play();
- }
- }
另外
http://www.codefans.net/articles/1334.shtml
这是我收集的一个由C#编写的音乐播放器的主Form代码,觉得里面有一些小技巧还是不错的,特此发给大家。里面有播放器背景设置、线程定义、调用读取文件目录方法、播放时间计数器、设置LV背景、获取播放歌曲、播放按钮,切换播放or暂停、切换歌曲到下一首,调用切歌方法、显示播放列表、歌词局中、播放窗体最小化隐藏到托盘设置、进度条滚动模块、从歌曲列表中删除文件等。
001 using System; 002 using System.Collections.Generic; 003 using System.ComponentModel; 004 using System.Data; 005 using System.Drawing; 006 using System.Linq; 007 using System.Text; 008 using System.Windows.Forms; 009 using System.IO; 010 using System.Drawing.Imaging; 011 using System.Threading; 012 namespace MyMusicBox 013 { 014 public partial class MainForm : Form 015 { 016 public MainForm() 017 { 018 InitializeComponent(); 019 SetStyle(ControlStyles.UserPaint, true); 020 SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 021 SetStyle(ControlStyles.AllPaintingInWmPaint, true); 022 SetStyle(ControlStyles.DoubleBuffer, true); 023 024 } 025 Song mySong = null;//播放的歌曲对象 026 ListViewItem itemLrc;//打开歌词路径项 027 Bitmap bm ;//用于动态设置LV背景 028 public SavingInfo sa = null;//持久化信息类 029 Thread thread;//后台线程 030 031 #region 加载事件 032 private void MainForm_Load(object sender, EventArgs e) 033 { 034 //调用读取配置文件方法 035 Path.ReadPath(); 036 //窗体背景设置为配置文件中的图片 037 this.BackgroundImage = Image.FromFile(Path.bgPath); 038 //调用读取文件目录方法 039 Path.ReadSong(); 040 //读取播放列表 041 sa = new SavingInfo(); 042 sa.LoadPlayList(); 043 //绑定LV 044 BindAllLV(); 045 BindPlayLV(); 046 SetLV(); 047 SetLrcBG(); 048 timer1.Enabled = Path.Playing; 049 //显示时间 050 lblTime.Text = System.DateTime.Now.ToString(); 051 //两个lbl用来根据歌曲总长和播放长度显示进度,设置位置重叠 052 lblRuning.Location = lblLong.Location; 053 //启动线程 054 thread = new Thread(Renovate); 055 thread.IsBackground = true; 056 thread.Start(); 057 058 } 059 #endregion 060 #region 后台刷新 061 /// <summary> 062 /// 时间计数状态等刷新 063 /// </summary> 064 private void Renovate() 065 { 066 //while (true) 067 //{ 068 // //计数 069 // Count(); 070 // //获取当前时间 071 // lblTime.Text = System.DateTime.Now.ToString(); 072 // //无歌曲提示,如果播放列表为空且当前播放列表为显示状态 073 // if (lvPlay.Items.Count == 0 && lvPlay.Visible) 074 // lblTs.Visible = true; 075 // else 076 // lblTs.Visible = false; 077 // Thread.Sleep(1000); 078 //} 079 } 080 #endregion 081 #region 设置LV背景 082 /// <summary> 083 /// 设置LV背景 084 /// </summary> 085 public void SetLV() 086 { 087 bm = new Bitmap(this.BackgroundImage, this.Width, this.Height); 088 //绘制矩形,定义起始位置和宽高 089 Rectangle r = new Rectangle(lvPlay.Location.X, lvPlay.Location.Y, lvPlay.Width, lvSong.Height); 090 //按矩形尺寸和起始位置截取bm的一部分 091 bm= bm.Clone(r,bm.PixelFormat); 092 //把截取到的图片设置为lv背景,达到与主窗体背景完美契合的效果 093 lvSong.BeginUpdate(); 094 lvSong.BackgroundImage = (Image)bm; 095 lvSong.EndUpdate(); 096 lvPlay.BeginUpdate(); 097 lvPlay.BackgroundImage = (Image)bm; 098 lvPlay.EndUpdate(); 099 } 100 #endregion 101 102 #region 获取歌曲 103 /// <summary> 104 /// 获取播放歌曲 105 /// </summary> 106 private void GetSong() 107 { 108 //接收播放歌曲方法,获得一个歌曲对象 109 this.mySong = PlayList.Play(); 110 //如果歌曲对象不为空 111 if (mySong != null) 112 { 113 //播放器路径设置为该歌曲路径 114 wmp.URL = mySong.FileName; 115 //调用方法,显示歌曲名、歌手 116 lblName.Text = PlayList.GetName(); 117 lblSinger.Text = PlayList.GetSinger(); 118 mySong.SongState = SongPlayState.played;//修改播放状态 119 //播放按钮图片修改为暂停图片 120 btnPlay.Image = Image.FromFile("Images\\stop.png"); 121 try 122 { 123 124 //读取歌词路径 125 Path.ReadLrc(); 126 AddLrc(); 127 //启动一个定时器,此定时器只控制歌词轮播 128 timer2.Start(); 129 } 130 catch (Exception) 131 { 132 LrcNull(); 133 itemLrc = new ListViewItem(); 134 itemLrc.SubItems.Add("找不到该歌曲歌词文件!"); 135 itemLrc.ForeColor = Color.Blue; 136 itemLrc.Font = new Font("微软雅黑", 14.25F, ((FontStyle)((FontStyle.Bold | FontStyle.Underline))), GraphicsUnit.Point, ((byte)(134))); 137 lvLrc.Items.Add(itemLrc); 138 } 139 } 140 } 141 #endregion 142 #region 主定时器 143 //定时器1 144 private void timer1_Tick(object sender, EventArgs e) 145 { 146 if (Path.songPath == "") 147 { 148 timer1.Enabled = false; 149 return; 150 } 151 152 //如果当前无播放歌曲,调用获取歌曲的方法 153 if (this.mySong == null) 154 { 155 GetSong(); 156 } 157 //自动下一首,如果当前播放完毕 158 if (this.wmp.playState == WMPLib.WMPPlayState.wmppsStopped) 159 { 160 this.mySong = null; // 将歌曲设为空 161 Path.lrcPath = ""; 162 Path.dicLrc.Clear(); 163 PlayModel(); 164 PlayList.PlayNext(); 165 Thread.Sleep(1000); 166 } 167 // 切歌,当前有播放歌曲且播放状态为cut 168 if (this.mySong != null && this.mySong.SongState == SongPlayState.cut) 169 { 170 this.wmp.URL = "";//置空 171 timer2.Stop(); 172 Path.dicLrc.Clear(); 173 Path.lrcPath = ""; 174 this.mySong = null; 175 } 176 //如果当前有播放歌曲,需要获取的一些属性 177 if (wmp.URL!="") 178 { 179 //设置当前播放歌曲颜色 180 SongColor(); 181 //获取音量控件显示值,根据控制刻度数量计算 182 this.trackBar1.Value = wmp.settings.volume / 10; 183 //歌曲时间显示,一个是总长度,一个是已播放长度,字符串类型 184 lblAll.Text = wmp.currentMedia.durationString; 185 lblRun.Text = wmp.Ctlcontrols.currentPositionString; 186 //进度条,使用了两个lbl控件,歌曲长度/已播放长度=lbl1的宽/lbl2的宽 187 //乘1000为防止数据过小出现错误 188 double temp = (wmp.currentMedia.duration*1000) / (wmp.Ctlcontrols.currentPosition*1000); 189 double width = lblLong.Width; 190 double avg = width / temp; 191 //判断>1为了防止avg数值小于int界限 192 if(avg>1) 193 lblRuning.Width = Convert.ToInt32(avg); 194 picRun.Left = lblRuning.Right; 195 //托盘显示播放歌曲 196 this.notifyIcon1.Text =lblName.Text + lblSinger.Text; 197 } 198 } 199 #endregion 200 #region 播放按钮 201 /// <summary> 202 /// 播放按钮,切换播放or暂停 203 /// </summary> 204 private void btnPlay_Click(object sender, EventArgs e) 205 { 206 if(!timer1.Enabled) 207 timer1.Start(); 208 //如果当前为正在播放 209 if (wmp.playState == WMPLib.WMPPlayState.wmppsPlaying) 210 { 211 //暂停播放 212 wmp.Ctlcontrols.pause(); 213 //按钮图片修改为播放 214 btnPlay.Image = Image.FromFile("Images\\play.png"); 215 //停止控制歌词滚动的定时器 216 if (timer2.Enabled) 217 timer2.Stop(); 218 } 219 else if (wmp.playState == WMPLib.WMPPlayState.wmppsPaused) 220 { 221 //开始播放 222 wmp.Ctlcontrols.play(); 223 //按钮图片修改为暂停 224 btnPlay.Image = Image.FromFile("Images\\stop.png"); 225 //启动歌词滚动 226 if (!timer2.Enabled&&Path.dicLrc.Count>0) 227 timer2.Start(); 228 } 229 } 230 #endregion 231 #region 切歌 232 //切换下一首,调用切歌方法 233 private void btnRight_Click(object sender, EventArgs e) 234 { 235 PlayList.Cut(false); 236 } 237 //上一首,调用切歌方法 238 private void btnLeft_Click(object sender, EventArgs e) 239 { 240 PlayList.Cut(true); 241 } 242 #endregion 243 #region 音量 244 //静音,静音!=音量为0 245 private void btnModel_Click(object sender, EventArgs e) 246 { 247 //如果播放器非静音状态,前景文字显示红叉,状态改为静音 248 if (wmp.settings.mute == false) 249 { 250 btnSound.Text = "×"; 251 wmp.settings.mute = true; 252 } 253 else//反之,已是静音状态,清空红叉,设置非静音状态 254 { 255 btnSound.Text = ""; 256 wmp.settings.mute = false; 257 } 258 } 259 //设置音量,此事件为控制刻度值变化时 260 private void trackBar1_Scroll(object sender, EventArgs e) 261 { 262 //音量设置为控制刻度*10(因为初始化的时候是/10) 263 wmp.settings.volume = this.trackBar1.Value * 10; 264 } 265 #endregion 266 #region 歌曲列表操作 267 /// <summary> 268 /// 绑定所有歌曲LV数据 269 /// </summary> 270 private void BindAllLV() 271 { 272 //如果所有歌曲集合为空,结束 273 if (PlayList.song == null) 274 return; 275 lvSong.Items.Clear(); 276 //遍历集合绑定到所有歌曲LV 277 foreach (Song s in PlayList.song) 278 { 279 ListViewItem item = new ListViewItem(s.Name); 280 item.Tag = s; 281 item.SubItems.Add(s.Singer); 282 lvSong.Items.Add(item); 283 } 284 } 285 //点击显示所有歌曲 286 private void tslblAll_Click(object sender, EventArgs e) 287 { 288 //隐藏播放列表 289 lvPlay.Visible = false; 290 //两个LV位置重叠 291 lvSong.Location = lvPlay.Location; 292 //点击后改变背景色 293 tslblAll.BackColor = Color.Silver; 294 tslblPlay.BackColor = Color.Transparent; 295 //显示所有歌曲LV 296 lvSong.Visible = true; 297 } 298 //点击显示播放列表 299 private void tslblPlay_Click(object sender, EventArgs e) 300 { 301 //调用绑定播放LV数据 302 BindPlayLV(); 303 //背景色改变 304 tslblPlay.BackColor = Color.Silver; 305 tslblAll.BackColor = Color.Transparent; 306 //隐藏所有歌曲LV 307 lvSong.Visible = false; 308 //显示播放LV 309 lvPlay.Visible = true; 310 } 311 /// <summary> 312 /// 绑定播放列表 313 /// </summary> 314 private void BindPlayLV() 315 { 316 //如果播放集合不为空 317 if (PlayList.play!=null) 318 { 319 lvPlay.Items.Clear(); 320 //遍历集合绑定播放LV 321 foreach (Song s in PlayList.play) 322 { 323 int id = lvPlay.Items.Count + 1; 324 s.Id = id; 325 ListViewItem item = new ListViewItem(id.ToString()); 326 item.SubItems.Add(s.Name); 327 item.Tag = s; 328 lvPlay.Items.Add(item); 329 } 330 } 331 } 332 /// <summary> 333 /// 歌曲计数 334 /// </summary> 335 private void Count() 336 { 337 //if (lvSong.Visible) 338 // lblCount.Text = "已选" + lvSong.CheckedItems.Count + "\\总" + lvSong.Items.Count; 339 //if (lvPlay.Visible) 340 // lblCount.Text = "总" + lvPlay.Items.Count; 341 } 342 #endregion 343 #region 歌词操作 344 /// <summary> 345 /// 设置歌词背景 346 /// </summary> 347 public void SetLrcBG() 348 { 349 bm = new Bitmap(this.BackgroundImage, this.Width, this.Height); 350 //绘制矩形,定义起始位置和宽高 351 Rectangle r = new Rectangle(lvLrc.Location.X, lvLrc.Location.Y, lvLrc.Width, lvLrc.Height); 352 //按矩形尺寸和起始位置截取bm的一部分 353 bm = bm.Clone(r, PixelFormat.Undefined); 354 //把截取到的图片设置为lv背景,达到与主窗体背景完美契合的效果 355 lvLrc.BeginUpdate(); 356 lvLrc.BackgroundImage = (Image)bm; 357 lvLrc.EndUpdate(); 358 } 359 int nullCount;//记录空行位置 360 /// <summary> 361 /// 设置空行,使歌词局中 362 /// </summary> 363 private void LrcNull() 364 { 365 lvLrc.Items.Clear(); 366 nullCount = (int)Math.Ceiling(Convert.ToDouble(lvLrc.Height / 30 / 2)); 367 for (int i = 0; i <= nullCount; i++) 368 { 369 lvLrc.Items.Add(""); 370 } 371 } 372 /// <summary> 373 /// 添加歌词 374 /// </summary> 375 private void AddLrc() 376 { 377 LrcNull(); 378 foreach (string key in Path.dicLrc.Keys) 379 { 380 ListViewItem item = new ListViewItem(); 381 item.SubItems.Add(Path.dicLrc[key]); 382 item.Tag = key; 383 lvLrc.Items.Add(item); 384 } 385 } 386 //只控制歌词轮播 387 private void timer2_Tick(object sender, EventArgs e) 388 { 389 //调用显示歌词的方法 390 ShowLrc(); 391 } 392 /// <summary> 393 /// 显示歌词 394 /// </summary> 395 private void ShowLrc() 396 { 397 foreach (ListViewItem item in lvLrc.Items) 398 { 399 if (item.Tag == null) 400 continue; 401 if (item.Tag.ToString() == lblRun.Text ) 402 { 403 item.ForeColor = Color.DarkOrange; 404 item.Font = new Font("华文琥珀", 16F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); 405 lvLrc.Items[item.Index -1].ForeColor = Color.Silver; 406 lvLrc.Items[item.Index - 1].Font = new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); 407 if (item.Index + 1<lvLrc.Items.Count) 408 lvLrc.Items[item.Index+1].EnsureVisible(); 409 } 410 } 411 } 412 #endregion 413 #region 窗体隐藏到托盘设置 414 //最小化到托盘 415 private void btnMix_Click(object sender, EventArgs e) 416 { 417 this.Hide(); 418 } 419 //主窗体大小状态变化时 420 private void MainForm_SizeChanged(object sender, EventArgs e) 421 { 422 if (this.WindowState == FormWindowState.Minimized) 423 { 424 //启动一个控件,该控制可以让程序在右下角显示图标,需要提前设置该控制显示的图标图片 425 this.notifyIcon1.Visible = true; 426 } 427 } 428 //托盘显示,双击显示窗体 429 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) 430 { 431 ShowMain(); 432 } 433 /// <summary> 434 /// 显示主窗体 435 /// </summary> 436 public void ShowMain() 437 { 438 if (!this.Visible) 439 { 440 this.TopMost = true; 441 this.Show(); 442 this.TopMost = false; 443 } 444 else 445 { 446 this.TopMost = true; 447 this.TopMost = false; 448 } 449 } 450 //托盘打开窗体 451 private void tsmiShow_Click(object sender, EventArgs e) 452 { 453 ShowMain(); 454 } 455 //托盘退出 456 private void tsmiExit_Click(object sender, EventArgs e) 457 { 458 Application.Exit(); 459 } 460 #endregion 461 #region 窗体位移 462 Point mouseOff;//鼠标移动位置变量 463 bool leftFlag;//标记是否为左键 464 private void pnlMain_MouseDown(object sender, MouseEventArgs e) 465 { 466 if (e.Button == MouseButtons.Left) 467 { 468 mouseOff = new Point(-e.X, -e.Y); //得到变量的值 469 leftFlag = true; //点击左键按下时标注为true; 470 } 471 } 472 private void pnlMain_MouseMove(object sender, MouseEventArgs e) 473 { 474 if (leftFlag) 475 { 476 Point mouseSet = Control.MousePosition; 477 mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置 478 Location = mouseSet; 479 } 480 } 481 private void pnlMain_MouseUp(object sender, MouseEventArgs e) 482 { 483 if (leftFlag) 484 { 485 leftFlag = false;//释放鼠标后标注为false; 486 } 487 } 488 #endregion 489 #region 进度条滚动块移动 490 int runX;//记录滚动块初始位置 491 private void picRun_MouseDown(object sender, MouseEventArgs e) 492 { 493 runX = e.X; 494 } 495 //释放鼠标位移,进度前进 496 private void picRun_MouseUp(object sender, MouseEventArgs e) 497 { 498 picRun.Left += e.X - runX - picRun.Width / 2; 499 if (picRun.Left > lblLong.Right) 500 picRun.Left = lblLong.Right; 501 if(picRun.Left<lblLong.Left) 502 picRun.Left = lblLong.Left; 503 if (mySong != null) 504 { 505 lblRuning.Width = picRun.Right- lblRuning.Left; 506 double temp = (lblLong.Width*1000.0 )/ (lblRuning.Width*1000.0); 507 wmp.Ctlcontrols.currentPosition = wmp.currentMedia.duration / temp; 508 } 509 } 510 #endregion 511 #region 歌曲增删操作 512 //添加歌曲到播放列表 513 private void tsmiAdd_Click(object sender, EventArgs e) 514 { 515 if (lvSong.CheckedItems.Count > 0) 516 { 517 foreach (ListViewItem item in lvSong.CheckedItems) 518 { 519 // 遍历播放集合,如果存在不添加 520 foreach (Song s in PlayList.play) 521 { 522 if (s.Name == item.Text) 523 { 524 PlayList.play.Remove(s); 525 break; 526 } 527 } 528 PlayList.play.Add(item.Tag as Song); 529 } 530 } 531 } 532 //所有歌曲:删除选中歌曲 533 private void tsmiDel_Click(object sender, EventArgs e) 534 { 535 foreach (ListViewItem item in lvSong.Items) 536 { 537 if (item.Checked) 538 { 539 PlayList.song.Remove(item.Tag as Song); 540 } 541 } 542 BindAllLV(); 543 } 544 //全选歌曲 545 private void tsmiCheck_Click(object sender, EventArgs e) 546 { 547 foreach (ListViewItem item in lvSong.Items) 548 { 549 if (tsmiCheck.Text == "全部选中") 550 { 551 item.Checked = true; 552 if(item.Index+1==lvSong.Items.Count) 553 tsmiCheck.Text = "取消选中"; 554 } 555 else 556 { 557 item.Checked = false; 558 if (item.Index + 1 == lvSong.Items.Count) 559 tsmiCheck.Text = "全部选中"; 560 } 561 } 562 } 563 //选中项的同时选中复选框 564 private void lvSong_MouseDown(object sender, MouseEventArgs e) 565 { 566 if (lvSong.SelectedItems.Count > 0 && e.Button == MouseButtons.Left) 567 { 568 lvSong.SelectedItems[0].Checked = true; 569 } 570 } 571 //播放列表删除事件 572 private void tsmiPlayDel_Click(object sender, EventArgs e) 573 { 574 foreach (ListViewItem item in lvPlay.Items) 575 { 576 if (item.Selected) 577 { 578 //PlayList.id = (lvPlay.Items[item.Index+1].Tag as Song).Id; 579 //this.song = null; 580 PlayList.play.Remove(item.Tag as Song); 581 } 582 } 583 BindPlayLV(); 584 } 585 //清空播放列表 586 private void tsmiPlayClear_Click(object sender, EventArgs e) 587 { 588 PlayList.play.Clear(); 589 this.mySong = null; 590 BindPlayLV(); 591 } 592 //从播放列表删除文件 593 private void tsmiPlayDelFile_Click(object sender, EventArgs e) 594 { 595 if (lvPlay.SelectedItems.Count > 0) 596 { 597 DelFile(lvPlay.SelectedItems[0]); 598 } 599 } 600 /// <summary> 601 /// 删除某项对应文件 602 /// </summary> 603 /// <param name="item"></param> 604 private void DelFile(ListViewItem item) 605 { 606 try 607 { 608 string path = (item.Tag as Song).FileName; 609 File.Delete(path); 610 BindAllLV(); 611 BindPlayLV(); 612 } 613 catch (Exception) 614 { 615 MessageBox.Show("该文件为只读文件或用户权限不够,无法删除!"); 616 } 617 } 618 //从所有歌曲中删除文件 619 private void tsmiDelFile_Click(object sender, EventArgs e) 620 { 621 if (lvSong.SelectedItems.Count > 0) 622 { 623 DelFile(lvSong.SelectedItems[0]); 624 } 625 } 626 //双击列表项播放歌曲 627 private void lvPlay_DoubleClick(object sender, EventArgs e) 628 { 629 if (!timer1.Enabled) 630 { 631 PlayList.id = (lvPlay.SelectedItems[0].Tag as Song).Id; 632 timer1.Start(); 633 } 634 if (lvPlay.SelectedItems.Count > 0) 635 { 636 PlayList.id = (lvPlay.SelectedItems[0].Tag as Song).Id; 637 this.mySong = null; 638 } 639 } 640 #endregion 641 #region 设置 642 //跳转设置窗体 643 private void btnSet_Click(object sender, EventArgs e) 644 { 645 SetForm set = new SetForm(); 646 set.parent = this;//把主窗体传过去 647 set.Show(this); 648 } 649 #endregion 650 #region 回滚 651 /// <summary> 652 /// 路径重置后 653 /// </summary> 654 public void Back() 655 { 656 BindAllLV(); 657 PlayList.Update(); 658 timer1.Start(); 659 } 660 #endregion 661 #region 退出 662 //关闭时保存列表 663 private void MainForm_FormClosing(object sender, FormClosingEventArgs e) 664 { 665 if (Path.SaveList) 666 sa.SavePlayList(); 667 } 668 //面板退出 669 private void btnExit_Click(object sender, EventArgs e) 670 { 671 if (Path.Exit) 672 { 673 Application.Exit(); 674 } 675 else 676 { 677 this.Hide(); 678 } 679 } 680 681 #endregion 682 #region 背景颜色等变换控制 683 private void btnLeft_MouseEnter(object sender, EventArgs e) 684 { 685 ((Button)sender).BackgroundImage = Image.FromFile("Images//allbtn_highlight.png"); 686 } 687 private void btnLeft_MouseLeave(object sender, EventArgs e) 688 { 689 ((Button)sender).BackgroundImage = null; 690 } 691 private void tslblPlay_MouseEnter(object sender, EventArgs e) 692 { 693 ((ToolStripLabel)sender).BackgroundImage = Image.FromFile("Images//Album_bkg_wubian.png"); 694 } 695 private void tslblPlay_MouseLeave(object sender, EventArgs e) 696 { 697 ((ToolStripLabel)sender).BackgroundImage = null; 698 } 699 //选中改变颜色 700 private void lvSong_ItemChecked(object sender, ItemCheckedEventArgs e) 701 { 702 foreach (ListViewItem item in lvSong.Items) 703 { 704 if (item.Checked) 705 { 706 item.ForeColor = Color.DarkOrange; 707 } 708 else 709 { 710 item.ForeColor = Color.DimGray; 711 } 712 } 713 } 714 /// <summary> 715 /// 当前播放歌曲改变颜色 716 /// </summary> 717 private void SongColor() 718 { 719 foreach (ListViewItem item in lvPlay.Items) 720 { 721 if (item.SubItems[1].Text == lblName.Text) 722 { 723 item.ForeColor = Color.DeepSkyBlue; 724 } 725 else 726 { 727 item.ForeColor = Color.White; 728 } 729 } 730 } 731 #endregion 732 #region 选择歌词 733 private void lvLrc_MouseDown(object sender, MouseEventArgs e) 734 { 735 if (lvLrc.SelectedItems.Count>0&&lvLrc.SelectedItems[0] == itemLrc) 736 { 737 if (Path.OpenLrc()) 738 { 739 AddLrc(); 740 //启动一个定时器,此定时器只控制歌词轮播 741 timer2.Start(); 742 } 743 } 744 } 745 #endregion 746 #region 窗体最大化设置 747 Size now;//原始尺寸 748 Point pN;//原始位置 749 //最大化 750 private void btnMax_Click(object sender, EventArgs e) 751 { 752 Size max = new Size(Screen.PrimaryScreen.WorkingArea.Width - 10, Screen.PrimaryScreen.WorkingArea.Height - 20); 753 if (this.Size != max) 754 { 755 pN = this.Location; 756 now= this.Size; 757 this.Size = max; 758 this.Left = 5; 759 this.Top = 5; 760 btnMax.Image = Image.FromFile("Images//max1.png") 761 ; 762 } 763 else 764 { 765 this.Location = pN; 766 this.Size = now; 767 btnMax.Image = Image.FromFile("Images//max.png"); 768 } 769 SetLV(); 770 SetLrcBG(); 771 } 772 #endregion 773 #region 歌词、MV切换 774 private void tsmiLrc_Click(object sender, EventArgs e) 775 { 776 lvLrc.Visible = true; 777 wmp.Visible = false; 778 tsmiLrc.BackColor = Color.LawnGreen; 779 tsmiMv.BackColor = Color.Transparent; 780 } 781 private void tsmiMv_Click(object sender, EventArgs e) 782 { 783 wmp.Location = lvLrc.Location; 784 wmp.Size = lvLrc.Size; 785 lvLrc.Visible = false; 786 wmp.Visible = true; 787 tsmiLrc.BackColor = Color.Transparent; 788 tsmiMv.BackColor = Color.LawnGreen; 789 } 790 #endregion 791 #region 播放模式切换 792 /// <summary> 793 /// 播放模式 794 /// </summary> 795 private void PlayModel() 796 { 797 if (tsmiOne.Checked) 798 { 799 PlayList.PlayOne(); 800 } 801 else if (tsmiSJ.Checked) 802 { 803 PlayList.PlayRandom(); 804 } 805 } 806 private void tsmiXH_Click(object sender, EventArgs e) 807 { 808 ToolStripMenuItem ts=((ToolStripMenuItem)sender); 809 foreach(ToolStripMenuItem item in tsmiModel.DropDownItems) 810 { 811 if (ts.Checked && ts == item) 812 { 813 continue; 814 } 815 else 816 { 817 item.Checked = false; 818 } 819 } 820 } 821 #endregion 822 823 } 824 }