zoukankan      html  css  js  c++  java
  • 获取基于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;
                }
            }
    }
  • 相关阅读:
    Java序列化与反序列化
    Java中的反射机制
    Java中读文件操作
    Java中写文件操作
    判断单链表是否有环
    Java继承与组合
    Java类初始化
    堆排序
    希尔排序
    插入排序
  • 原文地址:https://www.cnblogs.com/siso/p/3692486.html
Copyright © 2011-2022 走看看