屏幕截图:
[Flags]
public 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
}
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
public static void Mouse_Event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo)
{
mouse_event(flags, dx, dy, data, extraInfo);
}
鼠标控制:
/// <summary>
/// 不支持wndows服务截图
/// </summary>
/// <returns></returns>
public static Bitmap GetDeskImg()
{
Bitmap img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
return img;
//img.Save("c://a.Bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
remoting实现传输数据:
public class RemotingHelper
{
public static TcpClientChannel clientChannel;
public static TcpServerChannel serverChannel;
public void RegServer<T>(int Mode)
{
WellKnownObjectMode WMode;
if (Mode == 1)
WMode = WellKnownObjectMode.Singleton;
else
WMode = WellKnownObjectMode.SingleCall;
serverChannel = new TcpServerChannel(8085);
ChannelServices.RegisterChannel(serverChannel,false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(T), "server", WMode);
//RemotingConfiguration.Configure(
// AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
// );
//RemotingConfiguration.Configure( System.Environment.CurrentDirectory + "//Server.config",true);
//Remote.SendEvent += new Remote.SendEventHandler(OnSend);
}
public void RegServer<T>(int Mode, int port)
{
WellKnownObjectMode WMode;
if (Mode == 1)
WMode = WellKnownObjectMode.Singleton;
else
WMode = WellKnownObjectMode.SingleCall;
serverChannel = new TcpServerChannel(port);
ChannelServices.RegisterChannel(serverChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(T), "server", WMode);
}
public void UnRegServer()
{
ChannelServices.UnregisterChannel(serverChannel);
}
public void RegClientChannel()
{
RemotingHelper.clientChannel = new TcpClientChannel();
ChannelServices.RegisterChannel(clientChannel, false);
}
public T RegRemoteObject<T>(string IP,int port)
{
try
{
string _ip;
if (string.IsNullOrEmpty(IP))
{
_ip = System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"];
}
else
{
_ip = "tcp://" + IP + ":" + port.ToString() + "/server";
}
T t = (T)Activator.GetObject(typeof(T), _ip);
return t;
}
catch
{
ChannelServices.UnregisterChannel(clientChannel);
return default(T);
}
}
public void UnRegClient()
{
try
{
ChannelServices.UnregisterChannel(RemotingHelper.clientChannel);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
被控制端开机自动运行:
public class RegistryHelper
{
/// <summary>
/// 开机自动运行
/// </summary>
/// <param name="strExeName">EXE程序名(例如:****.exe)</param>
/// <param name="isRunMachine">是否开机自动运行</param>
public static void SetAutoRun(string key,string strExeName, bool IsAutoRun)
{
//获取Run键
RegistryKey key1 = Registry.LocalMachine;
RegistryKey key2 = key1.CreateSubKey("SOFTWARE");
RegistryKey key3 = key2.CreateSubKey("Microsoft");
RegistryKey key4 = key3.CreateSubKey("Windows");
RegistryKey key5 = key4.CreateSubKey("CurrentVersion");
RegistryKey key6 = key5.CreateSubKey("Run");
if (IsAutoRun)
{
key6.SetValue(key, strExeName);
}
else
{
//key6.SetValue(strExeName, false);
if (key6.GetValue(key) != null)
key6.DeleteValue(key);
}
}
public static string GetAutoRunFile(string key)
{
RegistryKey key1 = Registry.LocalMachine;
RegistryKey key2 = key1.CreateSubKey("SOFTWARE");
RegistryKey key3 = key2.CreateSubKey("Microsoft");
RegistryKey key4 = key3.CreateSubKey("Windows");
RegistryKey key5 = key4.CreateSubKey("CurrentVersion");
RegistryKey key6 = key5.CreateSubKey("Run");
object objFile = key6.GetValue(key);
if (objFile != null)
return objFile.ToString();
else
return null;
}
}
远程窗口控制类:
/// <summary>
/// 远程控制窗口
/// </summary>
public class WindowClass
{
public IRemoteWindow.IRemoteWindow ObjRemoteWindow = null;
public delegate void GetRemoteWindowHandler(Image img);
public event GetRemoteWindowHandler GetRemoteWindowEvent;
/// <summary>
/// 获取失败事件委托
/// </summary>
public delegate void GetRemoteWindowErrorHandler(string errMsg);
/// <summary>
/// 获取失败事件
/// </summary>
public event GetRemoteWindowErrorHandler GetRemoteWindowErrorEvent;
private PictureBox _PicScreen = null;
private ThreadStart thStart;
private Thread th;
private bool _IsLoop = false;
public int ScreenWidth = 0;
public int ScreenHeight = 0;
/// <summary>
/// 是否远程控制
/// </summary>
public bool IsControl = false;
/// <summary>
/// 是否正在远程桌面
/// </summary>
public bool IsLoop
{
get { return _IsLoop; }
}
public WindowClass(IRemoteWindow.IRemoteWindow objRemoteWindow, PictureBox picScreen)
{
ObjRemoteWindow = objRemoteWindow;
_PicScreen = picScreen;
thStart = new ThreadStart(GetWindowLoopEvent);
}
public void Start()
{
th = new Thread(thStart);
th.IsBackground = true;
th.Start();
_IsLoop = true;
}
public void Stop()
{
//RemotingHelper remotingHelper = new RemotingHelper();
//remotingHelper.UnRegClient();
_IsLoop = false;
IsControl = false;
}
public void Trigger()
{
if (_IsLoop )
Stop();
else
Start();
}
private void GetWindowLoopEvent()
{
do
{
GetRemoteWindow(); Thread.Sleep(500);
}
while (_IsLoop);
}
/// <summary>
/// 获取远程桌面通过二进制序列化
/// </summary>
private void GetRemoteWindow()
{
try
{
byte[] e = ObjRemoteWindow.GetRemoteWindow();
if (e != null)
{
MemoryStream ms = new MemoryStream(e);
Image img = Image.FromStream(ms);
if (_PicScreen != null)
_PicScreen.Image = img;
//this._FrmScreen.picWindow.Image = img;
ScreenWidth = img.Width;
ScreenHeight = img.Height;
if (GetRemoteWindowEvent != null)
GetRemoteWindowEvent(img);
//_FrmScreen.picWindow.Image = img;
//_FrmScreen.lblDistShow(img.Width, img.Height);
}
else
{
_PicScreen = null;
}
//Width = 800;
//Height = 600;
}
catch (Exception ex)
{
Stop();
if (GetRemoteWindowErrorEvent != null)
GetRemoteWindowErrorEvent(ex.Message);
}
}
}
局域网计算机扫描类:
public class ScanComputerList
{
public bool IsRun = false;
public ScanComputer[] ScanComputers;
/// <summary>
/// 单个扫描个数
/// </summary>
public int RunCount = 0;
#region 事件
public delegate void FoundComputerHandler(string[] arr);
/// <summary>
/// 单个计算机扫描成功事件
/// </summary>
public event FoundComputerHandler FoundComputerEvent;
public delegate void ScanAllFinishedHandler(int LastCount);
/// <summary>
/// 单个计算机扫描结束
/// </summary>
public event ScanAllFinishedHandler ScanAllFinishedEvent;
public delegate void ScanAllSartHandler(int AllCount);
/// <summary>
/// 扫描开始事件
/// </summary>
public event ScanAllSartHandler ScanAllStartEvent;
#endregion
public ScanComputerList()
{
ScanComputer.FoundComputerEvent += new ScanComputer.FoundComputerHandler(ScanComputer_FoundComputerEvent);
ScanComputer.ScanAllFinishedEvent += new ScanComputer.ScanAllFinishedHandler(ScanComputer_ScanAllFinishedEvent);
}
/// <summary>
/// 开始扫描局域网计算机
/// </summary>
/// <param name="startIP"></param>
/// <param name="endIP"></param>
public bool Run(int startIP, int endIP)
{
if (RunCount > 0) return false;
string ipPrefix = "192.168.1";
ipPrefix = System.Configuration.ConfigurationManager.AppSettings.Get("IPPrefix");
if (string.IsNullOrEmpty(ipPrefix))
ipPrefix = "192.168.1";
ipPrefix += ".";
ScanComputers = new ScanComputer[endIP - startIP +1];
RunCount = ScanComputers.Length;
if (ScanAllStartEvent != null)
ScanAllStartEvent(RunCount);
int thIndex = -1;
for (int i = startIP; i <= endIP; i++)
{
thIndex++;
ScanComputers[thIndex] = new ScanComputer(ipPrefix + i);
ScanComputers[thIndex].Start();
}
return true;
}
/// <summary>
/// 单个计算机扫描成功事件
/// </summary>
/// <param name="arr"></param>
protected void ScanComputer_FoundComputerEvent(string[] arr)
{
if (FoundComputerEvent != null)
FoundComputerEvent(arr);
}
/// <summary>
/// 单个扫描完成
/// </summary>
private void ScanComputer_ScanAllFinishedEvent()
{
RunCount--;
if (ScanAllFinishedEvent != null)
ScanAllFinishedEvent(RunCount);
}
}
另外还有远程文件控制类、windows图标获取类