www.pudn.com > sharewindows.rar > Nblh_Link_DeskTop.cs, change:2016-01-07,size:8015b


using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace NBLH_LINK 
{ 
    public class Nblh_Link_DeskTop 
    { 
        private struct MOUSE_INPUT 
        { 
            public uint dx; 
 
            public uint dy; 
 
            public uint mouseData; 
 
            public uint dwFlags; 
 
            public uint time; 
 
            public uint dwExtraInfo; 
        } 
 
        private struct KEYBD_INPUT 
        { 
            public ushort wVk; 
 
            public ushort wScan; 
 
            public uint dwFlags; 
 
            public uint time; 
 
            public uint dwExtraInfo; 
        } 
 
        [StructLayout(LayoutKind.Explicit)] 
        private struct INPUT 
        { 
            [FieldOffset(0)] 
            public uint type; 
 
            [FieldOffset(4)] 
            public Nblh_Link_DeskTop.MOUSE_INPUT mi; 
 
            [FieldOffset(4)] 
            public Nblh_Link_DeskTop.KEYBD_INPUT ki; 
        } 
 
        private const uint MOUSEEVENTF_MOVE = 1u; 
 
        private const uint MOUSEEVENTF_LEFTDOWN = 2u; 
 
        private const uint MOUSEEVENTF_LEFTUP = 4u; 
 
        private const uint MOUSEEVENTF_RIGHTDOWN = 8u; 
 
        private const uint MOUSEEVENTF_RIGHTUP = 16u; 
 
        private const uint MOUSEEVENTF_MIDDLEDOWN = 32u; 
 
        private const uint MOUSEEVENTF_MIDDLEUP = 64u; 
 
        private const uint MOUSEEVENTF_WHEEL = 2048u; 
 
        private const uint MOUSEEVENTF_ABSOLUTE = 32768u; 
 
        private const uint KEYEVENTF_EXTENDEDKEY = 1u; 
 
        private const uint KEYEVENTF_KEYUP = 2u; 
 
        private const uint INPUT_MOUSE = 0u; 
 
        private const uint INPUT_KEYBOARD = 1u; 
 
        private static byte[] PreviousBitmapBytes = null; 
 
        [DllImport("user32.dll")] 
        private static extern IntPtr GetDesktopWindow(); 
 
        [DllImport("gdi32.dll")] 
        private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop); 
 
        [DllImport("user32.dll")] 
        private static extern int GetSystemMetrics(int nIndex); 
 
        [DllImport("user32.dll")] 
        private static extern uint SendInput(uint nInputs, ref Nblh_Link_DeskTop.INPUT input, int cbSize); 
 
        [DllImport("user32.dll")] 
        private static extern void SetCursorPos(int x, int y); 
 
        /// <summary> 
        /// 获取屏幕大小Size 
        /// </summary> 
        /// <returns></returns> 
        public Size GetDesktopBitmapSize() 
        { 
            return new Size(Nblh_Link_DeskTop.GetSystemMetrics(0), Nblh_Link_DeskTop.GetSystemMetrics(1)); 
        } 
 
        /// <summary> 
        /// 获得屏幕截图 
        /// </summary> 
        /// <returns></returns> 
        private Bitmap GetDesktopBitmap() 
        { 
            Size DesktopBitmapSize = this.GetDesktopBitmapSize(); 
            Graphics Graphic = Graphics.FromHwnd(Nblh_Link_DeskTop.GetDesktopWindow()); 
            Bitmap MemImage = new Bitmap(DesktopBitmapSize.Width, DesktopBitmapSize.Height, Graphic); 
            Graphics MemGraphic = Graphics.FromImage(MemImage); 
            IntPtr dc = Graphic.GetHdc(); 
            IntPtr dc2 = MemGraphic.GetHdc(); 
            Nblh_Link_DeskTop.BitBlt(dc2, 0, 0, DesktopBitmapSize.Width, DesktopBitmapSize.Height, dc, 0, 0, 13369376); 
            Graphic.ReleaseHdc(dc); 
            MemGraphic.ReleaseHdc(dc2); 
            Graphic.Dispose(); 
            MemGraphic.Dispose(); 
            return MemImage; 
        } 
 
        /// <summary> 
        ///  判断两个图的二进制数组是否相同 
        /// </summary> 
        /// <param name="a"></param> 
        /// <param name="b"></param> 
        /// <returns></returns> 
        private static bool BitmapsAreEqual(ref byte[] a, ref byte[] b) 
        { 
            bool Result = a != null && b != null && a.Length == b.Length; 
            if (Result) 
            { 
                int i = 0; 
                while (Result && i < a.Length) 
                { 
                    if (a[i] != b[i]) 
                    { 
                        Result = false; 
                    } 
                    i++; 
                } 
            } 
            return Result; 
        } 
 
        /// <summary> 
        /// 获得图像二进制的数组 
        /// </summary> 
        /// <returns></returns> 
        public byte[] GetDesktopBitmapBytes() 
        { 
            Bitmap CurrentBitmap = this.GetDesktopBitmap(); 
            MemoryStream MS = new MemoryStream(); 
            CurrentBitmap.Save(MS, ImageFormat.Jpeg); 
            CurrentBitmap.Dispose(); 
            MS.Seek(0L, SeekOrigin.Begin); 
            byte[] CurrentBitmapBytes = new byte[MS.Length]; 
            int NumBytesToRead = (int)MS.Length; 
            int NumBytesRead = 0; 
            while (NumBytesToRead > 0) 
            { 
                int i = MS.Read(CurrentBitmapBytes, NumBytesRead, NumBytesToRead); 
                if (i == 0) 
                { 
                    break; 
                } 
                NumBytesRead += i; 
                NumBytesToRead -= i; 
            } 
            MS.Close(); 
            byte[] Result = new byte[0]; 
            if (!Nblh_Link_DeskTop.BitmapsAreEqual(ref CurrentBitmapBytes, ref Nblh_Link_DeskTop.PreviousBitmapBytes)) 
            { 
                Result = CurrentBitmapBytes; 
                Nblh_Link_DeskTop.PreviousBitmapBytes = CurrentBitmapBytes; 
            } 
            return Result; 
        } 
 
        /// <summary> 
        /// 模拟鼠标、键盘操作 
        /// </summary> 
        /// <param name="Press"></param> 
        /// <param name="Left"></param> 
        /// <param name="X"></param> 
        /// <param name="Y"></param> 
        public void PressOrReleaseMouseButton(bool Press, bool Left, int X, int Y) 
        { 
            Nblh_Link_DeskTop.INPUT input = default(Nblh_Link_DeskTop.INPUT); 
            input.type = 0u; 
            input.mi.dx = (uint)X; 
            input.mi.dy = (uint)Y; 
            input.mi.mouseData = 0u; 
            input.mi.dwFlags = 0u; 
            input.mi.time = 0u; 
            input.mi.dwExtraInfo = 0u; 
            if (Left) 
            { 
                input.mi.dwFlags = (Press ? 2u : 4u); 
            } 
            else 
            { 
                input.mi.dwFlags = (Press ? 8u : 16u); 
            } 
            Nblh_Link_DeskTop.SendInput(1u, ref input, Marshal.SizeOf(input)); 
        } 
 
        /// <summary> 
        /// 移动鼠标坐标 
        /// </summary> 
        /// <param name="x"></param> 
        /// <param name="y"></param> 
        public void MoveMouse(int x, int y) 
        { 
            Nblh_Link_DeskTop.SetCursorPos(x, y); 
        } 
 
        /// <summary> 
        /// 发送键盘事件 
        /// </summary> 
        /// <param name="VirtualKeyCode"></param> 
        /// <param name="ScanCode"></param> 
        /// <param name="KeyDown"></param> 
        /// <param name="ExtendedKey"></param> 
        public void SendKeystroke(byte VirtualKeyCode, byte ScanCode, bool KeyDown, bool ExtendedKey) 
        { 
            Nblh_Link_DeskTop.INPUT input = default(Nblh_Link_DeskTop.INPUT); 
            input.type = 1u; 
            input.ki.wVk = (ushort)VirtualKeyCode; 
            input.ki.wScan = (ushort)ScanCode; 
            input.ki.dwExtraInfo = 0u; 
            input.ki.time = 0u; 
            if (!KeyDown) 
            { 
                input.ki.dwFlags = (input.ki.dwFlags | 2u); 
            } 
            if (ExtendedKey) 
            { 
                input.ki.dwFlags = (input.ki.dwFlags | 1u); 
            } 
            Nblh_Link_DeskTop.SendInput(1u, ref input, Marshal.SizeOf(input)); 
        } 
    } 
}