获取基于Internet Explorer_Server的聊天窗口内容
假设在得到窗体中控件的句柄(通过SPY++)的前提下,如果是像文本框这种控件,只要用SendMessage就可得到文本了,但是对于聊天记录窗口却行不通(返回空值),因为那其实是一个内置浏览器Internet Explorer_Server
经查询,可用以下方式获取:
先引用:mshtml
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using mshtml; [Flags] public enum SendMessageTimeoutFlags : uint { SMTO_NORMAL = 0x0, SMTO_BLOCK = 0x1, SMTO_ABORTIFHUNG = 0x2, SMTO_NOTIMEOUTIFNOTHUNG = 0x8 } public class WinAPI { [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern uint RegisterWindowMessage(string lpString); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags flags, uint timeout, out UIntPtr result); [DllImport("oleacc.dll", PreserveSig = false)] [return: MarshalAs(UnmanagedType.Interface)] private static extern object ObjectFromLresult(UIntPtr lResult, [MarshalAs(UnmanagedType.LPStruct)] Guid refiid, IntPtr wParam); public IHTMLDocument2 GetHtmlDocument(IntPtr hWnd) { uint lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT"); UIntPtr lRes; SendMessageTimeout(hWnd, lMsg, IntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out lRes); IHTMLDocument2 htmlDoc = null; if (lRes != UIntPtr.Zero) { htmlDoc = (IHTMLDocument2)ObjectFromLresult(lRes, typeof(IHTMLDocument2).GUID, IntPtr.Zero); } return htmlDoc; } public string GetWebText(IntPtr hWnd) { IHTMLDocument2 htmlDoc = GetHtmlDocument(hWnd); if (htmlDoc != null) { return htmlDoc.body.innerHTML; } else { return string.Empty; } } }