• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#API学习小例子

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1。游戏自动登录,MousePosition.X获得系统鼠标位置的X坐标

     第一种方法:指定文本框的坐标,适用于窗口最大化按钮不能用的,如QQ游戏

      int x=500;

      int y=300;

      SendKeys.SendWait("274040246");   //输入游戏ID         
      SetCursorPos(x, y); //设置鼠标位置,x,y为账号框相对屏幕的位置                
      mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
      mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
      System.Threading.Thread.Sleep(500);//设置暂停时间,不然账号和密码会发在一个框中

      SendKeys.SendWait("{Tab}");//发送Tab
      SendKeys.SendWait("1111111111111"); //输入游戏密码
      SendKeys.SendWait("{ENTER}");//输入回车键

      第二种方法:

      IntPtr myIntPtr = FindWindow(null, "QQ游戏");
      IntPtr handle1 = FindWindowEx(myIntPtr, IntPtr.Zero, "ComboBox", null);//找账号输入框句柄
      IntPtr handle2 = FindWindowEx(myIntPtr, handle1, "Edit", null);//找密码输入框句柄
      if (handle1 != null && handle2 != null)
      {
              InputStr(handle1, "274040246"); //输入游戏ID
              InputStr(handle2, "1111111111111"); //输入游戏密码                    
              PostMessage(handle2, WM_KEYDOWN, 13, 0); //输入ENTER
              PostMessage(handle2, WM_KEYUP, 13, 0);
        }

        /// <summary>
        /// 发送一个字符串
        /// </summary>
        /// <param name="myIntPtr">窗口句柄</param>
        /// <param name="Input">字符串</param>
        public void InputStr(IntPtr myIntPtr, string Input)
        {
            byte[] ch = (ASCIIEncoding.ASCII.GetBytes(Input));
            for (int i = 0; i < ch.Length; i++)
            {
                SendMessage(myIntPtr, WM_CHAR, ch[i], 0);
            }
        }

 

       第三种:具有一定的通用性

       IntPtr myIntPtr = FindWindow(null, "QQ游戏");

       //获取当前活动窗口的线程句柄
       IntPtr threadId1 = GetCurrentThreadId();
       //获得指定句柄的线程句柄
       IntPtr threadId2 = GetWindowThreadProcessId(myIntPtr, IntPtr.Zero);

       //将两个线程的输入队列联系起来,只有这样,GetFocus函数才能获得其它线程中的焦点窗口   
       AttachThreadInput(threadId2, threadId1, 1);
        //得到当前的具有输入焦点的子窗口(控件)的句柄
        IntPtr getAccountTextBox = GetFocus();
        //将连到一起的两个线程的输入队列分离   
        AttachThreadInput(threadId2, threadId1, 0);

        if (getAccountTextBox != null)
        {
               Rectangle rc = new Rectangle();
               GetWindowRect(getAccountTextBox.ToInt32(), ref rc);
               InputStr(getAccountTextBox, "274040246"); //输入游戏ID
               SetCursorPos(rc.X + 3, rc.Y + 32); //设置鼠标位置             
               mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
               mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
               System.Threading.Thread.Sleep(200);//如果账号和密码出现在一个文本框中,则此时间需要设置长
               //再次查找拥有输入焦点的文本框句柄
               AttachThreadInput(threadId2, threadId1, 1);
               IntPtr getPwdTextBox = GetFocus();
               AttachThreadInput(threadId2, threadId1, 0);
               InputStr(getPwdTextBox, "1111111111111111"); //输入游戏密码
               PostMessage(getPwdTextBox, WM_KEYDOWN, 13, 0); //输入ENTER
               PostMessage(getPwdTextBox, WM_KEYUP, 13, 0);
         }       

