本系列為圖解教程,所以照著圖做就可以了。
步驟一(新建項目,如圖):
文件名設(shè)置如圖:
步驟二(添加所需控件):
把窗體Form1的"text"屬性修改為“我的mp3播放器“,然后再在左邊的”工具箱“里雙擊”MainMenu"添加一個菜單。如圖
接著在窗體最下面會看到如,表示已把菜單添加進去了:
緊接著給菜單添加選項,先點擊上圖的那個圖標,再單擊窗體上的菜單,如圖設(shè)置
注:文件(&F)表示可以用快捷方式ALT+F打開
步驟三(添加播放組件):
右鍵點擊“工具箱”,再選“添加/移除項”,如圖
再彈出的對話框中選“COM”選項卡,再找到“Windows Media Player”雙擊它,等待一會即可把它添加到“工具箱”里。
接著再雙擊剛才添加的這個播放組件,就可以在窗體中看到添加了。
步驟四(添加事件):
單擊“文件”菜單,在彈出的子菜單中雙擊“打開”進入代碼視圖。
寫下以下代碼。
步驟五(運行結(jié)果):
按F5運行,最終結(jié)果如下
至此,一個屬于你自己的MP3播放器就完成了,慢慢享受自己的努力成果吧,你再試下播放其它格式的,如電影WMV等。嘿嘿
很多人對原來的標題似乎很敏感,既然如此,改掉就是了
本文于2005-05-16 19:51:39.762被jabbyzheng第5次修改。
完整代碼:
============
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MP3Player
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private AxMediaPlayer.AxMediaPlayer axMediaPlayer1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設(shè)計器生成的代碼
///
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
///
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.axMediaPlayer1 = new AxMediaPlayer.AxMediaPlayer();
((System.ComponentModel.ISupportInitialize)(this.axMediaPlayer1)).BeginInit();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3});
this.menuItem1.Text = "文件(&F)";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "打開(&O)...";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "退出(&E)";
//
// axMediaPlayer1
//
this.axMediaPlayer1.Location = new System.Drawing.Point(0, 0);
this.axMediaPlayer1.Name = "axMediaPlayer1";
this.axMediaPlayer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMediaPlayer1.OcxState")));
this.axMediaPlayer1.Size = new System.Drawing.Size(424, 280);
this.axMediaPlayer1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(424, 281);
this.Controls.Add(this.axMediaPlayer1);
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "我的MP3播放器";
((System.ComponentModel.ISupportInitialize)(this.axMediaPlayer1)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.Text = "我的MP3播放器";
OpenFileDialog open = new OpenFileDialog(); //創(chuàng)建一個打開對話框
open.AddExtension = true; //設(shè)置是否自動在文件中添加擴展名
open.CheckFileExists = true; //檢查文件
open.Filter = "MP3文件 (*.mp3)|*.mp3|所有文件 (*.*)|*.*"; //設(shè)置要打開的類型為mp3和任意文件
if(open.ShowDialog() == DialogResult.OK) //如果用戶點擊了“確定”
{
this.axMediaPlayer1.FileName = open.FileName; //開始播放
this.Text += " -- 當前播放:" + this.axMediaPlayer1.FileName;
}
}
}
}