我来自唐朝
	
	
		
		
 
 我想编制一个qq自动登陆程序,确发现C#中对很多api没有封装,只有使用平台调用了。其中用到窗体查找和虚拟键盘、虚拟鼠标等函数。具体代码如下:
我想编制一个qq自动登陆程序,确发现C#中对很多api没有封装,只有使用平台调用了。其中用到窗体查找和虚拟键盘、虚拟鼠标等函数。具体代码如下:

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

 namespace SimulateMouse
namespace SimulateMouse


 {
{
 public partial class DemoForm : Form
    public partial class DemoForm : Form

 
     {
{
 [StructLayout(LayoutKind.Sequential)]
        [StructLayout(LayoutKind.Sequential)]
 struct NativeRECT
        struct NativeRECT

 
         {
{
 public int left;
            public int left;
 public int top;
            public int top;
 public int right;
            public int right;
 public int bottom;
            public int bottom;
 }
        }

 [Flags]
        [Flags]
 enum MouseEventFlag : uint
        enum MouseEventFlag : uint

 
         {
{
 Move        = 0x0001,
            Move        = 0x0001,
 LeftDown    = 0x0002,
            LeftDown    = 0x0002,
 LeftUp      = 0x0004,
            LeftUp      = 0x0004,
 RightDown   = 0x0008,
            RightDown   = 0x0008,
 RightUp     = 0x0010,
            RightUp     = 0x0010,
 MiddleDown  = 0x0020,
            MiddleDown  = 0x0020,
 MiddleUp    = 0x0040,
            MiddleUp    = 0x0040,
 XDown       = 0x0080,
            XDown       = 0x0080,
 XUp         = 0x0100,
            XUp         = 0x0100,
 Wheel       = 0x0800,
            Wheel       = 0x0800,
 VirtualDesk = 0x4000,
            VirtualDesk = 0x4000,
 Absolute    = 0x8000
            Absolute    = 0x8000
 }
        }

 [DllImport("user32.dll")]
        [DllImport("user32.dll")]
 static extern bool SetCursorPos(int X, int Y);
        static extern bool SetCursorPos(int X, int Y);  

 [DllImport("user32.dll")]
        [DllImport("user32.dll")]
 static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
        static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

 [DllImport("user32.dll")]
        [DllImport("user32.dll")]
 static extern IntPtr FindWindow(string strClass, string strWindow);
        static extern IntPtr FindWindow(string strClass, string strWindow);

 [DllImport("user32.dll")]
        [DllImport("user32.dll")]
 static extern IntPtr FindWindowEx(HandleRef hwndParent, HandleRef hwndChildAfter, string strClass, string strWindow);
        static extern IntPtr FindWindowEx(HandleRef hwndParent, HandleRef hwndChildAfter, string strClass, string strWindow);

 [DllImport("user32.dll")]
        [DllImport("user32.dll")]
 static extern bool GetWindowRect(HandleRef hwnd, out NativeRECT rect);
        static extern bool GetWindowRect(HandleRef hwnd, out NativeRECT rect);
 [DllImport("user32.dll")]
        [DllImport("user32.dll")]
 static extern void keybd_event(byte bVk,byte bScan,uint dwFlags,uint dwExtraInfo);
        static extern void keybd_event(byte bVk,byte bScan,uint dwFlags,uint dwExtraInfo);
 private Point endPosition;
        private Point endPosition;

 public DemoForm()
        public DemoForm()

 
         {
{
 InitializeComponent();
            InitializeComponent();
 }
        }

 private void button1_Click(object sender, EventArgs e)
               private void button1_Click(object sender, EventArgs e)

 
         {
{
 NativeRECT rect;
            NativeRECT rect;

 IntPtr ptrTaskbar = FindWindow("#32770","QQ用户登录");
            IntPtr ptrTaskbar = FindWindow("#32770","QQ用户登录");
 if (ptrTaskbar == IntPtr.Zero)
            if (ptrTaskbar == IntPtr.Zero)

 
             {
{
 MessageBox.Show("No taskbar found.");
                MessageBox.Show("No taskbar found.");
 return;
                return;
 }
            }

 IntPtr ptrStartBtn = FindWindowEx(new HandleRef(this, ptrTaskbar), new HandleRef(this, IntPtr.Zero), "ComboBox", null);
            IntPtr ptrStartBtn = FindWindowEx(new HandleRef(this, ptrTaskbar), new HandleRef(this, IntPtr.Zero), "ComboBox", null);
 if (ptrStartBtn == IntPtr.Zero)
            if (ptrStartBtn == IntPtr.Zero)

 
             {
{
 MessageBox.Show("No qq号码框 found.");
                MessageBox.Show("No qq号码框 found.");
 return;
                return;
 }
            }

 GetWindowRect(new HandleRef(this, ptrStartBtn), out rect);
            GetWindowRect(new HandleRef(this, ptrStartBtn), out rect);

 endPosition.X = (rect.left + rect.right) / 2;
            endPosition.X = (rect.left + rect.right) / 2;
 endPosition.Y = (rect.top + rect.bottom) / 2;
            endPosition.Y = (rect.top + rect.bottom) / 2;

 SetCursorPos(endPosition.X, endPosition.Y);
            SetCursorPos(endPosition.X, endPosition.Y);
 mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
 mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
 mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
 mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);


 
           

 System.IO.StreamWriter tw = new System.IO.StreamWriter(@"d:/aa.txt");
           System.IO.StreamWriter tw = new System.IO.StreamWriter(@"d:/aa.txt");
 tw.Write("49593533");
           tw.Write("49593533");
 tw.Close();
           tw.Close();

 System.IO.StreamReader tr = new System.IO.StreamReader(@"d:/aa.txt");
           System.IO.StreamReader tr = new System.IO.StreamReader(@"d:/aa.txt");
 String mystr = tr.ReadToEnd();
           String mystr = tr.ReadToEnd();
 tr.Close();
           tr.Close();

 for(int i=0;i<mystr.Length;i++)
           for(int i=0;i<mystr.Length;i++)
 keybd_event((byte)mystr[i], 0, 0, 0);
           keybd_event((byte)mystr[i], 0, 0, 0);

 IntPtr ptrPassWord = FindWindowEx(new HandleRef(this, ptrTaskbar), new HandleRef(this, IntPtr.Zero), "#32770", null);
       IntPtr ptrPassWord = FindWindowEx(new HandleRef(this, ptrTaskbar), new HandleRef(this, IntPtr.Zero), "#32770", null);
 GetWindowRect(new HandleRef(this, ptrPassWord), out rect);
       GetWindowRect(new HandleRef(this, ptrPassWord), out rect);

 endPosition.X = (rect.left + rect.right) / 2;
       endPosition.X = (rect.left + rect.right) / 2;
 endPosition.Y = (rect.top + rect.bottom) / 2;
       endPosition.Y = (rect.top + rect.bottom) / 2;

 SetCursorPos(endPosition.X, endPosition.Y);
       SetCursorPos(endPosition.X, endPosition.Y);
 mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
       mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
 mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
       mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
 mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
       mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
 mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
       mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);


 mystr = "mypassword";
       mystr = "mypassword";
 for (int i = 0; i < mystr.Length; i++)
       for (int i = 0; i < mystr.Length; i++)

 
        {
{
 keybd_event((byte)mystr[i], 0, 0, 0);
          keybd_event((byte)mystr[i], 0, 0, 0);
 }
       }

 }
        }
 }
    }
 }
}

 说明:(1)所有虚拟键盘码可以到http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp去查找。
说明:(1)所有虚拟键盘码可以到http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp去查找。

 (2)首先介绍一下Keybd_event函数。Keybd_event能触发一个按键事件,也就是说回产生一个WM_KEYDOWN或WM_KEYUP消息。当然也可以用产生这两个消息来模拟按键,但是没有直接用这个函数方便。Keybd_event共有四个参数,第一个为按键的虚拟键值,如回车键为vk_return, tab键为vk_tab。第二个参数为扫描码,一般不用设置,用0代替就行第三个参数为选项标志,如果为keydown则置0即可,如果为keyup则设成“KEYEVENTF_KEYUP”或者2,第四个参数一般也是置0即可。
(2)首先介绍一下Keybd_event函数。Keybd_event能触发一个按键事件,也就是说回产生一个WM_KEYDOWN或WM_KEYUP消息。当然也可以用产生这两个消息来模拟按键,但是没有直接用这个函数方便。Keybd_event共有四个参数,第一个为按键的虚拟键值,如回车键为vk_return, tab键为vk_tab。第二个参数为扫描码,一般不用设置,用0代替就行第三个参数为选项标志,如果为keydown则置0即可,如果为keyup则设成“KEYEVENTF_KEYUP”或者2,第四个参数一般也是置0即可。

 keybd_event(VK_CONTROL, (BYTE)0, 0 ,0);
keybd_event(VK_CONTROL, (BYTE)0, 0 ,0);
 keybd_event('A',(BYTE)0, 0 ,0); //此处可以用 'A', (BYTE)65, 用'a'不起作用.
keybd_event('A',(BYTE)0, 0 ,0); //此处可以用 'A', (BYTE)65, 用'a'不起作用.
 keybd_event('A', (BYTE)0, KEYEVENTF_KEYUP,0);
keybd_event('A', (BYTE)0, KEYEVENTF_KEYUP,0);
 keybd_event(VK_CONTROL, (BYTE)0, KEYEVENTF_KEYUP,0);
keybd_event(VK_CONTROL, (BYTE)0, KEYEVENTF_KEYUP,0);

 上面的代码表示 ctl+a
上面的代码表示 ctl+a

 
 
