远程控制(3)
本来想把这些都写在(2)里面的,想想还是写到(3)吧,这样比较容易看点
完成图片传送和显示,那么就开始控制,有了前面的基础,对于网络传输都有一定的了解了,那么我们再传其他东西也差不多道理.还不懂就得找本书看了..老实说,看书才是硬道理.
先看下面两个数据结构:
一个是鼠标事件的结构体,一个鼠标事件类型枚举
我们这次发送和接收的包,当然,还可以有键盘事件的包,我偷懒,没做网络传输就不说了,和(1)(2)差不多,上面的结构体也定义得可以用了.
说说流程吧
鼠标的事件都是在panel上的事件,move,click,doubleclick,左键右键就自己判断了,由于panel大小是和受控方屏幕大小一样,所以获得坐标就等同于受控方屏幕的坐标,这样,一个MouseEvent的包就可以组成了,发送.
受控放接收,直接构造函数构造一个新的MouseEvent(Byte[] Content).就可以通过结构体的属性获得相应的内容,说明一下,图片和事件的侦听和发送端口不能一样.这样就可以进行控制了.
怎么控制呢?这里就要调用win32 API了,还有用到委托,因为委托是线程安全的.
首先看一下声明吧,我们要用到的API
然后鼠标事件类型的枚举:
声明委托与事件
添加事件
到这里,我们就可以模拟鼠标的事件了
完成图片传送和显示,那么就开始控制,有了前面的基础,对于网络传输都有一定的了解了,那么我们再传其他东西也差不多道理.还不懂就得找本书看了..老实说,看书才是硬道理.
先看下面两个数据结构:
struct MouseEvent
{
private Byte[] type;
private Byte[] x;
private Byte[] y;
public MouseEvent(MouseEventType Type,int X,int Y)
{
this.type = BitConverter.GetBytes((int)Type);
this.x = BitConverter.GetBytes(X);
this.y = BitConverter.GetBytes(Y);
}
public MouseEvent(Byte[] Type, Byte[] X, Byte[] Y)
{
this.type = Type;
this.x = X;
this.y = Y;
}
public MouseEvent(Byte[] Content)
{
type = new byte[4];
x = new byte[4];
y = new byte[4];
for (int i = 0; i < Content.Length; i++)
{
if (i >= 0 && i < 4)
type[i] = Content[i];
if (i >= 4 && i < 8)
x[i-4] = Content[i];
if (i >= 8 && i < 12)
y[i-8] = Content[i];
}
}
public MouseEventType Type
{
get { return (MouseEventType)BitConverter.ToInt32(type, 0); }
}
public int X
{
get { return BitConverter.ToInt32(x, 0); }
}
public int Y
{
get { return BitConverter.ToInt32(y, 0); }
}
public Byte[] ToBytes()
{
Byte[] Bytes = new Byte[12];
type.CopyTo(Bytes, 0);
x.CopyTo(Bytes, 4);
y.CopyTo(Bytes, 8);
return Bytes;
}
}
enum MouseEventType : int
{
MouseMove=0,
MouseLeftDown,
MouseLeftUp,
MouseRightDown,
MouseRightUp,
MouseClick,
MouseDoubleClick
}
{
private Byte[] type;
private Byte[] x;
private Byte[] y;
public MouseEvent(MouseEventType Type,int X,int Y)
{
this.type = BitConverter.GetBytes((int)Type);
this.x = BitConverter.GetBytes(X);
this.y = BitConverter.GetBytes(Y);
}
public MouseEvent(Byte[] Type, Byte[] X, Byte[] Y)
{
this.type = Type;
this.x = X;
this.y = Y;
}
public MouseEvent(Byte[] Content)
{
type = new byte[4];
x = new byte[4];
y = new byte[4];
for (int i = 0; i < Content.Length; i++)
{
if (i >= 0 && i < 4)
type[i] = Content[i];
if (i >= 4 && i < 8)
x[i-4] = Content[i];
if (i >= 8 && i < 12)
y[i-8] = Content[i];
}
}
public MouseEventType Type
{
get { return (MouseEventType)BitConverter.ToInt32(type, 0); }
}
public int X
{
get { return BitConverter.ToInt32(x, 0); }
}
public int Y
{
get { return BitConverter.ToInt32(y, 0); }
}
public Byte[] ToBytes()
{
Byte[] Bytes = new Byte[12];
type.CopyTo(Bytes, 0);
x.CopyTo(Bytes, 4);
y.CopyTo(Bytes, 8);
return Bytes;
}
}
enum MouseEventType : int
{
MouseMove=0,
MouseLeftDown,
MouseLeftUp,
MouseRightDown,
MouseRightUp,
MouseClick,
MouseDoubleClick
}
一个是鼠标事件的结构体,一个鼠标事件类型枚举
我们这次发送和接收的包,当然,还可以有键盘事件的包,我偷懒,没做网络传输就不说了,和(1)(2)差不多,上面的结构体也定义得可以用了.
说说流程吧
鼠标的事件都是在panel上的事件,move,click,doubleclick,左键右键就自己判断了,由于panel大小是和受控方屏幕大小一样,所以获得坐标就等同于受控方屏幕的坐标,这样,一个MouseEvent的包就可以组成了,发送.
受控放接收,直接构造函数构造一个新的MouseEvent(Byte[] Content).就可以通过结构体的属性获得相应的内容,说明一下,图片和事件的侦听和发送端口不能一样.这样就可以进行控制了.
怎么控制呢?这里就要调用win32 API了,还有用到委托,因为委托是线程安全的.
首先看一下声明吧,我们要用到的API
[DllImport("user32")]
public static extern void mouse_event(MouseEventFlag flags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport("user32")]
public static extern bool SetCursorPos(int X, int Y);
public static extern void mouse_event(MouseEventFlag flags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport("user32")]
public static extern bool SetCursorPos(int X, int Y);
然后鼠标事件类型的枚举:
public enum MouseEventFlag
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
声明委托与事件
public delegate void DoMouseButtons(MouseEventFlag flags, int dx, int dy, int dwData, int dwExtraInfo);
public delegate bool DoMouseMove(int X, int Y);
private event DoMouseMove MouseMove;
private event DoMouseButtons MouseButton;
public delegate bool DoMouseMove(int X, int Y);
private event DoMouseMove MouseMove;
private event DoMouseButtons MouseButton;
添加事件
MouseButton += new DoMouseButtons(mouse_event);
MouseMove += new DoMouseMove(SetCursorPos);
MouseMove += new DoMouseMove(SetCursorPos);
到这里,我们就可以模拟鼠标的事件了
switch (MEvent.Type)
{
case MouseEventType.MouseMove:
MouseMove(MEvent.X, MEvent.Y);
break;
case MouseEventType.MouseLeftDown:
MouseButton(MouseEventFlag.LeftDown, MEvent.X, MEvent.Y, 0, 0);
break;
case MouseEventType.MouseLeftUp:
MouseButton(MouseEventFlag.LeftUp, MEvent.X, MEvent.Y, 0, 0);
break;
case MouseEventType.MouseRightDown:
MouseButton(MouseEventFlag.RightDown, MEvent.X, MEvent.Y, 0, 0);
break;
case MouseEventType.MouseRightUp:
MouseButton(MouseEventFlag.RightUp, MEvent.X, MEvent.Y, 0, 0);
break;
case MouseEventType.MouseClick:
MouseButton(MouseEventFlag.LeftDown, MEvent.X, MEvent.Y, 0, 0);
MouseButton(MouseEventFlag.LeftUp, MEvent.X, MEvent.Y, 0, 0);
break;
case MouseEventType.MouseDoubleClick:
MouseButton(MouseEventFlag.LeftDown, MEvent.X, MEvent.Y, 0, 0);
MouseButton(MouseEventFlag.LeftDown, MEvent.X, MEvent.Y, 0, 0);
MouseButton(MouseEventFlag.LeftUp, MEvent.X, MEvent.Y, 0, 0);
MouseButton(MouseEventFlag.LeftUp, MEvent.X, MEvent.Y, 0, 0);
break;
}
{
case MouseEventType.MouseMove:
MouseMove(MEvent.X, MEvent.Y);
break;
case MouseEventType.MouseLeftDown:
MouseButton(MouseEventFlag.LeftDown, MEvent.X, MEvent.Y, 0, 0);
break;
case MouseEventType.MouseLeftUp:
MouseButton(MouseEventFlag.LeftUp, MEvent.X, MEvent.Y, 0, 0);
break;
case MouseEventType.MouseRightDown:
MouseButton(MouseEventFlag.RightDown, MEvent.X, MEvent.Y, 0, 0);
break;
case MouseEventType.MouseRightUp:
MouseButton(MouseEventFlag.RightUp, MEvent.X, MEvent.Y, 0, 0);
break;
case MouseEventType.MouseClick:
MouseButton(MouseEventFlag.LeftDown, MEvent.X, MEvent.Y, 0, 0);
MouseButton(MouseEventFlag.LeftUp, MEvent.X, MEvent.Y, 0, 0);
break;
case MouseEventType.MouseDoubleClick:
MouseButton(MouseEventFlag.LeftDown, MEvent.X, MEvent.Y, 0, 0);
MouseButton(MouseEventFlag.LeftDown, MEvent.X, MEvent.Y, 0, 0);
MouseButton(MouseEventFlag.LeftUp, MEvent.X, MEvent.Y, 0, 0);
MouseButton(MouseEventFlag.LeftUp, MEvent.X, MEvent.Y, 0, 0);
break;
}
鼠标双击的这个事件有点问题,有时候不行...
恩,完了,就这样,一个简单的远程控制就完成了,这个东西是我个人娱乐所作,虽然没有什么技术含量,但是通过这样,也可以学到很多东西,同时很多东西也加深了理解...
今天是元旦佳节哦~
祝大家猪年快乐! 年年进步!
友情提示:
如需转载本文,请遵守"本站协议"并加入下面声明
且注明原文链接。
作者:kevin wu
来源:kevin wu's corner
by Kevin wu