用vs 2010 写了一个 远程控制程序 。在调试运行过程中正常,但是打开bin/debug下的exe部分无效。 点击本程序窗口和浏览器,txt都可以,但是点击文件夹和桌面,鼠标事件都不可用。但是鼠标移动和键盘输入都是正常的。
[DllImport ( "user32.dll" , EntryPoint = "mouse_event" )]
private extern static int mouse_event(int dwFlags, int dx, int dy, int cButtons, IntPtr dwExtraInfo);
通过 int的返回值 看出在vs中运行时得到的是1,用exe直接打开 点击桌面或者文件夹时,mouseDown 返回1 ,mouseUp返回0 ,之后就一直为0了,用鼠标选中 自己程序窗口或者浏览器,再用远程控制 返回的又是1 ,可用了。不知道为什么 exe打开无法操作 桌面和文件夹。
如果是用vs调试上述问题不存在,良好执行。
是一个远程桌面,下面附源码:被控端:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;namespace SocketCaptureClient
{
public partial class CaptureClientFrm : Form
{ #region 声明变量和调用的Windows库函数 private const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
private const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
private const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起
//调用系统函数 将鼠标移动到相应位置
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
public extern static bool SetCursorPos(int x, int y); //调用系统函数 鼠标事件函数
[DllImport("user32", EntryPoint = "mouse_event")]
private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); #endregion
public CaptureClientFrm()
{
InitializeComponent();
ipAddress.Text = "10.10.54.174";
nudPort.Value = 10001; }
#region 属性
private string serveraddress = "10.10.54.174";//默认IP地址
private int serverport = 10001;//端口 默认端口 private Thread tReceive;//处理线程
private TcpClient clientsocket;//client套接字 socket的封装
private NetworkStream ns; private bool connected;
bool keepalive = true; #endregion void InfoDeal(string strRet)
{ String[] temp = strRet.Split('#'); for (int i = 0; i < temp.Length - 1; i++)
{
if (temp[i].IndexOf('@') != -1)
{
//MessageBox.Show(temp[i]);
String[] temp1 = temp[i].Split('@'); if (temp1[0].IndexOf('$') != -1)
{
String[] temp2 = temp1[0].Split('$');
// MessageBox.Show(temp2[0]);
MouseClss.OperMouse(temp2[0]);
}
else
{
//MessageBox.Show(temp1[0]);
KeyBoardClss.keyInput_single(temp1[0]); }
}
else
{
MouseClss.OperMouseMove(temp[i]);
}
}
} /// <summary>
/// 接收信息
/// </summary>
public void Receive()
{
List<string> lReceiveMsg = new List<string>();
int i;
while (keepalive)
{
Byte[] buffer = new Byte[102400];
string strRet = "";
try
{
i = ns.Read(buffer, 0, buffer.Length); //返回兑取的字节数
}
catch
{
connected = false;
Stop();//关闭套接字,终止循环,终止线程
break;
}
if (i > 0)
{
strRet = System.Text.Encoding.Default.GetString(buffer, 0, i);//字节数组解码为字符串
try
{
InfoDeal(strRet);//收到信息的处理
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ErrorInfo_Receive", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} System.Threading.Thread.Sleep(10);
}
} /// <summary>
/// 关闭套接字,终止循环,终止线程
/// </summary>
public void Stop()
{
try
{
timer1.Enabled = false;
if (connected)
{
try
{
clientsocket.Close(); //关闭套接字
}
catch { }
}
if (tReceive != null && tReceive.IsAlive)
{
try
{
keepalive = false;//停止循环
tReceive.Abort();//终止线程
}
catch { }
}
connected = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ErrorInfo_Stop", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} /// <summary>
/// 捕获屏幕图像 (并发送图像)
/// </summary>
public void CaptureImage(Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle) //Rectangle绘制矩形形状
{
try
{
Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height);//创建图像对象 Graphics gra = Graphics.FromImage(bitmap);//以bitmap为基础创建图面,,图像是建立在bitmap 上的
gra.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);//从屏幕到 Graphics 的绘图图面的位块传输 byte[] outbytes;
using (MemoryStream ms = new MemoryStream()) //自动释放范围内的资源
{
//pictureBox1.Image.Save ( ms , ImageFormat.Jpeg );
bitmap.Save(ms, ImageFormat.Jpeg);//将图像以指定的格式存放在流中
outbytes = ms.ToArray();
}
Send(outbytes);// 发送命令单条信息
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} /// <summary>
/// 发送命令单条信息
/// </summary>
public int Send(Byte[] outbytes)
{
if (outbytes.Length <= 0) return -1;
if (connected)
{
try
{
ns.Write(outbytes, 0, outbytes.Length);
return 1;
}
catch
{
connected = false;
Stop();
return -2;
}
}
else
return -3;
} /// <summary>
/// 建立连接
/// </summary>
public bool Start()
{
try
{
clientsocket = new TcpClient(serveraddress, serverport);//套接字
ns = clientsocket.GetStream();//建立流
connected = true;
keepalive = true;
tReceive = new Thread(new ThreadStart(Receive));//建立接受字符的线程
tReceive.Start();
//----------------------------------------------------------------------- }
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ErrorInfo_Start", MessageBoxButtons.OK, MessageBoxIcon.Error);
connected = false;
}
return connected;
}
/// <summary>
/// timer计时器函数,并不是一执行就启动,条件控制启动
/// 定时调用截图函数,发送截图
/// </summary>
private void timer1_Tick(object sender, EventArgs e)
{
Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));//显示器最大矩形
CaptureImage(Point.Empty, Point.Empty, bounds);
} private void toolStripButton1_Click(object sender, EventArgs e)
{
if (sender.Equals(tsListen))
{
serveraddress = ipAddress.Text;
try
{
serverport = Convert.ToInt32(nudPort.Value);
}
catch
{
serverport = 10001;
}
if (tsListen.Text == "ListenRun")
{
tsListen.Text = "ListenStop";
Start();//建立链接,启动接受线程,接受鼠标键盘控制信息
timer1.Enabled = true;//调整到Start()下面,链接后发送图像
}
else
{
timer1.Enabled = false;
tsListen.Text = "ListenRun";
Stop();
}
}
} //关闭窗口
private void CaptureClientFrm_FormClosing(object sender, FormClosingEventArgs e)
{
Stop();
}
}
}
[DllImport ( "user32.dll" , EntryPoint = "mouse_event" )]
private extern static int mouse_event(int dwFlags, int dx, int dy, int cButtons, IntPtr dwExtraInfo);
通过 int的返回值 看出在vs中运行时得到的是1,用exe直接打开 点击桌面或者文件夹时,mouseDown 返回1 ,mouseUp返回0 ,之后就一直为0了,用鼠标选中 自己程序窗口或者浏览器,再用远程控制 返回的又是1 ,可用了。不知道为什么 exe打开无法操作 桌面和文件夹。
如果是用vs调试上述问题不存在,良好执行。
是一个远程桌面,下面附源码:被控端:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;namespace SocketCaptureClient
{
public partial class CaptureClientFrm : Form
{ #region 声明变量和调用的Windows库函数 private const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
private const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
private const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起
//调用系统函数 将鼠标移动到相应位置
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
public extern static bool SetCursorPos(int x, int y); //调用系统函数 鼠标事件函数
[DllImport("user32", EntryPoint = "mouse_event")]
private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); #endregion
public CaptureClientFrm()
{
InitializeComponent();
ipAddress.Text = "10.10.54.174";
nudPort.Value = 10001; }
#region 属性
private string serveraddress = "10.10.54.174";//默认IP地址
private int serverport = 10001;//端口 默认端口 private Thread tReceive;//处理线程
private TcpClient clientsocket;//client套接字 socket的封装
private NetworkStream ns; private bool connected;
bool keepalive = true; #endregion void InfoDeal(string strRet)
{ String[] temp = strRet.Split('#'); for (int i = 0; i < temp.Length - 1; i++)
{
if (temp[i].IndexOf('@') != -1)
{
//MessageBox.Show(temp[i]);
String[] temp1 = temp[i].Split('@'); if (temp1[0].IndexOf('$') != -1)
{
String[] temp2 = temp1[0].Split('$');
// MessageBox.Show(temp2[0]);
MouseClss.OperMouse(temp2[0]);
}
else
{
//MessageBox.Show(temp1[0]);
KeyBoardClss.keyInput_single(temp1[0]); }
}
else
{
MouseClss.OperMouseMove(temp[i]);
}
}
} /// <summary>
/// 接收信息
/// </summary>
public void Receive()
{
List<string> lReceiveMsg = new List<string>();
int i;
while (keepalive)
{
Byte[] buffer = new Byte[102400];
string strRet = "";
try
{
i = ns.Read(buffer, 0, buffer.Length); //返回兑取的字节数
}
catch
{
connected = false;
Stop();//关闭套接字,终止循环,终止线程
break;
}
if (i > 0)
{
strRet = System.Text.Encoding.Default.GetString(buffer, 0, i);//字节数组解码为字符串
try
{
InfoDeal(strRet);//收到信息的处理
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ErrorInfo_Receive", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} System.Threading.Thread.Sleep(10);
}
} /// <summary>
/// 关闭套接字,终止循环,终止线程
/// </summary>
public void Stop()
{
try
{
timer1.Enabled = false;
if (connected)
{
try
{
clientsocket.Close(); //关闭套接字
}
catch { }
}
if (tReceive != null && tReceive.IsAlive)
{
try
{
keepalive = false;//停止循环
tReceive.Abort();//终止线程
}
catch { }
}
connected = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ErrorInfo_Stop", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} /// <summary>
/// 捕获屏幕图像 (并发送图像)
/// </summary>
public void CaptureImage(Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle) //Rectangle绘制矩形形状
{
try
{
Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height);//创建图像对象 Graphics gra = Graphics.FromImage(bitmap);//以bitmap为基础创建图面,,图像是建立在bitmap 上的
gra.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);//从屏幕到 Graphics 的绘图图面的位块传输 byte[] outbytes;
using (MemoryStream ms = new MemoryStream()) //自动释放范围内的资源
{
//pictureBox1.Image.Save ( ms , ImageFormat.Jpeg );
bitmap.Save(ms, ImageFormat.Jpeg);//将图像以指定的格式存放在流中
outbytes = ms.ToArray();
}
Send(outbytes);// 发送命令单条信息
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} /// <summary>
/// 发送命令单条信息
/// </summary>
public int Send(Byte[] outbytes)
{
if (outbytes.Length <= 0) return -1;
if (connected)
{
try
{
ns.Write(outbytes, 0, outbytes.Length);
return 1;
}
catch
{
connected = false;
Stop();
return -2;
}
}
else
return -3;
} /// <summary>
/// 建立连接
/// </summary>
public bool Start()
{
try
{
clientsocket = new TcpClient(serveraddress, serverport);//套接字
ns = clientsocket.GetStream();//建立流
connected = true;
keepalive = true;
tReceive = new Thread(new ThreadStart(Receive));//建立接受字符的线程
tReceive.Start();
//----------------------------------------------------------------------- }
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ErrorInfo_Start", MessageBoxButtons.OK, MessageBoxIcon.Error);
connected = false;
}
return connected;
}
/// <summary>
/// timer计时器函数,并不是一执行就启动,条件控制启动
/// 定时调用截图函数,发送截图
/// </summary>
private void timer1_Tick(object sender, EventArgs e)
{
Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));//显示器最大矩形
CaptureImage(Point.Empty, Point.Empty, bounds);
} private void toolStripButton1_Click(object sender, EventArgs e)
{
if (sender.Equals(tsListen))
{
serveraddress = ipAddress.Text;
try
{
serverport = Convert.ToInt32(nudPort.Value);
}
catch
{
serverport = 10001;
}
if (tsListen.Text == "ListenRun")
{
tsListen.Text = "ListenStop";
Start();//建立链接,启动接受线程,接受鼠标键盘控制信息
timer1.Enabled = true;//调整到Start()下面,链接后发送图像
}
else
{
timer1.Enabled = false;
tsListen.Text = "ListenRun";
Stop();
}
}
} //关闭窗口
private void CaptureClientFrm_FormClosing(object sender, FormClosingEventArgs e)
{
Stop();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;namespace SocketCaptureClient
{
public class MouseClss
{
#region 声明变量和调用的Windows库函数 private const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
private const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
private const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起
private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
//调用系统函数 将鼠标移动到相应位置
[DllImport ( "user32.dll" , EntryPoint = "SetCursorPos" )]
public extern static bool SetCursorPos ( int x , int y ); //调用系统函数 鼠标事件函数
[DllImport ( "user32.dll" , EntryPoint = "mouse_event" )]
private extern static int mouse_event(int dwFlags, int dx, int dy, int cButtons, IntPtr dwExtraInfo);
#endregion public static void OperMouse ( string strRet )
{
//Console.WriteLine(strRet);
string[] temp = strRet.Split('_');
int ;
if (strRet == "mouseright")//鼠标右键事件
{ try
{
= mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, IntPtr.Zero);
Thread.Sleep(8);
Console.WriteLine("mouseright:" + + ";\n");
}
catch ( Exception ex )
{
MessageBox.Show ( ex.Message , "ErrorInfo_mouseright" , MessageBoxButtons.OK , MessageBoxIcon.Error );
}
}
else if (strRet == "mouseleft_Down")//鼠标左键事件
{
try
{
= mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_ABSOLUTE, 0, 0, 0, IntPtr.Zero);
Thread.Sleep(8);
Console.WriteLine("mouseleft_Down:" + + ";\n");
}
catch ( Exception ex )
{
MessageBox.Show ( ex.Message , "ErrorInfo_mouseleft"+temp[1] , MessageBoxButtons.OK , MessageBoxIcon.Error );
}
}
else if (strRet == "mouseleft_Up")//鼠标左键事件
{
try
{
= mouse_event(MOUSEEVENTF_LEFTUP|MOUSEEVENTF_ABSOLUTE, 0, 0, 0, IntPtr.Zero);
Thread.Sleep(8);
Console.WriteLine("mouseleft_Up:" + + ";\n");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ErrorInfo_mouseleft" + temp[1], MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else if (strRet == "mousedouble")//鼠标双击事件
{
try
{ = mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, IntPtr.Zero);
Thread.Sleep(200);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, IntPtr.Zero);
Thread.Sleep(8);
Console.WriteLine("mousedouble:" + +";\n");
}
catch ( Exception ex )
{
MessageBox.Show ( ex.Message , "ErrorInfo_mousedouble" , MessageBoxButtons.OK , MessageBoxIcon.Error );
}
}
} public static void OperMouseMove(String strRet)
{
try
{
String[] temp = strRet.Split('_'); SetCursorPos(Int32.Parse(temp[1]), Int32.Parse(temp[2]));
}
catch (Exception ex)
{
MessageBox.Show(strRet);
MessageBox.Show(ex.Message, "ErrorInfo_SetCursorPos", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace SocketCaptureClient
{
public class KeyBoardClss
{
[DllImport ( "user32.dll" , EntryPoint = "keybd_event" )]
public static extern void keybd_event ( //模拟按键事件
byte bVk ,
byte bScan ,
int dwFlags ,
int dwExtraInfo
);
public static void keyInput_single(string strRet)
{
try
{
String[] temp = strRet.Split('_');
byte bKey = byte.Parse(temp[1]); if (bKey != 0)
{
if (temp[0] == "Key")
{
keybd_event(bKey, 0, 0, 0);//按键点下
if(temp[1]=="18")
keybd_event(bKey, 0, 2, 0);
}
else
{
keybd_event(bKey, 0, 2, 0);//按键弹起
}
}
}
catch (Exception ex)
{
MessageBox.Show(strRet);
MessageBox.Show(ex.Message, "ErrorInfo_keyInput_single", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }}