c#虚拟键盘、虚拟鼠标以及窗口查找

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

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;

namespace SimulateMouse
{
    
public partial class DemoForm : Form
    
{
        [StructLayout(LayoutKind.Sequential)]
        
struct NativeRECT
        
{
            
public int left;
            
public int top;
            
public int right;
            
public int bottom;
        }


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


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

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

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

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

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

        
public DemoForm()
        
{
            InitializeComponent();
        }


               
private void button1_Click(object sender, EventArgs e)
        
{
            NativeRECT rect;

            IntPtr ptrTaskbar 
= FindWindow("#32770","QQ用户登录");
            
if (ptrTaskbar == IntPtr.Zero)
            
{
                MessageBox.Show(
"No taskbar found.");
                
return;
            }


            IntPtr ptrStartBtn 
= FindWindowEx(new HandleRef(this, ptrTaskbar), new HandleRef(this, IntPtr.Zero), "ComboBox"null);
            
if (ptrStartBtn == IntPtr.Zero)
            
{
                MessageBox.Show(
"No qq号码框 found.");
                
return;
            }


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

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

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


           

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

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

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

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

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

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


       mystr 
= "mypassword";
       
for (int i = 0; i < mystr.Length; i++)
       
{
          keybd_event((
byte)mystr[i], 000);
       }


        }

    }

}


说明:(
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即可。

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

上面的代码表示 ctl
+a

 
原文地址:https://www.cnblogs.com/kokoliu/p/712193.html