实例介绍
【实例简介】
【实例截图】
【核心代码】
操作类
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace SmartKernel.Net { public partial class MonitorClient : Form { #region 字段 private bool Initialized = false; private bool InProgress = false; private System.Threading.Thread MonitorThread = null; #endregion #region 构造函数 public MonitorClient() { InitializeComponent(); Application.Idle = new EventHandler(Application_Idle); } #endregion #region 应用程序空闲状态的处理过程 void Application_Idle(object sender, EventArgs e) { this.checkBox1.Enabled = this.textBox1.Text.Trim().Length > 0; this.checkBox2.Enabled = this.checkBox1.Enabled; } #endregion #region 监控开始 private void Monitor() { while (true) { this.monitorUserControl1.UpdateDisplay(); System.Threading.Thread.Sleep(200); } } #endregion #region 监视 private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (((CheckBox)sender).Checked) { if (!Initialized) { Cursor = Cursors.WaitCursor; this.monitorUserControl1.Initialize(this.textBox1.Text.Trim()); Initialized = true; Cursor = Cursors.Arrow; } InProgress = true; MonitorThread = new System.Threading.Thread(new System.Threading.ThreadStart(Monitor)); MonitorThread.Start(); } else { InProgress = false; MonitorThread.Abort(); } } #endregion #region 控制 private void checkBox2_CheckedChanged(object sender, EventArgs e) { this.monitorUserControl1.SetControl(((CheckBox)sender).Checked); } #endregion } }
通信类
using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.IO; using System.Net.Sockets; using System.Threading; namespace SmartKernel.Net { public class Monitor : System.MarshalByRefObject { #region 常量 private const uint MOUSEEVENTF_MOVE = 0x0001; //系统消息:鼠标移动 private const uint MOUSEEVENTF_LEFTDOWN = 0x0002; //系统消息:左键按下 private const uint MOUSEEVENTF_LEFTUP = 0x0004; //系统消息:左键放开 private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008; //系统消息:右键按下 private const uint MOUSEEVENTF_RIGHTUP = 0x0010; //系统消息:右键放开 private const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020; //系统消息:中间健按下 private const uint MOUSEEVENTF_MIDDLEUP = 0x0040; //系统消息:中间健放开 private const uint MOUSEEVENTF_WHEEL = 0x0800; //系统消息:滚动滚轮 private const uint MOUSEEVENTF_ABSOLUTE = 0x8000; //指定鼠标坐标系统中的一个绝对位置 private const uint KEYEVENTF_EXTENDEDKEY = 0x0001; //一个扩展键 private const uint KEYEVENTF_KEYUP = 0x0002; //模拟松开一个键 private const uint INPUT_MOUSE = 0; //模拟鼠标事件 private const uint INPUT_KEYBOARD = 1; //模拟键盘事件 private static byte[] PreviousBitmapBytes = null; #endregion #region 构造函数 public Monitor() { } #endregion #region Win32API方法包装 [DllImport("user32.dll")] private static extern IntPtr GetDesktopWindow(); [DllImport("gdi32.dll")] private static extern bool BitBlt ( IntPtr hdcDest, //指向目标设备环境的句柄 int nXDest, //指定目标矩形区域克上角的X轴逻辑坐标 int nYDest, //指定目标矩形区域左上角的Y轴逻辑坐标 int nWidth, //指定源和目标矩形区域的逻辑宽度 int nHeight, //指定源和目标矩形区域的逻辑高度 IntPtr hdcSrc, //指向源设备环境句柄 int nXSrc, //指定源矩形区域左上角的X轴逻辑坐标 int nYSrc, //指定源矩形区域左上角的Y轴逻辑坐标 System.Int32 dwRop //指定光栅操作代码。这些代码将定义源矩形区域的颜色数据,如何与目标矩形区域的颜色数据组合以完成最后的颜色 ); [DllImport("user32.dll")] private static extern int GetSystemMetrics(int nIndex); [DllImport("user32.dll")] private static extern uint SendInput ( uint nInputs, ref INPUT input, int cbSize ); [DllImport("user32.dll")] private static extern void SetCursorPos(int x, int y); #endregion #region Win32结构包装 struct MOUSE_INPUT { public uint dx; public uint dy; public uint mouseData; public uint dwFlags; public uint time; public uint dwExtraInfo; } struct KEYBD_INPUT { public ushort wVk; public ushort wScan; public uint dwFlags; public uint time; public uint dwExtraInfo; } [StructLayout(LayoutKind.Explicit)] struct INPUT { [FieldOffset(0)] public uint type; [FieldOffset(4)] public MOUSE_INPUT mi; [FieldOffset(4)] public KEYBD_INPUT ki; } #endregion #region 获得屏幕的大小 public Size GetDesktopBitmapSize() { return new Size(GetSystemMetrics(0), GetSystemMetrics(1)); } #endregion #region 模拟鼠标、键盘操作 public void PressOrReleaseMouseButton(bool Press, bool Left, int X, int Y) { INPUT input = new INPUT(); input.type = INPUT_MOUSE; input.mi.dx = (uint) X; input.mi.dy = (uint) Y; input.mi.mouseData = 0; input.mi.dwFlags = 0; input.mi.time = 0; input.mi.dwExtraInfo = 0; if (Left) { input.mi.dwFlags = Press ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP; } else { input.mi.dwFlags = Press ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP; } SendInput(1, ref input, Marshal.SizeOf(input)); } public void MoveMouse(int x, int y) { SetCursorPos(x, y); } public void SendKeystroke(byte VirtualKeyCode, byte ScanCode, bool KeyDown, bool ExtendedKey)//发送键盘事件 { INPUT input = new INPUT(); input.type = INPUT_KEYBOARD; input.ki.wVk = VirtualKeyCode; input.ki.wScan = ScanCode; input.ki.dwExtraInfo = 0; input.ki.time = 0; if (!KeyDown) { input.ki.dwFlags |= KEYEVENTF_KEYUP; } if (ExtendedKey) { input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY; } SendInput(1, ref input, Marshal.SizeOf(input)); } #endregion #region 获得屏幕截图 private Bitmap GetDesktopBitmap() { Size DesktopBitmapSize = GetDesktopBitmapSize(); Graphics Graphic = Graphics.FromHwnd(GetDesktopWindow());//从窗口的指定句柄创建新的 Graphics 对象 Bitmap MemImage = new Bitmap(DesktopBitmapSize.Width, DesktopBitmapSize.Height, Graphic);//生成图像 Graphics MemGraphic = Graphics.FromImage(MemImage);//从指定的 Image 对象创建新 Graphics 对象 IntPtr dc1 = Graphic.GetHdc();//获取与此 Graphics 对象关联的设备上下文的句柄 IntPtr dc2 = MemGraphic.GetHdc(); BitBlt(dc2, 0, 0, DesktopBitmapSize.Width, DesktopBitmapSize.Height, dc1, 0, 0, 0xCC0020); Graphic.ReleaseHdc(dc1);//释放通过以前对此 Graphics 对象的 GetHdc 方法的调用获得的设备上下文句柄 MemGraphic.ReleaseHdc(dc2); Graphic.Dispose(); MemGraphic.Dispose(); return MemImage; } #endregion #region 判断两个图的二进制数组是否相同 private static bool BitmapsAreEqual(ref byte[] a, ref byte[] b) { bool Result = (a != null && b != null && a.Length == b.Length); if (Result) { for (int i = 0; Result && i < a.Length; i ) { if (a[i] != b[i]) { Result = false; } } } return Result; } #endregion #region 获得图像二进制的数组 public byte[] GetDesktopBitmapBytes() { Bitmap CurrentBitmap = GetDesktopBitmap(); MemoryStream MS = new MemoryStream(); CurrentBitmap.Save(MS, ImageFormat.Jpeg);//将图片写入流 CurrentBitmap.Dispose(); MS.Seek(0, SeekOrigin.Begin); byte[] CurrentBitmapBytes = new byte[MS.Length]; int NumBytesToRead = (int) MS.Length; int NumBytesRead = 0; while (NumBytesToRead > 0) { int n = MS.Read(CurrentBitmapBytes, NumBytesRead, NumBytesToRead); if (n == 0) { break; } NumBytesRead = n; NumBytesToRead -= n; } MS.Close(); byte[] Result = new byte[0]; if (!BitmapsAreEqual(ref CurrentBitmapBytes, ref PreviousBitmapBytes)) { Result = CurrentBitmapBytes; PreviousBitmapBytes = CurrentBitmapBytes; } return Result; } #endregion } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)