一:1.打开vs2019,选择新建项目,选择windows窗体应用模板(NET Framework)
2.选中工具箱,添加组件,右键常规---选择项---选中COM组件,选择Windows Media Player
背景音乐:
private void btnPlay_Click(object sender, EventArgs e)
{
string s = @"D:\music\f1.mp3";
axWindowsMediaPlayer1.URL = s;
}
private void btnStop_Click(object sender, EventArgs e)
{
//用于暂停正在播放的背景音乐
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
3.初始界面:
4.添加缺少的确认发送的控件:
5.连接服务器:
代码展示:
private NetworkStream stream;
private TcpClient tcpClient = new TcpClient();
public Form1()
{
InitializeComponent();
try
{
//向指定的IP地址的服务器发出连接请求
tcpClient.Connect("10.1.230.74", 3900);
listBox1.Items.Add("连接成功!");
stream = tcpClient.GetStream();
byte[] data = new byte[1024];
//判断网络流是否可读
if (stream.CanRead)
{
int len = stream.Read(data, 0, data.Length);
//Encoding ToEncoding = Encoding.GetEncoding("UTF-8");
//Encoding FromEncoding = Encoding.GetEncoding("GB2312");
//data=Encoding.Convert(FromEncoding, ToEncoding, data);
//string msg = Encoding.UTF8.GetString(data, 0, data.Length);
string msg = Encoding.Default.GetString(data, 0, data.Length);
string str = "\r\n";
char[] str1 = str.ToCharArray();
string[] msg1 = msg.Split(str1);
for (int j = 0; j < msg1.Length; j++)
{
listBox1.Items.Add(msg1[j]);
}
}
}
catch
{
listBox1.Items.Add("服务器未启动!");
}
}
6.发送数据:
//判断连接是否断开
if (tcpClient.Connected)
{
//向服务器发送数据
string msg = textBox1.Text;
Byte[] outbytes = System.Text.Encoding.Default.GetBytes(msg + "\n");
stream.Write(outbytes, 0, outbytes.Length);
byte[] data = new byte[1024];
//接收服务器回复数据
if (stream.CanRead)
{
int len = stream.Read(data, 0, data.Length);
string msg1 = Encoding.Default.GetString(data, 0, data.Length);
string str = "\r\n";
char[] str1 = str.ToCharArray();
string[] msg2 = msg1.Split(str1);
for (int j = 0; j < msg2.Length; j++)
{
listBox1.Items.Add(msg2[j]);
}
}
textBox1.Clear();
}
else
{
listBox1.Items.Add("连接已断开");
}
7.背景图片的切换:(每30s切换一次)
//图片
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 30000;
int i = 0;
timer.Elapsed += delegate
{
i++;
Image image = Image.FromFile(i + ".jpg");
pictureBox1.Image = image;
if (i == 3) i = 0;
};
timer.Start();