我想編制一個(gè)qq自動(dòng)登陸程序,確發(fā)現(xiàn)C#中對(duì)很多api沒有封裝,只有使用平臺(tái)調(diào)用了。其中用到窗體查找和虛擬鍵盤、虛擬鼠標(biāo)等函數(shù)。具體代碼如下:
![](http://image.360doc.com/DownloadImg/2008/7/9/65004_1412050_1.gif)
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;
![](http://image.360doc.com/DownloadImg/2008/7/9/65004_1412050_1.gif)
namespace SimulateMouse
![](http://pubimage.360doc.com/wz/default.gif) ![](http://pubimage.360doc.com/wz/default.gif) {
public partial class DemoForm : Form
![](http://pubimage.360doc.com/wz/default.gif) {
[StructLayout(LayoutKind.Sequential)]
struct NativeRECT
![](http://pubimage.360doc.com/wz/default.gif) {
public int left;
public int top;
public int right;
public int bottom;
}
![](http://pubimage.360doc.com/wz/default.gif)
[Flags]
enum MouseEventFlag : uint
![](http://pubimage.360doc.com/wz/default.gif) {
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
![](http://pubimage.360doc.com/wz/default.gif)
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
![](http://pubimage.360doc.com/wz/default.gif)
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
![](http://pubimage.360doc.com/wz/default.gif)
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string strClass, string strWindow);
![](http://pubimage.360doc.com/wz/default.gif)
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(HandleRef hwndParent, HandleRef hwndChildAfter, string strClass, string strWindow);
![](http://pubimage.360doc.com/wz/default.gif)
[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;
![](http://pubimage.360doc.com/wz/default.gif)
public DemoForm()
![](http://pubimage.360doc.com/wz/default.gif) {
InitializeComponent();
}
![](http://pubimage.360doc.com/wz/default.gif)
private void button1_Click(object sender, EventArgs e)
![](http://pubimage.360doc.com/wz/default.gif) {
NativeRECT rect;
![](http://pubimage.360doc.com/wz/default.gif)
IntPtr ptrTaskbar = FindWindow("#32770","QQ用戶登錄");
if (ptrTaskbar == IntPtr.Zero)
![](http://pubimage.360doc.com/wz/default.gif) {
MessageBox.Show("No taskbar found.");
return;
}
![](http://pubimage.360doc.com/wz/default.gif)
IntPtr ptrStartBtn = FindWindowEx(new HandleRef(this, ptrTaskbar), new HandleRef(this, IntPtr.Zero), "ComboBox", null);
if (ptrStartBtn == IntPtr.Zero)
![](http://pubimage.360doc.com/wz/default.gif) {
MessageBox.Show("No qq號(hào)碼框 found.");
return;
}
![](http://pubimage.360doc.com/wz/default.gif)
GetWindowRect(new HandleRef(this, ptrStartBtn), out rect);
![](http://pubimage.360doc.com/wz/default.gif)
endPosition.X = (rect.left + rect.right) / 2;
endPosition.Y = (rect.top + rect.bottom) / 2;
![](http://pubimage.360doc.com/wz/default.gif)
SetCursorPos(endPosition.X, endPosition.Y);
mouse_event(MouseEventFlag.LeftDown, 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.LeftUp, 0, 0, 0, UIntPtr.Zero);
![](http://pubimage.360doc.com/wz/default.gif)
![](http://pubimage.360doc.com/wz/default.gif)
![](http://pubimage.360doc.com/wz/default.gif)
System.IO.StreamWriter tw = new System.IO.StreamWriter(@"d:/aa.txt");
tw.Write("49593533");
tw.Close();
![](http://pubimage.360doc.com/wz/default.gif)
System.IO.StreamReader tr = new System.IO.StreamReader(@"d:/aa.txt");
String mystr = tr.ReadToEnd();
tr.Close();
![](http://pubimage.360doc.com/wz/default.gif)
for(int i=0;i<mystr.Length;i++)
keybd_event((byte)mystr[i], 0, 0, 0);
![](http://pubimage.360doc.com/wz/default.gif)
IntPtr ptrPassWord = FindWindowEx(new HandleRef(this, ptrTaskbar), new HandleRef(this, IntPtr.Zero), "#32770", null);
GetWindowRect(new HandleRef(this, ptrPassWord), out rect);
![](http://pubimage.360doc.com/wz/default.gif)
endPosition.X = (rect.left + rect.right) / 2;
endPosition.Y = (rect.top + rect.bottom) / 2;
![](http://pubimage.360doc.com/wz/default.gif)
SetCursorPos(endPosition.X, endPosition.Y);
mouse_event(MouseEventFlag.LeftDown, 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.LeftUp, 0, 0, 0, UIntPtr.Zero);
![](http://pubimage.360doc.com/wz/default.gif)
![](http://pubimage.360doc.com/wz/default.gif)
mystr = "mypassword";
for (int i = 0; i < mystr.Length; i++)
![](http://pubimage.360doc.com/wz/default.gif) {
keybd_event((byte)mystr[i], 0, 0, 0);
}
![](http://pubimage.360doc.com/wz/default.gif)
}
}
}
![](http://pubimage.360doc.com/wz/default.gif)
說明:(1)所有虛擬鍵盤碼可以到http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp去查找。
![](http://pubimage.360doc.com/wz/default.gif)
(2)首先介紹一下Keybd_event函數(shù)。Keybd_event能觸發(fā)一個(gè)按鍵事件,也就是說回產(chǎn)生一個(gè)WM_KEYDOWN或WM_KEYUP消息。當(dāng)然也可以用產(chǎn)生這兩個(gè)消息來模擬按鍵,但是沒有直接用這個(gè)函數(shù)方便。Keybd_event共有四個(gè)參數(shù),第一個(gè)為按鍵的虛擬鍵值,如回車鍵為vk_return, tab鍵為vk_tab。第二個(gè)參數(shù)為掃描碼,一般不用設(shè)置,用0代替就行第三個(gè)參數(shù)為選項(xiàng)標(biāo)志,如果為keydown則置0即可,如果為keyup則設(shè)成“KEYEVENTF_KEYUP”或者2,第四個(gè)參數(shù)一般也是置0即可。
![](http://pubimage.360doc.com/wz/default.gif)
keybd_event(VK_CONTROL, (BYTE)0, 0 ,0);
keybd_event('A',(BYTE)0, 0 ,0); //此處可以用 'A', (BYTE)65, 用'a'不起作用.
keybd_event('A', (BYTE)0, KEYEVENTF_KEYUP,0);
keybd_event(VK_CONTROL, (BYTE)0, KEYEVENTF_KEYUP,0);
![](http://pubimage.360doc.com/wz/default.gif)
上面的代碼表示 ctl+a
|