2、控制客户端
控制客户端主要还是通过在控制端发送一个消息指令到客户端,客户端在接受消息后执行该执行。本例中主要以模拟鼠标点击事件为例来说明这一实现过程,通过在控制端中PictureBox控件上点击图像的某个位置,然后将这一消息转换成指令发送至客户端,客户端在接受该指令后执行,具体实现过程如下:
1)将控制端控制转换成指令
在此模拟鼠标点击事件,所以发送至客户端的命令可采用如下格式:
光标X坐标,光标Y坐标,鼠标事件
其中,鼠标事件中用0表示没有任何鼠标点击事件,1表示单击,2表示双击、3表示点击鼠标右键。
要获取光标的坐标,可以通过鼠标在PictureBox控件上点击位置进行转换,如下:
MouseEventArgs mouseE = (MouseEventArgs)e;
int origWidth = ClientPictureBox.Image.Width;
int origHeight = ClientPictureBox.Image.Height;
int cursorX, cursorY;
cursorX = mouseE.X * origWidth / ClientPictureBox.Width;
cursorY = mouseE.Y * origHeight / ClientPictureBox.Height;
其中,origWidth表示传输的客户端界面原始宽度,origHeight表示客户端界面原始高度,ClientPictureBox为控制端中显示客户端界面的图像控件,cursorX和cursorY表示鼠标在客户端计算机上的位置。
于是,在ClientPictureBox控件的Click()事件中添加如下代码:
【控制端】:
private void ClientPictureBox_Click(object sender, EventArgs e)
{
if (ClientPictureBox.Image != null)
{
//将图片中的坐标位置转换成客户端桌面的坐标
MouseEventArgs mouseE = (MouseEventArgs)e;
int origWidth = ClientPictureBox.Image.Width;
int origHeight = ClientPictureBox.Image.Height;
int cursorX, cursorY;
cursorX = mouseE.X * origWidth / ClientPictureBox.Width;
cursorY = mouseE.Y * origHeight / ClientPictureBox.Height;
string message = cursorX.ToString() + "," + cursorY.ToString();
if (mouseE.Button.ToString() == "Left")
{
message += ",1";
}
else if (mouseE.Button.ToString() == "Right")
{
message += ",3";
}
SendMessage(message);
}
}
其中,SendMessage()函数为一个发生消息至客户端的函数,稍后将介绍。在ClientPictureBox控件的DoubleClick ()事件中添加如下代码:
【控制端】:
private void ClientPictureBox_DoubleClick(object sender, EventArgs e)
{
if (ClientPictureBox.Image != null)
{
//将图片中的坐标位置转换成客户端桌面的坐标
MouseEventArgs mouseE = (MouseEventArgs)e;
int origWidth = ClientPictureBox.Image.Width;
int origHeight = ClientPictureBox.Image.Height;
int cursorX, cursorY;
cursorX = mouseE.X * origWidth / ClientPictureBox.Width;
cursorY = mouseE.Y * origHeight / ClientPictureBox.Height;
string message = cursorX.ToString() + "," + cursorY.ToString() + ",2";
SendMessage(message);
}
}
2)发送指令至客户端
前面已经提到了SendMessage()这个函数,该函数主要实现向客户端发送消息,其定义如下:
【控制端】:
private void SendMessage(string message)
{
TcpClient client = new TcpClient();
Int32 port = Int32.Parse(portTextBox.Text);
client.Connect(IPAddress.Parse(IPAddressTextBox.Text), port);
if (client.Connected)
{
byte[] bytes = Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
client.Close();
}
}
3)客户端接受指令
接受消息同样要采用TcpListener类来监听端口,一旦有数据传输过来时就接收,所以类似要在全局中定义变量:
【客户端】:
TcpListener tcpListener;
然后定义监听端口函数Listen(),当有数据传输过来时,将数据转换成String字符串,如下:
【客户端】:
private void Listen()
{
try
{
Int32 port = Int32.Parse(portTextBox.Text);
tcpListener = new TcpListener(port);
tcpListener.Start();
while (true)
{
TcpClient client = tcpListener.AcceptTcpClient();
NetworkStream stream = client.GetStream();
if (stream.CanRead)
{
string mouseEvent = "";
byte[] bytes = new byte[1024];
stream.Read(bytes, 0, bytes.Length);
mouseEvent = Encoding.ASCII.GetString(bytes);
//分解字符串
int i, mouseX, mouseY, mouseClick;
i = mouseEvent.IndexOf(",");
mouseX = int.Parse(Microsoft.VisualBasic.Strings.Left(mouseEvent, i));
mouseEvent = Microsoft.VisualBasic.Strings.Right(mouseEvent, mouseEvent.Length - i - 1);
i = mouseEvent.IndexOf(",");
mouseY = int.Parse(Microsoft.VisualBasic.Strings.Left(mouseEvent, i));
i = mouseEvent.IndexOf(",");
mouseEvent = Microsoft.VisualBasic.Strings.Right(mouseEvent, mouseEvent.Length - i - 1);
mouseClick = int.Parse(mouseEvent);
//设置鼠标
mouseClickEvent(mouseX, mouseY, mouseClick);
}
client.Close();
}
}
finally
{
tcpListener.Stop();
}
}
上述代码中,变量mouseEvent为控制端传输的指令,其格式为“光标X坐标,光标Y坐标,鼠标事件”,所以需要将其字符串进行分割,分割出光标X坐标、光标Y坐标和鼠标事件,然后使用mouseClickEvent()函数来执行该命令,关于这个执行鼠标命令的函数下面开始介绍。
4)执行鼠标命令
执行鼠标命令主要通过Windows API中的SetCursorPos()函数设置光标位置,mouse_event()函数来指定鼠标事件,所以首先需要在全局中引用这些函数,如下:
【客户端】:
[DllImport("user32.dll")]
private extern static bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
private static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
然后定义执行鼠标命令的函数mouseClickEvent(),其代码如下:
【客户端】:
private void mouseClickEvent(int x, int y, int state)
{
SetCursorPos(x, y);
if (state == 0)
{
return;
}
else if (state == 1) //单击
{
mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.LeftUp, x, y, 0, (UIntPtr)0);
}
else if (state == 2) //双击
{
mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.LeftUp, x, y, 0, (UIntPtr)0);
mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.LeftUp, x, y, 0, (UIntPtr)0);
}
else if (state == 3) //右键
{
mouse_event(MouseEventFlag.RightDown | MouseEventFlag.RightUp, x, y, 0, (UIntPtr)0);
}
}