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

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

最版版本一载地址

很多人问的问题是怎么模拟向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;
            }
        }
时间: 2024-10-02 16:33:00

小熊鼠标键盘模拟工具 使用说明 及核心代码的相关文章

python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为

1.参考 autopy (实践见最后一章节) 用Python制作游戏外挂(上) AutoPy Introduction and Tutorial autopy.mouse.smooth_move(1, 1) 可以实现平滑移动 autopy - API Reference pip install PyUserInput SavinaRoja/PyUserInput [python3.5][PyUserInput]模拟鼠标和键盘模拟 Python-模拟鼠标键盘动作 autoit selenium借助

Delphi下利用WinIo模拟鼠标键盘详解

http://www.cnblogs.com/rogee/archive/2010/09/14/1827248.html 本文最早在编程论坛上发表,文章地址:http://programbbs.com/bbs/view12-17207-1.htm,相关文件可以在上述地址的页面中下载.转载时请注明出处. 前言 一日发现SendInput对某程序居然无效,无奈只好开始研究WinIo.上网查了很多资料,发现关于WinIo模拟鼠标键盘的资料很少,有的也只是支言片语讲的不是很详细,而且大部分都是关于模拟键

Linux 模拟 鼠标 键盘 事件

/************************************************************************ * Linux 模拟 鼠标 键盘 事件 * 说明: * 以前看到有些软件能够控制鼠标移动,键盘操作等功能,总想知道这些到底 * 是怎么做到的,好像是2年前也尝试去做这件事,但那时候对知识的匮乏直接导致 * 无法进行,早上突然想到这件事,于是又搜索了一下,鉴于目前经常接触Linux * 驱动,对这些东西的理解也就很容易. * * 2016-2-27 深

MFC中热键&模拟键盘&模拟鼠标的使用

1. 热键的使用 热键不用了的话一定要卸载,否则下次启动时会被占用. 函数原型 注册函数 BOOL RegisterHotKey( HWND hWnd, // handle to window int id, // hot key identifier UINT fsModifiers, // key-modifier options UINT vk // virtual-key code ); hWnd------窗口句柄: id------热键的标识:(如果是exe 这个标识的范围就在0-4

C#模拟鼠标键盘控制其他窗口(一)

编写程序模拟鼠标和键盘操作可以方便的实现你需要的功能,而不需要对方程序为你开放接口.比如,操作飞信定时发送短信等.我之前开发过飞信耗子,用的是对飞信协议进行抓包,然后分析协议,进而模拟协议的执行,开发出了客户端,与移动服务器进行通信,但是这有一些缺点.如果移动的服务器对接口进行变更,我所编写的客户端也要进行相应的升级.如果服务器的协议进行了更改,甚至个人编写的这种第三方客户端需要重写.而我个人也没有这个时间和精力,或者说没有足够的利益支撑我继续去重构飞信耗子.因此,这款还算优秀的软件,现在就束之

模拟鼠标键盘操作,含硬件模拟技术[转载]

键盘是我们使用计算机的一个很重要的输入设备了,即使在鼠标大行其道的今天,很多程序依然离不开键盘来操作.但是有时候,一些重复性的,很繁琐的键盘操作总会让人疲惫,于是就有了用程序来代替人们按键的方法,这样可以把很多重复性的键盘操作交给程序来模拟,省了很多精力,按键精灵就是这样的一个软件.那么我们怎样才能用VB来写一个程序,达到与按键精灵类似的功能呢?那就让我们来先了解一下windows中响应键盘事件的机制.    当用户按下键盘上的一个键时,键盘内的芯片会检测到这个动作,并把这个信号传送到计算机.如

WinIo驱动级键盘模拟编程

WinIo驱动级键盘模拟编程 前天无聊,翻翻自己的兴趣项目文件夹,发现了这个放下很久的项目!那是大三时候的事了.当时是为了提高我魔兽三的按键速度,用了个叫移花接木的软件,把键盘的键位改了.的确是有所帮助,但这是共享软件,用40次就不能再用了除非注册.于是乎就有了做一个类似的软件出来,在网上搜索了一把发现WinIo模拟按键是最可靠的了,就决定向这方向钻进去了.哎...技术不够,看了很久的技术文章和代码也只是了解了一点,而且那时MFC技术还没到家根本做不出什么能用的东西来...后来听个兄弟说:反汇编

多电脑同局域网,同一套鼠标键盘,跨屏操作利器。

妈妈再也不担心我拿错键盘鼠标了. Input Director是款Windows下的一套鼠标键盘控制多台电脑工具,使用户可以通过连接在一台计算机上的一套键盘鼠标,轻松控制多台电脑.对于经常在不同电脑之间经常切换的用户来说,非常实用.我也不用为了测试一个软件,在本本和测试机之间来回切换.不过为了使用方便,最好是能将各个系统的显示器排成一排.  除了支持多个系统的切换和操作,Input Director同样支持“共享”剪贴板,在一个计算机上复制数据,可以切换到其他计算机上粘贴使用.不过Windows

Python - selenium_WebDriver 鼠标键盘事件

from selenium import webdriver #引入ActionChains类 提供了鼠标的操作方法 from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys from ReadTxt_demo import readTxt import time #鼠标键盘事件 ''' ActionChains 常用方法 per