码迷,mamicode.com
首页 > 其他好文 > 详细

小熊鼠标键盘模拟工具 使用说明 及核心代码

时间:2015-01-11 14:54:16      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:

旧版本有些问题,请下载新版本

最版版本一载地址


很多人问的问题是怎么模拟向QQ群发送信息的 

其实标题就说了是模拟鼠标,按键。

具体哪些按键呢,可能有些人习惯鼠标操作不太清楚,其实也很简单。enter键 next键。配合鼠标使用就可以了。

其实就是这么简单了。

核心代码:

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using Feng.Office;
using System.Text;
using Feng.Utils;
namespace Feng.Forms
{
    public class WindowInfo
    {
        public List<WindowInfo> WindowsList = new List<WindowInfo>();
        public void SetWindowCaption(int hwnd, string lpString)
        {
            UnsafeNativeMethods.SetWindowText(hwnd, lpString);
        }
        public WindowInfo()
        { 
        } 
        public string Title { get; set; }
        public string ProcessName { get; set; }
        public int ProcessID { get; set; }
        public string ProcessFile { get; set; }
        public IntPtr MainHandle { get; set; }
        public bool ProcessResponding { get; set; }
        public bool Visible { get;set; }
        public int Handler { get; set; }
        public string ClassName { get; set; }
        public int Left { get; set; }
        public int Top { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public Rectangle Rect
        {
            get
            {
                return new Rectangle(this.Left, this.Top, this.Width, this.Height);
        } }

        public bool IsWindows()
        {
            return UnsafeNativeMethods.IsWindow(this.Handler);
        }
 
        public List<WindowInfo> GetProcessMainWindowInfo()
        {
            System.Diagnostics.Process pro = null;
            List<WindowInfo> list = new List<WindowInfo>();
            foreach (Process pro_loopVariable in System.Diagnostics.Process.GetProcesses())
            {
                pro = pro_loopVariable;
                if (pro.ProcessName.ToLower() == "system" | pro.Id == 0)
                {
                    break; // TODO: might not be correct. Was : Exit For
                }
                if (!string.IsNullOrEmpty(pro.MainWindowTitle.Trim()))
                {
                    WindowInfo li = new WindowInfo();
                    li.Title = (pro.MainWindowTitle);
                    li.ProcessID = pro.Id;
                    li.ProcessResponding = pro.Responding;

                    li.MainHandle = (pro.MainWindowHandle);
                    li.ProcessName = (pro.ProcessName);
                    li.ProcessFile = (pro.MainModule.FileName);
                    list.Add(li); 
                }
            }
            return list;
        }
        public static List<WindowInfo> GetChildWindows(int handle)
        { 
            int res = Feng.Utils.UnsafeNativeMethods.EnumChildWindows(handle, CallBackEnumWindows, 0);
            return listchild;
        }
        public void CloseWindow()
        {
            Feng.Utils.UnsafeNativeMethods.PostMessage(this.MainHandle, UnsafeNativeMethods.WM_QUIT, 0, 0);
        }

        public static List<WindowInfo> GetWindowInfo(int handle,
            string classname, 
            int minwidth, 
            int minheight,
            string appname)
        {
            listchild.Clear();
            _classname = classname;
            _minwidth = minwidth;
            _minheight = minheight;
            _appname = appname;
            UnsafeNativeMethods.GetDesktopWindow();
            IntPtr _WindowsStation = UnsafeNativeMethods.GetProcessWindowStation();
            IntPtr _ThreadID = new IntPtr(UnsafeNativeMethods.GetCurrentThreadId());
            IntPtr _DeskTop = UnsafeNativeMethods.GetThreadDesktop(_ThreadID);
            IntPtr _HwinstaUser = UnsafeNativeMethods.OpenWindowStation("WinSta0", false, 33554432);
            if (_HwinstaUser == IntPtr.Zero)
            {
                UnsafeNativeMethods.RpcRevertToSelf();
               
            }
            UnsafeNativeMethods.SetProcessWindowStation(_HwinstaUser);
            IntPtr _HdeskUser = UnsafeNativeMethods.OpenDesktop("Default", 0, false, 33554432);
            UnsafeNativeMethods.RpcRevertToSelf();
            if (_HdeskUser == IntPtr.Zero)
            {
                UnsafeNativeMethods.SetProcessWindowStation(_WindowsStation);
                UnsafeNativeMethods.CloseWindowStation(_HwinstaUser);
                
            }
            UnsafeNativeMethods.SetThreadDesktop(_HdeskUser);
            int res = Feng.Utils.UnsafeNativeMethods.EnumChildWindows(handle, EnumWindowInfoCallBackByClassName, 0);
            IntPtr _GuiThreadId = _ThreadID;
            _GuiThreadId = IntPtr.Zero;
            UnsafeNativeMethods.SetThreadDesktop(_DeskTop);
            UnsafeNativeMethods.SetProcessWindowStation(_WindowsStation);
            UnsafeNativeMethods.CloseDesktop(_HdeskUser);
            UnsafeNativeMethods.CloseWindowStation(_HwinstaUser);
            return listchild;
        }

        public static List<WindowInfo> GetWindowInfo(int handle,
          string classname,
            string title,
          int minwidth,
          int minheight,
          string appname)
        {
            listchild.Clear();
            _classname = classname;
            _minwidth = minwidth;
            _minheight = minheight;
            _appname = appname;
            _title = title;
            UnsafeNativeMethods.GetDesktopWindow();
            IntPtr _WindowsStation = UnsafeNativeMethods.GetProcessWindowStation();
            IntPtr _ThreadID = new IntPtr(UnsafeNativeMethods.GetCurrentThreadId());
            IntPtr _DeskTop = UnsafeNativeMethods.GetThreadDesktop(_ThreadID);
            IntPtr _HwinstaUser = UnsafeNativeMethods.OpenWindowStation("WinSta0", false, 33554432);
            if (_HwinstaUser == IntPtr.Zero)
            {
                UnsafeNativeMethods.RpcRevertToSelf();

            }
            UnsafeNativeMethods.SetProcessWindowStation(_HwinstaUser);
            IntPtr _HdeskUser = UnsafeNativeMethods.OpenDesktop("Default", 0, false, 33554432);
            UnsafeNativeMethods.RpcRevertToSelf();
            if (_HdeskUser == IntPtr.Zero)
            {
                UnsafeNativeMethods.SetProcessWindowStation(_WindowsStation);
                UnsafeNativeMethods.CloseWindowStation(_HwinstaUser);

            }
            UnsafeNativeMethods.SetThreadDesktop(_HdeskUser);
            int res = Feng.Utils.UnsafeNativeMethods.EnumChildWindows(handle, EnumWindowInfoCallBackByClassNameAndTitle, 0);
            IntPtr _GuiThreadId = _ThreadID;
            _GuiThreadId = IntPtr.Zero;
            UnsafeNativeMethods.SetThreadDesktop(_DeskTop);
            UnsafeNativeMethods.SetProcessWindowStation(_WindowsStation);
            UnsafeNativeMethods.CloseDesktop(_HdeskUser);
            UnsafeNativeMethods.CloseWindowStation(_HwinstaUser);
            return listchild;
        }
        private static string _classname = string.Empty;
        private static int _minwidth; 
        private static int _minheight;
        private static string _appname = string.Empty;
        private static string _title = string.Empty;
        public static WindowInfo CreateWindowInfo(string titletemp, System.Diagnostics.Process pro, int hwnd, string classname, Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT wp)
        {
            WindowInfo li = new WindowInfo();
            li.Title = titletemp;
            li.ProcessID = pro.Id;
            li.ProcessResponding = pro.Responding;
            li.Handler = hwnd;
            li.MainHandle = (pro.MainWindowHandle);
            li.ProcessName = (pro.ProcessName);
            li.ClassName = classname;
            li.Width = wp.rcNormalPosition.Right - wp.rcNormalPosition.Left;
            li.Height = wp.rcNormalPosition.Bottom - wp.rcNormalPosition.Top;
            li.Left = wp.rcNormalPosition.Left;
            li.Top = wp.rcNormalPosition.Top;
            try
            {
                li.ProcessFile = (pro.MainModule.FileName);
            }
            catch (Exception)
            {

            }
            return li;
        }
        public string GetTitle()
        {
            StringBuilder title = new StringBuilder(255);
            Feng.Utils.UnsafeNativeMethods.GetWindowText(this.Handler, title, 255);
            return title.ToString();
        }
        private static bool EnumWindowInfoCallBackByClassName(int hwnd, int lParam)
        {
            StringBuilder title = new StringBuilder(255);
            int ThreadIDR = 0;
            int ProcessIDR = 0;
            int titleall = 0;
            string titletemp = null;
            Feng.Utils.UnsafeNativeMethods.enumshow lstyle = 0;

            titleall = Feng.Utils.UnsafeNativeMethods.GetWindowText(hwnd, title, 255);
            string classname = string.Empty;
            StringBuilder sbclassname = new StringBuilder(256);
            int res = Feng.Utils.UnsafeNativeMethods.GetClassName(hwnd, sbclassname, 128);
            classname = sbclassname.ToString().Trim('\0');
            if (classname == _classname)
            {
                titletemp = title.ToString();
                Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT wp = default(Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT);

                if (Feng.Utils.UnsafeNativeMethods.GetWindowPlacement(hwnd, ref wp))
                {
                    int width=wp.rcNormalPosition.Right - wp.rcNormalPosition.Left;
                    int height=wp.rcNormalPosition .Bottom -wp.rcNormalPosition.Top;
                    if ((width > _minwidth)
                        && (height > _minheight))
                    {
                        ThreadIDR = Feng.Utils.UnsafeNativeMethods.GetWindowThreadProcessId(hwnd, ref ProcessIDR);
                        lstyle = (Feng.Utils.UnsafeNativeMethods.enumshow)Feng.Utils.UnsafeNativeMethods.GetWindowLong(hwnd, Feng.Utils.UnsafeNativeMethods.enumshow.GWL_STYLE);
                        Process pro = Process.GetProcessById(ProcessIDR);
                        if (pro.ProcessName == _appname || string.IsNullOrEmpty(_appname))
                        {
                            WindowInfo li = CreateWindowInfo(titletemp, pro, hwnd, classname, wp);
                            listchild.Add(li);
                        }
                    }
                }
            }
            return true;
        }
        private static bool EnumWindowInfoCallBackByClassNameAndTitle(int hwnd, int lParam)
        {
            StringBuilder title = new StringBuilder(255);
            int ThreadIDR = 0;
            int ProcessIDR = 0;
            int titleall = 0;
            string titletemp = null;
            Feng.Utils.UnsafeNativeMethods.enumshow lstyle = 0;

            titleall = Feng.Utils.UnsafeNativeMethods.GetWindowText(hwnd, title, 255);
            string classname = string.Empty;
            StringBuilder sbclassname = new StringBuilder(256);
            int res = Feng.Utils.UnsafeNativeMethods.GetClassName(hwnd, sbclassname, 128);
            classname = sbclassname.ToString().Trim('\0');
            if (classname == _classname && title.ToString() == _title)
            {
                titletemp = title.ToString();
                Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT wp = default(Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT);

                if (Feng.Utils.UnsafeNativeMethods.GetWindowPlacement(hwnd, ref wp))
                {
                    int width = wp.rcNormalPosition.Right - wp.rcNormalPosition.Left;
                    int height = wp.rcNormalPosition.Bottom - wp.rcNormalPosition.Top;
                    if ((width > _minwidth)
                        && (height > _minheight))
                    {
                        ThreadIDR = Feng.Utils.UnsafeNativeMethods.GetWindowThreadProcessId(hwnd, ref ProcessIDR);
                        lstyle = (Feng.Utils.UnsafeNativeMethods.enumshow)Feng.Utils.UnsafeNativeMethods.GetWindowLong(hwnd, Feng.Utils.UnsafeNativeMethods.enumshow.GWL_STYLE);
                        Process pro = Process.GetProcessById(ProcessIDR);
                        if (pro.ProcessName == _appname || string.IsNullOrEmpty(_appname))
                        {
                            WindowInfo li = CreateWindowInfo(titletemp, pro, hwnd, classname, wp);
                            listchild.Add(li);
                        }
                    }
                }
            }
            return true;
        }

        public static Feng.Utils.UnsafeNativeMethods.CallBack CallBackEnumWindows = new UnsafeNativeMethods.CallBack(EnumWindowInfoCallBack);
        private static List<WindowInfo> listchild = new List<WindowInfo>();

        public static bool EnumWindowInfoCallBack(int hwnd, int lParam)
        {
            StringBuilder title = new StringBuilder(255);
            int ThreadIDR = 0;
            int ProcessIDR = 0; 
            string titletemp = null;
            Feng.Utils.UnsafeNativeMethods.enumshow lstyle = 0;
            Feng.Utils.UnsafeNativeMethods.GetWindowText(hwnd, title, 255);
 
            string classname = string.Empty;
            StringBuilder sbclassname = new StringBuilder(256);
            int res = Feng.Utils.UnsafeNativeMethods.GetClassName(hwnd, sbclassname, 128);
            classname = sbclassname.ToString();
            titletemp = title.ToString();
            Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT wp = default(Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT);

            if (Feng.Utils.UnsafeNativeMethods.GetWindowPlacement(hwnd, ref wp) == true)
            {
                if ((wp.rcNormalPosition.Right - wp.rcNormalPosition.Left > 4))
                {
                    ThreadIDR = Feng.Utils.UnsafeNativeMethods.GetWindowThreadProcessId(hwnd, ref ProcessIDR);
                    lstyle = (Feng.Utils.UnsafeNativeMethods.enumshow)Feng.Utils.UnsafeNativeMethods.GetWindowLong(hwnd, Feng.Utils.UnsafeNativeMethods.enumshow.GWL_STYLE);
                    Process pro = Process.GetProcessById(ProcessIDR);
                    WindowInfo li = CreateWindowInfo(titletemp, pro, hwnd, classname, wp);
                    listchild.Add(li);
                }
            }
            return true;
        }
        /// <summary>
        /// 开启窗体 使用后用 System.Windows.Forms.Application.Run(_Form);来启动窗体
        /// </summary>
        /// <param name="s">日志</param>
        public static void ServiceForm()
        {
            try
            {
                UnsafeNativeMethods.GetDesktopWindow();
                IntPtr _WindowsStation = UnsafeNativeMethods.GetProcessWindowStation();
                IntPtr _ThreadID = new IntPtr(UnsafeNativeMethods.GetCurrentThreadId());
                IntPtr _DeskTop = UnsafeNativeMethods.GetThreadDesktop(_ThreadID);
                IntPtr _HwinstaUser = UnsafeNativeMethods.OpenWindowStation("WinSta0", false, 33554432);
                if (_HwinstaUser == IntPtr.Zero)
                {
                    UnsafeNativeMethods.RpcRevertToSelf();
                    return;
                }
                UnsafeNativeMethods.SetProcessWindowStation(_HwinstaUser);
                IntPtr _HdeskUser = UnsafeNativeMethods.OpenDesktop("Default", 0, false, 33554432);
                UnsafeNativeMethods.RpcRevertToSelf();
                if (_HdeskUser == IntPtr.Zero)
                {
                    UnsafeNativeMethods.SetProcessWindowStation(_WindowsStation);
                    UnsafeNativeMethods.CloseWindowStation(_HwinstaUser);
                    return;
                }
                UnsafeNativeMethods.SetThreadDesktop(_HdeskUser);
                IntPtr _GuiThreadId = _ThreadID;
                _GuiThreadId = IntPtr.Zero;
                UnsafeNativeMethods.SetThreadDesktop(_DeskTop);
                UnsafeNativeMethods.SetProcessWindowStation(_WindowsStation);
                UnsafeNativeMethods.CloseDesktop(_HdeskUser);
                UnsafeNativeMethods.CloseWindowStation(_HwinstaUser);
            }
            catch (Exception ex)
            { 
            }
        }

        public static List<WindowInfo> GetActiveAllWindows(int desktopHandle)
        {
             
            int lnghand = 0;
            StringBuilder title = new StringBuilder(255);
            int ThreadIDR = 0;
            int ProcessIDR = 0;
            int titleall = 0;
            string titletemp = null;
            Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT wp = default(Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT);
 
            Feng.Utils.UnsafeNativeMethods.enumshow lstyle = 0;
            List<WindowInfo> list = new List<WindowInfo>();
            desktopHandle = Feng.Utils.UnsafeNativeMethods.GetDesktopWindow();
            lnghand = Feng.Utils.UnsafeNativeMethods.GetWindow(desktopHandle, UnsafeNativeMethods.GW_CHILD);
            wp.Length = Marshal.SizeOf(wp);
            List<int> listptr = new List<int>();
            while (lnghand != 0)
            {
                lnghand = Feng.Utils.UnsafeNativeMethods.GetWindow(lnghand, UnsafeNativeMethods.GW_HWNDNEXT);
                titleall = Feng.Utils.UnsafeNativeMethods.GetWindowText(lnghand, title, 255);
                string classname = string.Empty;
                StringBuilder sbclassname = new StringBuilder(256);
                int res = Feng.Utils.UnsafeNativeMethods.GetClassName(lnghand, sbclassname, 128);
                classname = sbclassname.ToString();
                titletemp = title.ToString();
                if (listptr.Contains(lnghand))
                {
                    continue;
                }
                listptr.Add(lnghand);
                if (Feng.Utils.UnsafeNativeMethods.GetWindowPlacement(lnghand, ref wp) == true)
                {
                    if ((wp.rcNormalPosition.Right - wp.rcNormalPosition.Left > 4))
                    {
                        ThreadIDR = Feng.Utils.UnsafeNativeMethods.GetWindowThreadProcessId(lnghand, ref ProcessIDR);
                        lstyle = (Feng.Utils.UnsafeNativeMethods.enumshow)Feng.Utils.UnsafeNativeMethods.GetWindowLong(lnghand, Feng.Utils.UnsafeNativeMethods.enumshow.GWL_STYLE);
                        Process pro = Process.GetProcessById(ProcessIDR);
                        WindowInfo li = CreateWindowInfo(titletemp, pro, lnghand, classname, wp);
                        list.Add(li); 
                    } 
                }
            }
            return list;
        }
        public static string ClearNullstr(string str)
        {
            str = str.Trim('\0');
            return str;
        }
        public bool SetWindowPlacementmenu(int lhandle, Feng.Utils.UnsafeNativeMethods.enumplacement nCmdShow)
        {
            Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT windowplacement  = default(Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT);
            windowplacement.Length = Marshal.SizeOf(windowplacement);
            if (Feng.Utils.UnsafeNativeMethods.GetWindowPlacement(lhandle, ref windowplacement) == false)
            {
                return false;
            }
            else
            {
                var _with6 = windowplacement;
                _with6.showCmd = (int)nCmdShow;
            }
            if (Feng.Utils.UnsafeNativeMethods.SetWindowPlacement(lhandle, ref windowplacement) == false)
            {
                return false;
            }
            else
            {
                return true;
            } 
        }
        public bool SetWindowPosmenu(int lhandle, Feng.Utils.UnsafeNativeMethods.enumwindpose PostSet)
        {
            Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT WINDOWPLACEMENT = default(Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT);
            WINDOWPLACEMENT.Length = Marshal.SizeOf(WINDOWPLACEMENT);
            if (Feng.Utils.UnsafeNativeMethods.GetWindowPlacement(lhandle, ref WINDOWPLACEMENT) == false)
            {
                return false;
            }
            else
            {
            }
            if (Feng.Utils.UnsafeNativeMethods.SetWindowPos(lhandle, (int)PostSet, WINDOWPLACEMENT.rcNormalPosition.Left, WINDOWPLACEMENT.rcNormalPosition.Top, WINDOWPLACEMENT.rcNormalPosition.Right - WINDOWPLACEMENT.rcNormalPosition.Left, WINDOWPLACEMENT.rcNormalPosition.Bottom - WINDOWPLACEMENT.rcNormalPosition.Top, WINDOWPLACEMENT.flags) == false)
            {
                return false;
            }
            else
            {
                return true;
            } 

        }
        public bool SendMessageToCloseWindow(int ProcID)
        {
            Process pro = null;
            pro = System.Diagnostics.Process.GetProcessById(ProcID);
            if (pro.CloseMainWindow() == true)
            {
                return true;
            }
            else
            {
                return false;
            } 
        }
        public bool KillProcess(int ProcID)
        {
            Process pro = null;
            Process.EnterDebugMode();
            pro = System.Diagnostics.Process.GetProcessById(ProcID);
            pro.Kill();
            Process.LeaveDebugMode();
            return false;
        }
        public override string ToString()
        {
            return string.Format("Title={0},ProcessName={1},ProcessID={2},Handler={3},ClassName={4},Rect={5},ProcessFile={6}",
                this.Title, this.ProcessName, this.ProcessID, this.Handler, this.ClassName, this.Rect, this.ProcessFile);
        }
        public bool SetPositionAndTopmostShow(Point pt)
        {
            if (SetWindowPosmenu(this.Handler, UnsafeNativeMethods.enumwindpose.HWND_TOP))
            {
                if (SetPosition(pt))
                {
                    return true;
                }
            }
            return false;
        }
        public bool SetPosition(Point pt)
        {
            Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT windowplacement = default(Feng.Utils.UnsafeNativeMethods.WINDOWPLACEMENT);
            windowplacement.Length = Marshal.SizeOf(windowplacement);

            if (!Feng.Utils.UnsafeNativeMethods.GetWindowPlacement(this.Handler, ref windowplacement))
            {
                return false;
            }
            windowplacement.rcNormalPosition.Left = pt.X;
            windowplacement.rcNormalPosition.Top = pt.Y;
            if (Feng.Utils.UnsafeNativeMethods.SetWindowPlacement(this.Handler, ref windowplacement))
            {
                return true;
            } 
            return false;
        }
        public bool Focus()
        {
            return UnsafeNativeMethods.SetForegroundWindow(this.Handler) > 0;
        }
    }
}


核心代码就是使用一些系统API。上面 【SetForegroundWindow】【GetWindowPlacement】这些大家经常用应该知道。

上述代码并没有在这个鼠标键盘拟模上用到。上述代码的主要作用是可以指定对哪些窗口进行模拟鼠标键盘操作。比如每天做的一些重复的动作。

由于想操作某个软件并获取相应的资料的目的不同,适合二次开发。(比如上面的模拟向QQ发信息,有时候打开别的界面会把QQ界面拦住,所以使用这个可以把窗口置前并激活)

下面是鼠标键盘的代码。主要就是用了Sendkeys类。太简单了。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using Feng.Office;

namespace Feng.Utils
{
    public abstract class SysEvents
    {
        public abstract string Title { get; }  
        public virtual void Excute()
        {

        }
        public abstract void Write(Feng.IO.BufferWriter bw);
        public abstract bool Read(string title, Feng.IO.BufferReader read);
 
    }

  //  以下是   SendKeys   的一些特殊键代码表。   
  //键   代码     
  //BACKSPACE   {BACKSPACE}、{BS}   或   {BKSP}     
  //BREAK   {BREAK}     
  //CAPS   LOCK   {CAPSLOCK}     
  //DEL   或   DELETE   {DELETE}   或   {DEL}     
  //DOWN   ARROW(下箭头键)   {DOWN}     
  //END   {END}     
  //ENTER   {ENTER}   或   ~     
  //ESC   {ESC}     
  //HELP   {HELP}     
  //HOME   {HOME}     
  //INS   或   INSERT   {INSERT}   或   {INS}     
  //LEFT   ARROW(左箭头键)   {LEFT}     
  //NUM   LOCK   {NUMLOCK}     
  //PAGE   DOWN   {PGDN}     
  //PAGE   UP   {PGUP}     
  //PRINT   SCREEN   {PRTSC}(保留,以备将来使用)     
  //RIGHT   ARROW(右箭头键)   {RIGHT}     
  //SCROLL   LOCK   {SCROLLLOCK}     
  //TAB   {TAB}     
  //UP   ARROW(上箭头键)   {UP}     
  //F1   {F1}     
  //F2   {F2}     
  //F3   {F3}     
  //F4   {F4}     
  //F5   {F5}     
  //F6   {F6}     
  //F7   {F7}     
  //F8   {F8}     
  //F9   {F9}     
  //F10   {F10}     
  //F11   {F11}     
  //F12   {F12}     
  //F13   {F13}     
  //F14   {F14}     
  //F15   {F15}     
  //F16   {F16}     
  //数字键盘加号   {ADD}     
  //数字键盘减号   {SUBTRACT}     
  //数字键盘乘号   {MULTIPLY}     
  //数字键盘除号   {DIVIDE}     
    public class KeySysEvents : SysEvents
    {
        public string Text { get; set; }
        public override string Title
        {
            get
            {
                return "KEY";
            }
        }
        public override void Excute()
        {
            SendKeys.Send(Text);
            SendKeys.Flush();

        }
        public override string ToString()
        {
            return string.Format("发送键:{0}", Text);
        }
        public override void Write(IO.BufferWriter bw)
        { 
            bw.Write(Text);
        }
        public override bool Read(string title, IO.BufferReader read)
        {
            if (title == this.Title)
            {
                this.Text = read.ReadString();
                return true;
            }
            return false;
        }
    }
    public class VersonSysEvents : SysEvents
    {
        int i = 0;
        public override string Title
        {
            get
            {
                return "VERSON";
            }
        }
        public override void Excute()
        {
            i++;
            SendKeys.Send("小熊鼠标键盘辅助工具V1.0.1.1 自动发送" + i.ToString());
            SendKeys.Flush();

        }
        public override string ToString()
        {
            return string.Format("版本信息 {0}", "");
        }
        public override void Write(IO.BufferWriter bw)
        {
             
        }
        public override bool Read(string title, IO.BufferReader read)
        { 
            return false;
        }
    }
    public class SpaceEvents : SysEvents
    {
        public int Interval { get; set; }
        public override string Title
        {
            get
            {
                return "SPACE";
            }
        }
        public override void Excute()
        {
            for (int i = 0; i < Interval * 10; i++)
            {
                Application.DoEvents();
                System.Threading.Thread.Sleep(100 * Interval);
            }
            SendKeys.Flush();

        }
        public override string ToString()
        {
            return string.Format("间隔:{0}秒", Interval);
        }
        public override void Write(IO.BufferWriter bw)
        {
            bw.Write(Interval);
        }
        public override bool Read(string title, IO.BufferReader read)
        {
            if (title == this.Title)
            {
                this.Interval = read.ReadInt32();
                return true;
            }
            return false;
        }
    }


    public class RandomEvents : SysEvents
    {
        public int RandomType { get; set; }
        public int lastvalue = 0;
        public DateTime lastdatetime = DateTime.Now;
        public override string Title
        {
            get
            {
                return "RANDOM";
            }
        }
        public string Text {
            get {
                switch (RandomType)
                {
                    case 1:
                        return lastvalue++.ToString();
                    case 2:
                        return new Random(DateTime.Now.Millisecond).Next(0, 100).ToString();
                    case 3:
                        return DateTime.Now.ToString("HH:mm:ss");
                    case 4:
                        return Guid.NewGuid().ToString().Substring (0,3);
                    default:
                        return lastvalue++.ToString(); 
                }
            }
        }
        public override void Excute()
        {
            SendKeys.Send(Text);
            SendKeys.Flush(); 
        }
        public override string ToString()
        {
            return string.Format("随机:{0}", RandomType);
        }
        public override void Write(IO.BufferWriter bw)
        {
            bw.Write(RandomType);
        }
        public override bool Read(string title, IO.BufferReader read)
        {
            if (title == this.Title)
            {
                this.RandomType = read.ReadInt32();
                return true;
            }
            return false;
        }
    }

    public class MouseClickSysEvents : SysEvents
    {
        public Point Point { get; set; }
        public Size OffSize { get; set; }
        public override string Title
        {
            get
            {
                return "Click";
            }
        }
        public override void Excute()
        {
            UnsafeNativeMethods.MouseMove(Point.X, Point.Y);
            System.Threading.Thread.Sleep(300);
            UnsafeNativeMethods.MouseClick(Point.X, Point.Y);
            System.Threading.Thread.Sleep(300);
        }
        public override string ToString()
        {
            return string.Format("单击:{0},OX={1},OY={2}", Point, OffSize.Width, OffSize.Height);
        }
        public override void Write(IO.BufferWriter bw)
        { 
            bw.Write(Point.X);
            bw.Write(Point.Y);

            bw.Write(OffSize.Width);
            bw.Write(OffSize.Height);
        }
        public override bool Read(string title, IO.BufferReader read)
        {
            if (title == this.Title)
            { 
                int x = read.ReadInt32();
                int y = read.ReadInt32();
                Point = new Point(x, y);
                int width = read.ReadInt32();
                int height = read.ReadInt32();
                OffSize = new Size(width, height);
                return true;
            }
            return false;
        }
    }

    public class MouseDoubleClickSysEvents : SysEvents
    {
        public   Point Point { get; set; }
        public Size OffSize { get; set; }
        public override string Title
        {
            get
            {
                return "DoubleClick";
            }
        }
        public override void Excute()
        {
            UnsafeNativeMethods.MouseMove(Point.X, Point.Y);
            System.Threading.Thread.Sleep(300);
            UnsafeNativeMethods.MouseClick(Point.X, Point.Y);
            System.Threading.Thread.Sleep(10);
            UnsafeNativeMethods.MouseClick(Point.X, Point.Y);
            System.Threading.Thread.Sleep(10);
        }
        public override string ToString()
        {
            return string.Format("双击:{0},OX={1},OY={2}", Point, OffSize.Width, OffSize.Height);
        }
        public override void Write(IO.BufferWriter bw)
        { 
            bw.Write(Point.X);
            bw.Write(Point.Y);

            bw.Write(OffSize.Width);
            bw.Write(OffSize.Height);
        }
        public override bool Read(string title, IO.BufferReader read)
        {
            if (title == this.Title)
            {
                int x = read.ReadInt32();
                int y = read.ReadInt32();
                Point = new Point(x, y);
                int width = read.ReadInt32();
                int height = read.ReadInt32();
                OffSize = new Size(width, height);
                return true;
            }
            return false;
        }
    }

    public class MouseWheelUpSysEvents : SysEvents
    {
        public   Point Point { get; set; }
        public override string Title
        {
            get
            {
                return "MouseWheelUp";
            }
        }
        public override void Excute()
        {
            UnsafeNativeMethods.MouseWheelUP(Point.X, Point.Y); 
            System.Threading.Thread.Sleep(300);
        }
        public override string ToString()
        {
            return string.Format("{0}", "向上滚动鼠标");
        }
        public override void Write(IO.BufferWriter bw)
        { 
            bw.Write(Point.X);
            bw.Write(Point.Y);
             
        }
        public override bool Read(string title, IO.BufferReader read)
        {
            if (title == this.Title)
            {
                int x = read.ReadInt32();
                int y = read.ReadInt32();
                Point = new Point(x, y); 
                return true;
            }
            return false;
        }
    }

    public class MouseWheelDownSysEvents : SysEvents
    {
        public   Point Point { get; set; }
        public override string Title
        {
            get
            {
                return "MouseWheelDOWN";
            }
        }
        public override void Excute()
        {
            UnsafeNativeMethods.MouseWheelDOWN(Point.X, Point.Y);
            System.Threading.Thread.Sleep(300);
        }
        public override string ToString()
        {
            return string.Format("{0}", "向下滚动鼠标");
        }
        public override void Write(IO.BufferWriter bw)
        { 
            bw.Write(Point.X);
            bw.Write(Point.Y);
             
        }
        public override bool Read(string title, IO.BufferReader read)
        {
            if (title == this.Title)
            {
                int x = read.ReadInt32();
                int y = read.ReadInt32();
                Point = new Point(x, y);
                return true;
            }
            return false;
        }
    }
}


使用方法如下:

timer1_Tick
为timer的事件

  void timer1_Tick(object sender, EventArgs e)
        {
            if (lck)
                return; 
            try
            {
                if (icount >= this.txtEventCount.Value)
                {
                    this.chk开始动作.Checked = false;
                    return;
                }
                icount++;
                lck = true;
                if (System.Windows.Forms.Control.ModifierKeys == Keys.Control)
                {
                    System.Threading.Thread.Sleep(3000);
                    return; 
                }
 
                foreach (SysEvents p in listevents)
                {
                    p.Excute();
                    System.Threading.Thread.Sleep(300);
                }
                System.Threading.Thread.Sleep(300);
                SendKeys.Flush();
            }
            catch (Exception ex)
            {
                Feng.Utils.ExceptionHelper.ShowError(ex);
            }
            finally
            {
                lck = false;
            }
        }



小熊鼠标键盘模拟工具 使用说明 及核心代码

标签:

原文地址:http://blog.csdn.net/zanfeng/article/details/42610751

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!