2。魔兽自动登录一种实现,通过设置窗口大小及位置来做

       //查找窗口
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        //可将最小化窗口还原
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        //将指定窗口激活
        [DllImport("user32.dll ")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        //设置鼠标位置
        [DllImport("user32.dll")]
        private static extern bool SetCursorPos(int X, int Y);
        //设置窗口位置和大小
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
        //模拟鼠标
        [DllImport("user32.dll")]
        private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        private const int SW_RESTORE = 9; //将窗口还原的参数
        private const uint SWP_SHOWWINDOW = 0x0040; //设置窗口位置的参数

        private const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
        private const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起

        IntPtr findPtr = FindWindow(null, "World of Warcraft");//World of Warcraft为游戏在任务栏的名称
        ShowWindow(findPtr, SW_RESTORE); //将窗口还原
        SetForegroundWindow(findPtr);//将指定窗口激活
         if (findPtr.ToInt32() != 0)
         {
                 SymmetricMethod sm = new SymmetricMethod();
                 SetWindowPos(findPtr, (IntPtr)(-1), 0, 0, 700, 550, SWP_SHOWWINDOW);
                 System.Threading.Thread.Sleep(500);
                 SetCursorPos(300, 300); //设置鼠标位置
                  mouse_event(0x0002, 0, 0, 0, 0); //模拟鼠标按下操作
                  mouse_event(0x0004, 0, 0, 0, 0); //模拟鼠标放开操作
                  System.Threading.Thread.Sleep(500);
                  SendKeys.Send(lvInfo.SelectedItems[0].SubItems[1].Text);   //模拟键盘输入游戏ID
                  SendKeys.Send("{TAB}"); //模拟键盘输入TAB
                  SendKeys.Send(sm.Decrypto(lvInfo.SelectedItems[0].SubItems[2].Text)); //模拟键盘输入游戏密码                       
                   SendKeys.Send("{ENTER}"); //模拟键盘输入ENTER
           }
          else
           {
                    MessageBox.Show("魔兽世界还没有打开!");
            }

3。判断当前系统活动窗口是否是指定的窗口

     //找到系统当前活动的窗口的句柄
      [DllImport("user32.dll")]
      private static extern IntPtr GetForegroundWindow();

      //根据任务栏应用程序显示的名称找相应窗口的句柄
      [DllImport("User32.dll", EntryPoint = "FindWindow")]
      private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

      //获取指定句柄窗口的标题
      [DllImport("User32.dll")]
      static extern int GetWindowText(IntPtr handle, StringBuilder text, int MaxLen);

      IntPtr findPtr = FindWindow(null, "QQ游戏");//QQ游戏为在任务栏的名称
      StringBuilder text1 = new StringBuilder(50);//按要求找到的进程的标题
      StringBuilder text2 = new StringBuilder(50);//当前活动进程的标题
      GetWindowText(GetForegroundWindow(), text1, 50);
      GetWindowText(findPtr, text2, 50);

      if(text1.Equals(text2))

                  MessageBox.Show("当前活动窗口是指定窗口");

     else

             MessageBox.Show("当前活动窗口不是指定窗口");

4。设置指定窗口选中(激活)

      private const int SW_RESTORE = 9;

      [DllImport("user32.dll ")]
      private static extern bool SetForegroundWindow(IntPtr hWnd);

      [DllImport("user32.dll")]
      private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

      IntPtr findPtr = FindWindow(null, "AION Client");//AION Client为游戏在任务栏的名称

      if (findPtr.ToInt32() != 0)
      {
             ShowWindow(findPtr, SW_RESTORE); //将窗口还原,如果不用此方法,缩小的窗口不能激活
             SetForegroundWindow(findPtr);//将指定的窗口选中(激活)

      }

    

5。获取指定窗口句柄相对屏幕中的位置    

      [DllImport("User32.dll", EntryPoint = "FindWindow")]
      private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
      [DllImport("User32.dll")]
      private static extern bool ClientToScreen(IntPtr hwnd, ref Point lpPoint);       

      IntPtr findPtr = FindWindow(null, "QQ游戏");

      Point p = new Point (0);
      ClientToScreen(findPtr, ref p);
      MessageBox.Show(p.X+","+p.Y);

6。判断指定窗口是否是最大化

      [DllImport("User32.dll", EntryPoint = "FindWindow")]
      private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  

      [DllImport("user32.dll")]
      private static extern bool IsZoomed(IntPtr hWnd);

      IntPtr findPtr = FindWindow(null, "QQ游戏");

      if (IsZoomed(findPtr))

          MessageBox.Show("最大化");

      else

          MessageBox.Show("最小化");

备注:程序中用到的

    //设置鼠标位置
        [DllImport("user32.dll")]
        private static extern bool SetCursorPos(int X, int Y);

        //模拟鼠标
        [DllImport("user32.dll")]
        private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        /// <summary>
        /// 查找句柄
        /// </summary> 
        [DllImport("User32.DLL")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        //发送按键消息必用PostMessage,SendMessage有时会不起作用
        [DllImport("user32.dll", EntryPoint = "PostMessage", SetLastError = true)]
        private extern static bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        //获取指定句柄的相对屏幕的位置和矩形区域
        [DllImport("user32")]
        public static extern int GetWindowRect(int hwnd, ref Rectangle lpRect);

        [DllImport("kernel32")]
        public static extern IntPtr GetCurrentThreadId();

        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

        [DllImport("user32.dll")]
        private static extern IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, int fAttach);

        [DllImport("user32.dll")]
        private static extern IntPtr GetFocus();

        private const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下参数
        private const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起参数

        //SendMessage参数
        private const int WM_KEYDOWN = 0X100;
        private const int WM_KEYUP = 0X101;
        private const int WM_SYSCHAR = 0X106;
        private const int WM_SYSKEYUP = 0X105;
        private const int WM_SYSKEYDOWN = 0X104;
        private const int WM_CHAR = 0X102;


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#工具:BootstrapWPFStyle,Bootstrap风格的WPF样式发布时间:2022-07-10
下一篇:
通过类名动态创建对象(C#)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap