C#获取类名为Internet_Explorer_Server控件的内容

为了让大家都能够使用demo,我以IE为测试对象,另外为了突出重点,所以如何获取窗口句柄我就不做演示了(不清楚的童鞋,可以去Google下哈),句柄值我使用spy++获得

      大家可以下载demo(附:spy++)到本地进行调试学习,希望这篇文章对大家有所帮助

知识点:

1.Internet_Explorer_Server

2.msthml

3.API:RegisterWindowMessage

4.API:ObjectFromLresult

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using mshtml;

namespace 获取类名Internet_Explorer_Server控件的内容
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if(String.IsNullOrEmpty(tbHandle.Text))
            {
                MessageBox.Show("请写好窗口类名为:Internet Explorer_Server的窗口句柄");
                return;
            }
            mshtml.IHTMLDocument2 id = GetHtmlDocument(int.Parse(tbHandle.Text));
            textBox1.Text = id.body.innerHTML;
        }      

        class Win32API
        {
            [DllImport("user32", EntryPoint = "SendMessage")]
            public static extern int SendMessage(int hwnd, int wMsg, int wParam, ref int lParam);

            [DllImport("user32", EntryPoint = "RegisterWindowMessage")]
            public static extern int RegisterWindowMessage(string lpString);

            [DllImport("OLEACC.DLL", EntryPoint = "ObjectFromLresult")]
            public static extern int ObjectFromLresult(
                int lResult,
                ref System.Guid riid,
                int wParam,
                [MarshalAs(UnmanagedType.Interface), System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out]ref System.Object ppvObject
                //注意这个函数ObjectFromLresult的声明
            );
        }

        public mshtml.IHTMLDocument2 GetHtmlDocument(int hwnd)
        {
            System.Object domObject = new System.Object();
            int tempInt = 0;
            System.Guid guidIEDocument2 = new Guid();
            int WM_Html_GETOBJECT =Win32API.RegisterWindowMessage("WM_Html_GETOBJECT");//定义一个新的窗口消息
            int W = Win32API.SendMessage(hwnd, WM_Html_GETOBJECT, 0, ref tempInt);//注:第二个参数是RegisterWindowMessage函数的返回值
            int lreturn = Win32API.ObjectFromLresult(W, ref guidIEDocument2, 0, ref domObject);
            mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)domObject;
            return doc;
        }
    }
}

这里,我以IE打开的百度首页为测试对象,效果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值