QQ登陸功能的實現2
由於看到園子里有朋友說需要講解和剖析實現的步驟,前面的QQ登陸實現只有代碼,所以這篇補上
1. 分析
2. 上面我們詳細分析了登陸過程的操作步驟需要的信息,現在來實現
1). 首先運行QQ,使用 Process pro = Process.Start(qqPath);這個方法來運行QQ
2). 然后我們需要找到登陸窗體的句柄,需要EnumDesktopWindows這個API來實現
函數原型BOOL WINAPI EnumDesktopWindows(__in_opt HDESK hDesktop,
__in WNDENUMPROC lpfn,
__in LPARAM lParam
);
大家都知道.net調用API比較煩,那么我們需要一個工具來簡化我們的操作,在這里推薦2個工具,pinvoke.net(http://www.pinvoke.net/)和P/Invoke Interop Assistant(http://clrinterop.codeplex.com/)后者是一個開源項目,個人來說喜歡前者一些,工具很簡單一看就會,就不講解了,接上我們需要EnumDesktopWindows來找到登陸窗體,這個api 的聲明如下
public delegate bool EnumDesktopWindowsDelegate(IntPtr hWnd, uint lParam);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)][return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDesktopWindowsDelegate lpEnumCallbackFunction, IntPtr lParam);
使用這個API,然后根據pid就可以找到登陸窗口了3). 找到登陸窗口之后我們需要找到登陸窗口里面的控件,這過程我們需要使用另一個API就是EnumChildWindows,函數原型:
BOOL EnumChildWindows(
HWND hWndParent,
WNDENUMPROC lpEnumFunc,
LPARAM lParam);
.net API聲明
[DllImport("user32.Dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr parentHandle, EnumChildWindowsDelegate callback, IntPtr lParam);
public delegate bool EnumChildWindowsDelegate(IntPtr hwnd, IntPtr lParam);
找控件方法如下
public static List<IntPtr> FindControl(IntPtr hwnd, string className, string title = null)
{
List<IntPtr> controls = new List<IntPtr>();
IntPtr handle = IntPtr.Zero;
while (true)
{
IntPtr tmp = handle;
handle = WinAPIHelper.FindWindowEx(hwnd, tmp, className, title);
if (handle != IntPtr.Zero)
{
controls.Add(handle);
}
else
break;
}
return controls;
}
4). 經過上面我們找到2個子控件,現在需要為2個子控件設置值,對於一般的文本框我們可以用SETText消息來實現,對於特殊的輸入框,我們只能模擬按鍵實現
首先設置QQ號碼WinAPIHelper.SendMessage(cons[0], WinAPIHelper.WM_SETTEXT, IntPtr.Zero, new StringBuilder(mainQQ)),其中cons[0]為輸入框的句柄,這樣就輸入了QQ號碼,輸入密碼需要以下步驟
A)窗體置頂,b)窗體獲取焦點,c)輸入控件獲取焦點,d)發送按鍵
WinAPIHelper.BringWindowToTop(hLogonWnd),使用BringWindowToTop這個API來實現置頂,WinAPIHelper.SetForegroundWindow(hLogonWnd)使用SetForegroundWindow這個API來實現獲取焦點,WinAPIHelper.SendMessage(cons[0], WinAPIHelper.WM_SETFOCUS, 0x001a0494, 0)使用SETFocus來設置控件焦點,SendKeys.SendWait(mainQQPwd)這個類來實現密碼輸入模擬
5). 經過上面步驟我們實現信息的輸入,現在我們需要點解按鈕
通過spy我們的到了按鈕的坐標信息,關於點擊的實現參照http://www.cnblogs.com/Rolends/archive/2012/04/18/2454964.html
6). 登陸操作過程完成,接下來就等待登陸結果,我們需要使用EnumDesktopWindows不斷的來獲取窗體進行辨別
word里寫還是不行,發出來亂完了


