winform窗体事件机制的理解:
1、 事件机制是要注册的,所以不能贸然的在后台代码加入一些没经过注册的代码
所以编程的时候如果不是又设计的界面双击进去的事件,是需要在这个文件加入代码的
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
鼠标对应的事件:new System.Windows.Forms.MouseEventHandler
this.button1.Click += new System.EventHandler(this.button1_Click);
按钮对应的事件:new System.EventHandler
进度条的实现:
在这个音乐播放器中是利用用户自定义控件+picturebox实现:
具体实现:
1、 用户自定义控件:
添加新项——用户自定义控件——名称UserControl1
在界面上添加三个picturebox
public partial class UserControl1 : UserControl
{
/// <summary>
/// 设置或获取当前播放进度条的宽度
/// </summary>
public int UCTime
{
get { return this.Width; }
set
{
if (this.Width >= 16)
{
this.Width = value;
}
else
{
this.Width = value + 16;
}
}
}
/// <summary>
/// 设置或获取当前播放进度条左边背景图片
/// </summary>
public Image LeftBackImage
{
get { return picBoxLeft.BackgroundImage; }
set { picBoxLeft.BackgroundImage = value; }
}
/// <summary>
///设置或获取当前播放进度条右边背景图片
/// </summary>
public Image RightBackImage
{
get { return picBoxRight.BackgroundImage; }
set { picBoxRight.BackgroundImage = value; }
}
/// <summary>
///设置或获取当前播放进度条中间背景图片
/// </summary>
public Image MiddleBackImage
{
get { return picBoxMiddle.BackgroundImage; }
set { picBoxMiddle.BackgroundImage = value; }
}
public UserControl1()
{
InitializeComponent();
picBoxLeft.Size = new Size(8, 10); //左边
picBoxRight.Size = new Size(8, 10); //右边
picBoxMiddle.Size = new Size(0, 10); //中间
picBoxLeft.Dock = DockStyle.Left;
picBoxRight.Dock = DockStyle.Right;
picBoxMiddle.Width = this.Width - 16;
picBoxMiddle.Location = new Point(8, 0);
this.BackColor = Color.Transparent;
picBoxLeft.BackColor = Color.Transparent;
picBoxRight.BackColor = Color.Transparent;
picBoxMiddle.BackColor = Color.Transparent;
picBoxLeft.BackgroundImage = LeftBackImage;
picBoxRight.BackgroundImage = RightBackImage;
picBoxMiddle.BackgroundImage = MiddleBackImage;
}
private void UserControl1_SizeChanged(object sender, EventArgs e)
{
this.Size = new Size(UCTime, 10);
picBoxMiddle.Width = this.Width - 16;
}
}
2、 设置音乐播放器界面;
添加一个picturebox,和用户自定义控件、timerTick
在后台添加代码:
private void timer1_Tick_1(object sender, EventArgs e)
{
lblcurrenttime.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString + " / " + axWindowsMediaPlayer1.currentMedia.durationString;
//currentPositionString当Ì¡À前¡ã播£¤放¤?时º¡À间?,ê?durationString目?前¡ã媒?体¬?总Á¨¹长¡è
UCcurrenttime = Convert.ToInt32(axWindowsMediaPlayer1.Ctlcontrols.currentPosition / axWindowsMediaPlayer1.currentMedia.duration*170);
if (UCcurrenttime <= 16)
{
UCcurrenttime = 16;
}
this.userControl11.Width = UCcurrenttime;
}
浙公网安备 33010602011771号