table of Contents
1. The overall framework
Create a new C# Windows Form application and add the control layout as follows:
Special note:
Many control properties need to be based on the actual situation of the child
1. Add axWindowsMediaPlaye control method:
Feel free to right-click a control in the toolbox, click "Select item", select the com component, Check WIndows Media player, click OK
2. Timer
set this value to 2000, that is, once every 2s, and Enabled is set to Ture
When dragging this control, pay attention to drag this control where there is no control, that is, set this control based on the entire window
2. Specific functions
1. Connect to the server
Double-click on the design interface to enter the game button, enter the function interface, enter the following code in the click function:
try
{
//实例化
tcpClient = new TcpClient();
//向指定的IP地址的服务器发出连接请求
tcpClient.Connect("10.160.52.106", 3900);
//获取网络传输流
stream = tcpClient.GetStream();
//接受数据并转化为字符串
byte[] data = new byte[1024];
int receive = stream.Read(data, 0, 1024);
string msg = Encoding.Default.GetString(data, 0, receive);
//去除字符串中的终端转义字符
msg = strDelete(msg);
//显示出来
textBox1.AppendText(msg);
}
catch
{
textBox1.AppendText("服务器未启动" + Environment.NewLine);
}
Effect picture:
and add in the Form1.cs file public partial class Form1: Form:
TcpClient tcpClient;
NetworkStream stream;
And create a new function in the Form1.cs file public partial class Form1: Form:
//去除转义字符
private string strDelete(string str)
{
int flag = -1, de = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '\\')
{
flag = i;
}
if (flag != -1)
{
de++;
}
if (str[i] == 'm' && flag != -1)
{
str = str.Remove(flag, de);
i = flag - 1;
flag = -1;
de = 0;
}
}
return str;
}
2. Interaction at both ends
Function: The data that the client wants to send to the server is input through the textbox, and then click the button to send it out, and the result returned by the server is displayed in the textbox.
Double-click the send button, and add the following code to the click function:
//获取textBox1内的文本内容
string msg = textBox2.Text + "\n";
//将文本内容转化成比特流并发送给服务器
byte[] data = new byte[1024];
data = Encoding.Default.GetBytes(msg);
stream.Write(data, 0, data.Length);
//接收服务器端传来的数据流并转化为字符串
byte[] data1 = new byte[1024];
int receive = stream.Read(data1, 0, 1024);
msg = Encoding.Default.GetString(data1, 0, receive);
//去除字符串中的终端转义字符
msg = strDelete(msg);
//清除显示框之前的内容
textBox1.Clear();
//显示数据
textBox1.AppendText(msg);
//刷新输入框
textBox2.Clear();
//将光标集中到输入框中
textBox2.Focus();
effect:
3. Play music
Function: After clicking to enter the game, play music in a loop
Add in the click function of the enter game button:
//字符串存储音乐路径
string s = @"D:\Demo.mp3";
//设置为循环播放
axWindowsMediaPlayer1.settings.setMode("loop", true);
//设置初始音乐的音量大小(范围:0——100)
axWindowsMediaPlayer1.settings.volume = 50;
//设置播放歌曲的路径
axWindowsMediaPlayer1.URL = s;
Effect: This can only be heard, not seen. . .
4. Background image
Function: cyclically change pictures
, add pictures to the imagelist control
Double-click the timer control and enter the tick function to add:
pictureBox1.Image = imageList1.Images[i];
i++;
if (i == 5)
{
i = 0;
}
Effect:
Note: If the picture is distorted, please refer to: Picture Distortion of ImageList
Summary and complete code
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.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
namespace 网游客户端
{
public partial class Form1 : Form
{
TcpClient tcpClient;
NetworkStream stream;
//去除转义字符
private string strDelete(string str)
{
int flag = -1, de = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '\\')
{
flag = i;
}
if (flag != -1)
{
de++;
}
if (str[i] == 'm' && flag != -1)
{
str = str.Remove(flag, de);
i = flag - 1;
flag = -1;
de = 0;
}
}
return str;
}
public Form1()
{
InitializeComponent();
}
//点击进入游戏连接服务器并显示内容,播放音乐
private void button3_Click(object sender, EventArgs e)
{
try
{
//实例化
tcpClient = new TcpClient();
//向指定的IP地址的服务器发出连接请求
tcpClient.Connect("10.160.52.106", 3900);
//获取网络传输流
stream = tcpClient.GetStream();
//接受数据并转化为字符串
byte[] data = new byte[1024];
int receive = stream.Read(data, 0, 1024);
string msg = Encoding.Default.GetString(data, 0, receive);
//去除字符串中的终端转义字符
msg = strDelete(msg);
//显示出来
textBox1.AppendText(msg);
}
catch
{
textBox1.AppendText("服务器未启动" + Environment.NewLine);
}
//字符串存储音乐路径
string s = @"D:\Demo.mp3";
//设置为循环播放
axWindowsMediaPlayer1.settings.setMode("loop", true);
//设置初始音乐的音量大小(范围:0——100)
axWindowsMediaPlayer1.settings.volume = 50;
//设置播放歌曲的路径
axWindowsMediaPlayer1.URL = s;
}
//点击发送,获取输入框内容发送给服务器
private void button2_Click(object sender, EventArgs e)
{
//获取textBox1内的文本内容
string msg = textBox2.Text + "\n";
//将文本内容转化成比特流并发送给服务器
byte[] data = new byte[1024];
data = Encoding.Default.GetBytes(msg);
stream.Write(data, 0, data.Length);
//接收服务器端传来的数据流并转化为字符串
byte[] data1 = new byte[1024];
int receive = stream.Read(data1, 0, 1024);
msg = Encoding.Default.GetString(data1, 0, receive);
//去除字符串中的终端转义字符
msg = strDelete(msg);
//清除显示框之前的内容
textBox1.Clear();
//显示数据
textBox1.AppendText(msg);
//刷新输入框
textBox2.Clear();
//将光标集中到输入框中
textBox2.Focus();
}
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image = imageList1.Images[i];
i++;
if (i == 5)
{
i = 0;
}
}
//
}
}
In the
process of programming, many codes have been referred to, and many problems have occurred, such as the problems related to music format used in the music playback method, the problem of display picture distortion and the problem of not displaying directly at the beginning, the problem of not being able to find the music playback control, etc., after perseverance Baidu and check the blog, and finally resolved.
Reference: C# writing online game client to connect to the game server
How to use C# ImageList image control
ImageList image distortion problem