1:首先要将要引用的dll文件引用到项目中,
2:在项目重建一个类;
3:在类中声明个dll文件中类的实例;
4:新建个类作为dll中类子类;
5:在子类中对方法进行重写;
6:如果dll类中的方法不是虚方法这时override就要改为new即对父类的隐藏
以下是一个简单的闹钟实例:
重写dll类:
namespace 闹钟2
{
//要重写dll中的类就要继承
class 覆盖dll:BlockLibarary.Block
{
public override bool IsPlay()
{
DateTime sheiding =Convert.ToDateTime( this.BlockTime);//设定时间
DateTime now = DateTime.Now;
TimeSpan xiang = now.Subtract(sheiding);
if (xiang.Seconds == 10)
{
return true;
}
else { return false; }
}
}
}
调用:
//重写dll方法闹钟延迟十秒执行
覆盖dll dd = new 覆盖dll();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)//浏览选背景乐
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName;
}
}
private void button2_Click(object sender, EventArgs e)//设定
{
dd.BlockTime = Convert.ToDateTime(textBox1.Text).ToString("yyyy-MM-dd hh:mm:ss");
dd.BlockBgSound = textBox2.Text;
MessageBox.Show("设定成功");
}
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)//timmer控件(至于timmer控件的开启可以放到设定按钮的事件中)
{
if (dd.IsPlay() == true)
{
axWindowsMediaPlayer1.URL = dd.BlockBgSound;
axWindowsMediaPlayer1.Ctlcontrols.play();
}
else { }
}