添加控件
在工具箱右击选择工具箱选项卡
如图所示勾选con组件——工具箱才有Windows Media Player控件
右击解决方案——添加——新建项目——类库 BlockLibarary
再右击类库——添加——新建项目——类 Block.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace BlockLibarary
- {
- public class Block
- {
- public string BlockTime { get; set; }
- public string BlockBgSound { get; set; }
- public virtual bool IsPlay()
- {
- if (BlockTime==DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"))
- {
- return true;
- }
- return false;
- }
- }
- }
控制台应用程序WindowsBlock——添加——新建项——windows窗体 Form1.cs
添加引用—— BlockLibarary 类库
Form1.cs后台代码
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using BlockLibarary;
- namespace WindowsBlock
- {
- public partial class Form1 : Form
- {
- Block myblock = new Block();
- public Form1()
- {
- InitializeComponent();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- DialogResult result= openFileDialog1.ShowDialog();
- if (result.ToString().ToLower()=="ok")
- {
- this.txtBgSound.Text = openFileDialog1.FileName;
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- myblock.BlockTime = Convert.ToDateTime(this.txtTime.Text).ToString("yyyy-MM-dd hh:mm:ss");
- myblock.BlockBgSound = this.txtBgSound.Text;
- MessageBox.Show("设定成功");
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (myblock.IsPlay()==true)
- {
- axWindowsMediaPlayer1.URL = myblock.BlockBgSound;
- axWindowsMediaPlayer1.Ctlcontrols.play();
- }
- }
- }
- }