Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 18k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8. namespace ch5_2
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private AxMediaPlayer.AxMediaPlayer axMediaPlayer1;
  16. private System.Windows.Forms.Button button1;
  17. private System.Windows.Forms.TextBox textBox1;
  18. private System.Windows.Forms.Button button2;
  19. /// <summary>
  20. /// 必需的设计器变量。
  21. /// </summary>
  22. private System.ComponentModel.Container components = null;
  23. private System.Windows.Forms.ListView listView1;
  24. private System.Windows.Forms.ColumnHeader columnHeader1;
  25. private System.Windows.Forms.ColumnHeader columnHeader2;
  26. private System.Windows.Forms.ColumnHeader columnHeader3;
  27. private System.Windows.Forms.Button button3;
  28. private System.Windows.Forms.Button button5;
  29. private System.Windows.Forms.Button button6;
  30. private System.Windows.Forms.Button button7;
  31. private System.Windows.Forms.ContextMenu contextMenu1;
  32. private System.Windows.Forms.MenuItem menuItem1;
  33. private System.Windows.Forms.MenuItem menuItem2;
  34. private System.Windows.Forms.MenuItem menuItem3;
  35. private System.Windows.Forms.MenuItem menuItem4;
  36. private System.Windows.Forms.ColumnHeader columnHeader4;
  37. private System.Windows.Forms.Button button8;
  38. private System.Windows.Forms.Button button9;
  39. private System.Windows.Forms.Button button4;
  40. private System.Windows.Forms.Button button10;
  41. //代表收藏夹的文件路径
  42. private string filePath;
  43. public Form1()
  44. {
  45. //
  46. // Windows 窗体设计器支持所必需的
  47. //
  48. InitializeComponent();
  49. //获取歌曲收藏夹文件路径
  50. string current =Directory.GetCurrentDirectory();
  51. filePath=current+"\mymp3.lst";
  52.             //在歌曲列表中显示收藏文件中的歌曲
  53. LoadSongs();
  54. //
  55. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  56. //
  57. }
  58. /// <summary>
  59. /// 清理所有正在使用的资源。
  60. /// </summary>
  61. protected override void Dispose( bool disposing )
  62. {
  63. if( disposing )
  64. {
  65. if (components != null) 
  66. {
  67. components.Dispose();
  68. }
  69. }
  70. base.Dispose( disposing );
  71. }
  72. public  void LoadSongs()
  73. {
  74.     try
  75. {
  76. //从文件中读取歌曲列表
  77. StreamReader fReader=new StreamReader(filePath,System.Text.Encoding.Default);
  78. //读出一首歌曲信息
  79. string s=fReader.ReadLine();
  80. while (s!=null)
  81. {
  82.     //获得歌曲信息
  83.      this.GetSongInfo(s);
  84. s=fReader.ReadLine();
  85. }
  86.                 //关闭文件流
  87.                 fReader.Close();
  88. }
  89. //如果文件不存在就创建一个文件
  90. catch (FileNotFoundException error)
  91. {
  92.                 File.Create(filePath);
  93. }
  94. }
  95. private void GetSongInfo(string s)
  96. {
  97.     this.axMediaPlayer1.FileName=s;
  98. //获得歌曲名称和路径
  99. string mp3Name=this.axMediaPlayer1.GetMediaInfoString(MediaPlayer.MPMediaInfoType.mpClipTitle);
  100. string mp3Path=s;
  101. //获取文件大小
  102. FileInfo fInfo=new FileInfo(s);
  103. float size= (float)fInfo.Length/(1024*1024);
  104. //获取作者信息
  105. string author=this.axMediaPlayer1.GetMediaInfoString(MediaPlayer.MPMediaInfoType.mpClipAuthor);
  106.    
  107. //构造子项
  108. string[] subItem={mp3Name,size.ToString().Substring(0,4)+"M",author,s};
  109. //构造项
  110. ListViewItem item=new ListViewItem(subItem);
  111. //插入项
  112. this.listView1.Items.Add(item);
  113. }
  114. #region Windows Form Designer generated code
  115. /// <summary>
  116. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  117. /// 此方法的内容。
  118. /// </summary>
  119. private void InitializeComponent()
  120. {
  121. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  122. this.axMediaPlayer1 = new AxMediaPlayer.AxMediaPlayer();
  123. this.button1 = new System.Windows.Forms.Button();
  124. this.textBox1 = new System.Windows.Forms.TextBox();
  125. this.button2 = new System.Windows.Forms.Button();
  126. this.listView1 = new System.Windows.Forms.ListView();
  127. this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
  128. this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
  129. this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
  130. this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
  131. this.contextMenu1 = new System.Windows.Forms.ContextMenu();
  132. this.menuItem1 = new System.Windows.Forms.MenuItem();
  133. this.menuItem2 = new System.Windows.Forms.MenuItem();
  134. this.menuItem3 = new System.Windows.Forms.MenuItem();
  135. this.menuItem4 = new System.Windows.Forms.MenuItem();
  136. this.button3 = new System.Windows.Forms.Button();
  137. this.button5 = new System.Windows.Forms.Button();
  138. this.button6 = new System.Windows.Forms.Button();
  139. this.button7 = new System.Windows.Forms.Button();
  140. this.button8 = new System.Windows.Forms.Button();
  141. this.button9 = new System.Windows.Forms.Button();
  142. this.button4 = new System.Windows.Forms.Button();
  143. this.button10 = new System.Windows.Forms.Button();
  144. ((System.ComponentModel.ISupportInitialize)(this.axMediaPlayer1)).BeginInit();
  145. this.SuspendLayout();
  146. // 
  147. // axMediaPlayer1
  148. // 
  149. this.axMediaPlayer1.Location = new System.Drawing.Point(0, 336);
  150. this.axMediaPlayer1.Name = "axMediaPlayer1";
  151. this.axMediaPlayer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMediaPlayer1.OcxState")));
  152. this.axMediaPlayer1.Size = new System.Drawing.Size(320, 24);
  153. this.axMediaPlayer1.TabIndex = 0;
  154. // 
  155. // button1
  156. // 
  157. this.button1.Location = new System.Drawing.Point(16, 8);
  158. this.button1.Name = "button1";
  159. this.button1.TabIndex = 1;
  160. this.button1.Text = "打开";
  161. this.button1.Click += new System.EventHandler(this.button1_Click);
  162. // 
  163. // textBox1
  164. // 
  165. this.textBox1.Location = new System.Drawing.Point(104, 8);
  166. this.textBox1.Name = "textBox1";
  167. this.textBox1.Size = new System.Drawing.Size(120, 21);
  168. this.textBox1.TabIndex = 2;
  169. this.textBox1.Text = "";
  170. // 
  171. // button2
  172. // 
  173. this.button2.Location = new System.Drawing.Point(240, 8);
  174. this.button2.Name = "button2";
  175. this.button2.TabIndex = 4;
  176. this.button2.Text = "加入收藏夹";
  177. this.button2.Click += new System.EventHandler(this.button2_Click);
  178. // 
  179. // listView1
  180. // 
  181. this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
  182. this.columnHeader1,
  183. this.columnHeader2,
  184. this.columnHeader4,
  185. this.columnHeader3});
  186. this.listView1.ContextMenu = this.contextMenu1;
  187. this.listView1.FullRowSelect = true;
  188. this.listView1.HideSelection = false;
  189. this.listView1.Location = new System.Drawing.Point(8, 80);
  190. this.listView1.MultiSelect = false;
  191. this.listView1.Name = "listView1";
  192. this.listView1.Size = new System.Drawing.Size(304, 200);
  193. this.listView1.TabIndex = 5;
  194. this.listView1.View = System.Windows.Forms.View.Details;
  195. this.listView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listView1_MouseDown);
  196. // 
  197. // columnHeader1
  198. // 
  199. this.columnHeader1.Text = "曲名";
  200. // 
  201. // columnHeader2
  202. // 
  203. this.columnHeader2.Text = "大小";
  204. // 
  205. // columnHeader4
  206. // 
  207. this.columnHeader4.Text = "作者";
  208. // 
  209. // columnHeader3
  210. // 
  211. this.columnHeader3.Text = "路径";
  212. // 
  213. // contextMenu1
  214. // 
  215. this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  216.  this.menuItem1,
  217.  this.menuItem2,
  218.  this.menuItem3,
  219.  this.menuItem4});
  220. // 
  221. // menuItem1
  222. // 
  223. this.menuItem1.Index = 0;
  224. this.menuItem1.Text = "删除当前";
  225. this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
  226. // 
  227. // menuItem2
  228. // 
  229. this.menuItem2.Index = 1;
  230. this.menuItem2.Text = "删除全部";
  231. this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
  232. // 
  233. // menuItem3
  234. // 
  235. this.menuItem3.Index = 2;
  236. this.menuItem3.Text = "-";
  237. // 
  238. // menuItem4
  239. // 
  240. this.menuItem4.Index = 3;
  241. this.menuItem4.Text = "选定曲目";
  242. this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
  243. // 
  244. // button3
  245. // 
  246. this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  247. this.button3.Location = new System.Drawing.Point(72, 48);
  248. this.button3.Name = "button3";
  249. this.button3.Size = new System.Drawing.Size(64, 24);
  250. this.button3.TabIndex = 6;
  251. this.button3.Text = "播放";
  252. this.button3.Click += new System.EventHandler(this.button3_Click);
  253. // 
  254. // button5
  255. // 
  256. this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  257. this.button5.Location = new System.Drawing.Point(136, 48);
  258. this.button5.Name = "button5";
  259. this.button5.Size = new System.Drawing.Size(64, 24);
  260. this.button5.TabIndex = 8;
  261. this.button5.Text = "停止";
  262. this.button5.Click += new System.EventHandler(this.button5_Click);
  263. // 
  264. // button6
  265. // 
  266. this.button6.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  267. this.button6.Location = new System.Drawing.Point(200, 48);
  268. this.button6.Name = "button6";
  269. this.button6.Size = new System.Drawing.Size(56, 24);
  270. this.button6.TabIndex = 9;
  271. this.button6.Text = "前一首";
  272. this.button6.Click += new System.EventHandler(this.button6_Click);
  273. // 
  274. // button7
  275. // 
  276. this.button7.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  277. this.button7.Location = new System.Drawing.Point(256, 48);
  278. this.button7.Name = "button7";
  279. this.button7.Size = new System.Drawing.Size(56, 24);
  280. this.button7.TabIndex = 10;
  281. this.button7.Text = "后一首";
  282. this.button7.Click += new System.EventHandler(this.button7_Click);
  283. // 
  284. // button8
  285. // 
  286. this.button8.Location = new System.Drawing.Point(104, 296);
  287. this.button8.Name = "button8";
  288. this.button8.Size = new System.Drawing.Size(104, 23);
  289. this.button8.TabIndex = 12;
  290. this.button8.Text = "保存歌曲列表";
  291. this.button8.Click += new System.EventHandler(this.button8_Click);
  292. // 
  293. // button9
  294. // 
  295. this.button9.Location = new System.Drawing.Point(208, 296);
  296. this.button9.Name = "button9";
  297. this.button9.Size = new System.Drawing.Size(104, 23);
  298. this.button9.TabIndex = 13;
  299. this.button9.Text = "载入歌曲列表";
  300. this.button9.Click += new System.EventHandler(this.button9_Click);
  301. // 
  302. // button4
  303. // 
  304. this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  305. this.button4.Location = new System.Drawing.Point(8, 48);
  306. this.button4.Name = "button4";
  307. this.button4.Size = new System.Drawing.Size(64, 24);
  308. this.button4.TabIndex = 14;
  309. this.button4.Text = "我的收藏";
  310. this.button4.Click += new System.EventHandler(this.button4_Click);
  311. // 
  312. // button10
  313. // 
  314. this.button10.Location = new System.Drawing.Point(8, 296);
  315. this.button10.Name = "button10";
  316. this.button10.Size = new System.Drawing.Size(96, 23);
  317. this.button10.TabIndex = 15;
  318. this.button10.Text = "编辑收藏夹";
  319. this.button10.Click += new System.EventHandler(this.button10_Click);
  320. // 
  321. // Form1
  322. // 
  323. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  324. this.ClientSize = new System.Drawing.Size(320, 365);
  325. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  326.   this.button10,
  327.   this.button4,
  328.   this.button9,
  329.   this.button8,
  330.   this.button7,
  331.   this.button6,
  332.   this.button5,
  333.   this.button3,
  334.   this.button2,
  335.   this.textBox1,
  336.   this.button1,
  337.   this.axMediaPlayer1,
  338.   this.listView1});
  339. this.Name = "Form1";
  340. this.Text = "mp3 播放器";
  341. ((System.ComponentModel.ISupportInitialize)(this.axMediaPlayer1)).EndInit();
  342. this.ResumeLayout(false);
  343. }
  344. #endregion
  345. /// <summary>
  346. /// 应用程序的主入口点。
  347. /// </summary>
  348. [STAThread]
  349. static void Main() 
  350. {
  351. Application.Run(new Form1());
  352. }
  353. private void button1_Click(object sender, System.EventArgs e)
  354. {
  355.    OpenFileDialog ofDialog=new OpenFileDialog();
  356.             //设置过滤器
  357. ofDialog.Filter="mp3 文件(*.mp3)|*.mp3|所有文件(*.*)|*.*";
  358. if (ofDialog.ShowDialog()==DialogResult.OK)
  359. {
  360. try
  361. {
  362. //获得播放文件名
  363. string s=ofDialog.FileName;
  364. //设置播放器的文件名
  365. axMediaPlayer1.FileName=s;
  366. //播放音乐
  367. axMediaPlayer1.Play();
  368. this.textBox1.Text=s;
  369.  
  370. //获得歌曲信息
  371.     this.GetSongInfo(s);
  372. }
  373. catch
  374. {
  375. //出错警告
  376. MessageBox.Show("无法播放此文件","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
  377. }
  378. }
  379. }
  380. private void button2_Click(object sender, System.EventArgs e)
  381. {
  382. if (this.textBox1.Text!="")
  383. {
  384.     string   s=this.textBox1.Text;
  385. try
  386. {
  387. //创建一个写文件流
  388. StreamWriter fWriter=new StreamWriter(this.filePath,true,System.Text.Encoding.Default);
  389. //将歌曲路径写入文件
  390. fWriter.WriteLine(s);
  391. //关闭文件流
  392.                     fWriter.Close();
  393. }
  394. catch (DirectoryNotFoundException error)
  395. {
  396.          //如果文件不存在则创建一个新文件,然后将歌曲路径写入文件
  397. File.Create(this.filePath);
  398. StreamWriter fWriter=new StreamWriter(this.filePath,true,System.Text.Encoding.Default);
  399. fWriter.WriteLine(s);
  400. fWriter.Close();
  401. }
  402. }
  403. }
  404. private void listView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  405. {
  406. //如果有选中的歌曲就播放这首歌
  407. if (this.listView1.SelectedItems.Count!=0)
  408. {
  409. //获得歌曲的详细路径
  410. string fileName=this.listView1.SelectedItems[0].SubItems[3].Text;
  411. this.axMediaPlayer1.FileName=fileName;
  412. this.axMediaPlayer1.Play();
  413. }
  414. }
  415. private void button3_Click(object sender, System.EventArgs e)
  416. {
  417. //如果有选中的歌曲就播放这首歌
  418. if (this.listView1.SelectedItems.Count!=0)
  419. {
  420. //获得歌曲的详细路径
  421. this.axMediaPlayer1.FileName=this.listView1.SelectedItems[0].SubItems[3].Text;
  422. this.axMediaPlayer1.Play();
  423. }
  424. }
  425. private void button5_Click(object sender, System.EventArgs e)
  426. {
  427. //如果播放器正在放音乐,就停止播放
  428. if (this.axMediaPlayer1.FileName!="")
  429. {
  430. this.axMediaPlayer1.Stop();
  431. }
  432. }
  433. private void button6_Click(object sender, System.EventArgs e)
  434. {
  435. if (this.listView1.Items.Count!=0)
  436. {
  437. if (this.listView1.SelectedItems.Count==0)
  438. {
  439. }
  440. else 
  441. {
  442. if (this.listView1.SelectedItems[0].Index==0)
  443. {
  444. }
  445. else
  446. {
  447. //选定位置向前移动一格
  448. int pos=this.listView1.SelectedItems[0].Index-1;
  449. //当前选定的项取消
  450. this.listView1.SelectedItems[0].Selected=false;
  451. //设置新的选定项
  452. this.listView1.Items[pos].Selected=true;
  453.  
  454. //播放当前选定项
  455. string fileName=this.listView1.SelectedItems[0].SubItems[3].Text;
  456. this.axMediaPlayer1.FileName=fileName;
  457. this.axMediaPlayer1.Play();
  458. }
  459. }
  460. }
  461. }
  462. private void button7_Click(object sender, System.EventArgs e)
  463. {
  464. if (this.listView1.Items.Count!=0)
  465. {
  466. if (this.listView1.SelectedItems.Count==0)
  467. {
  468. }
  469. else
  470. {
  471. if (this.listView1.SelectedItems[0].Index+1==this.listView1.Items.Count)
  472. {
  473. }
  474. else 
  475. {
  476. //选定位置向前移动一格
  477. int pos=this.listView1.SelectedItems[0].Index+1;
  478. //当前选定的项取消
  479. this.listView1.SelectedItems[0].Selected=false;
  480. //设置新的选定项
  481. this.listView1.Items[pos].Selected=true;
  482.  
  483. //播放当前选定项
  484. string fileName=this.listView1.SelectedItems[0].SubItems[3].Text;
  485. this.axMediaPlayer1.FileName=fileName;
  486. this.axMediaPlayer1.Play();
  487.     
  488. }
  489. }
  490. }
  491. }
  492. private void menuItem1_Click(object sender, System.EventArgs e)
  493. {
  494. if (this.listView1.Items.Count!=0)
  495. {
  496. if (this.listView1.SelectedItems.Count!=0)
  497. {
  498. //移除当前曲目
  499. this.listView1.SelectedItems[0].Remove();
  500. }
  501. }
  502. }
  503. private void menuItem2_Click(object sender, System.EventArgs e)
  504. {
  505. //删除所有的曲目
  506. this.listView1.Items.Clear();
  507. }
  508. private void menuItem5_Click(object sender, System.EventArgs e)
  509. {
  510. }
  511. private void button8_Click(object sender, System.EventArgs e)
  512. {
  513. SaveFileDialog sDialog=new SaveFileDialog();
  514. //设置过滤器
  515. sDialog.Filter="歌曲列表文件(*.lst)|*.lst|所有文件(*.*)|*.*";
  516. if (sDialog.ShowDialog()==DialogResult.OK)
  517. {
  518. //得到保存文件名
  519. string fileName=sDialog.FileName;
  520. //创建写文件流
  521. StreamWriter sWriter=new StreamWriter(fileName,false,System.Text.Encoding.Default);
  522. //将列表中的歌曲逐一写入文件之中
  523. foreach (ListViewItem item in this.listView1.Items)
  524. {
  525.       sWriter.WriteLine(item.SubItems[3].Text);
  526. }
  527. //关闭文件流
  528. sWriter.Close();
  529.          }
  530. }
  531. private void button9_Click(object sender, System.EventArgs e)
  532. {
  533. OpenFileDialog oDialog=new OpenFileDialog();
  534. //设置过滤器
  535. oDialog.Filter="歌曲列表文件(*.lst)|*.lst|所有文件(*.*)|*.*";
  536. if (oDialog.ShowDialog()==DialogResult.OK)
  537. {
  538. //得到加载的文件名
  539. string fileName=oDialog.FileName;
  540. //创建读文件流
  541. StreamReader sReader=new StreamReader(fileName,System.Text.Encoding.Default);
  542. //清空列表
  543. this.listView1.Items.Clear();
  544. //逐一将文件中的歌曲信息写入列表中
  545. string s=sReader.ReadLine();
  546. while (s!=null)
  547. {
  548.  this.GetSongInfo(s);
  549.  s= sReader.ReadLine();
  550.  
  551. }
  552. //关闭文件流
  553. sReader.Close();
  554. }
  555. }
  556. private void button4_Click(object sender, System.EventArgs e)
  557. {
  558. //清空listView1
  559. this.listView1.Items.Clear();
  560. //载入收藏夹的歌曲列表
  561. this.LoadSongs();
  562. }
  563. private void button10_Click(object sender, System.EventArgs e)
  564. {
  565. Form2 form2=new Form2(this.filePath);
  566. //打开收藏夹编辑器
  567. form2.Show();
  568. }
  569. private void menuItem4_Click(object sender, System.EventArgs e)
  570. {
  571.       
  572. //使用button1_Click的事件处理程序
  573. this.button1_Click(this,e);
  574. }
  575. }
  576. }