操作IE

/// 
        /// 获取IE句柄
        /// 
        /// 
        private bool GetHandleOfBrowser()
        {
            IEHandle = FindWindowA("IEFrame", null);
            if (IEHandle == 0)
                return false;
            else
                return true;
        }
 
 /// 
        /// 根据元素信息获取坐标
        /// 
        /// 元素ID
        /// 元素内容 格式: input|登 录
        /// 
        public Rectangle GetElementPointByMess(out IHTMLElement2 TempYs,string IdStr = "", string TextStr = "")
        {
            int YsX = -1;
            if (BrowserH < 1)
            {
                BrowserH=Screen.PrimaryScreen.WorkingArea.Height - ((IHTMLElement2)Document.documentElement).clientHeight;
            }
            int YsY = BrowserH;
            int YsWith = -1;
            int YsHeight = -1;
            //int dsadsa = ((IHTMLElement2)Document.documentElement).clientHeight;
            TempYs = null;
            try
            {
                if (IdStr != "")
                {
                    IHTMLElement2 TempElement = ((IHTMLElement2)Document.getElementById(IdStr));
                    YsX = TempElement.getBoundingClientRect().left;
                    YsY += TempElement.getBoundingClientRect().top;
                    YsWith = TempElement.scrollWidth;
                    YsHeight = TempElement.scrollHeight;
                    TempYs = TempElement;
                }
                else if (TextStr != "")
                {
                    IHTMLElement2 TempElement = null;
                    foreach (IHTMLElement i in Document.getElementsByTagName(TextStr.Split('|')[0].Trim()))
                    {
                        if (i.innerText == TextStr.Split('|')[1])
                        {
                            TempElement = (IHTMLElement2)i;
                            break;
                        }
                    }
                    YsX = TempElement.getBoundingClientRect().left;
                    YsY += TempElement.getBoundingClientRect().top;
                    YsWith = TempElement.scrollWidth;
                    YsHeight = TempElement.scrollHeight;
                    TempYs = TempElement;
                }
            }
            catch 
            {
                return new Rectangle(-1, -1, -1, -1);
            }
            return new Rectangle(YsX, YsY, YsWith, YsHeight);
        }
 
 
/// 
        /// 寻找子元素获取坐标
        /// 
        /// 返回找到的元素
        /// 
        public Rectangle GetElementPointByMess(out IHTMLElement2 TempYs)
        {
            int YsX = -1;
            if (BrowserH < 1)
            {
                BrowserH = Screen.PrimaryScreen.WorkingArea.Height - ((IHTMLElement2)Document.documentElement).clientHeight;
            }
            int YsY = BrowserH;
            int dsa = ((IHTMLElement2)Document.body).scrollHeight;
            int YsWith = -1;
            int YsHeight = -1;
            TempYs = null;
            try
            {
                IHTMLElement2 TempElement = null;
                IHTMLElementCollection IeC = (IHTMLElementCollection)Document.all.tags("A");
                TempElement = (IHTMLElement2)IeC.item("a", 0);
                YsX = TempElement.getBoundingClientRect().left;
                YsY += TempElement.getBoundingClientRect().top;
                YsWith = TempElement.scrollWidth;
                YsHeight = TempElement.scrollHeight;
                TempYs = TempElement;
            }
            catch
            {
                return new Rectangle(-1, -1, -1, -1);
            }
            return new Rectangle(YsX, YsY, YsWith, YsHeight);
        }
 
 
/// 
        /// 寻找子元素获取坐标
        /// 
        /// 返回找到的元素
        /// table|blog.tianya.cn|a|天涯
        /// 
        public Rectangle GetElementPointByMess(out IHTMLElement2 TempYs, string TextStr = "")
        {
            int YsX = -1;
            if (BrowserH < 1)
            {
                BrowserH = Screen.PrimaryScreen.WorkingArea.Height - ((IHTMLElement2)Document.documentElement).clientHeight;
            }
            int YsY = BrowserH;
            int dsa = ((IHTMLElement2)Document.body).scrollHeight;
            int YsWith = -1;
            int YsHeight = -1;
            TempYs = null;
            try
            {
                if (TextStr != "")
                {
                    IHTMLElement2 TempElement = null;
                    foreach (IHTMLElement i in Document.getElementsByTagName(TextStr.Split('|')[0].Trim()))
                    {
                        if (i.innerText != null && i.innerText.Contains(TextStr.Split('|')[1]))
                        {
                            foreach (IHTMLElement y in (IHTMLElementCollection)((IHTMLElementCollection)i.all).tags("A"))//.GetElementsByName(TextStr.Split('|')[2].Trim()
                            {
                                if (y.innerText != null && y.innerText.Contains(TextStr.Split('|')[3]) && y.tagName == "A")
                                {
                                    TempElement = (IHTMLElement2)y;
                                    break;
                                }
                            }
                            if (TempElement != null)
                                break;
                        }
                    }
                    YsX = TempElement.getBoundingClientRect().left;
                    YsY += TempElement.getBoundingClientRect().top;
                    YsWith = TempElement.scrollWidth;
                    YsHeight = TempElement.scrollHeight;
                    TempYs = TempElement;
                }
            }
            catch
            {
                return new Rectangle(-1, -1, -1, -1);
            }
            return new Rectangle(YsX, YsY, YsWith, YsHeight);
        }
 
 //设置浏览器版本
                    if (!RegSetting.SetReg(BrowserEnvironment_T[Rd.Next(BrowserEnvironment_T.Count)]))
                        continue;
                    MainForm.DisMes("伪装浏览器完成,启动浏览器中...");
 
 
//移动至输入框
                    IHTMLElement2 TempIH = null;
                    Rectangle Rt1 = NowBroswer.GetElementPointByMess(out TempIH, BroswerConfig[7].Substring(BroswerConfig[7].IndexOf("|") + 1), "");
                    MainForm.DisMes("移动至输入框...随机路径...");
                    if (MainForm.StopAll)
                        return;
                    MouseMove(new Point(Rd.Next(Rt1.X + 10, Rt1.X + Rt1.Width - 10), Rd.Next(Rt1.Y + 10, Rt1.Y + Rt1.Height - 10)));
                    if (MainForm.StopAll)
                        return;
                    MainForm.DisMes("点击鼠标左键");

 

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using mshtml;
using SHDocVw;

namespace WebClick_Tool
{
    /// 
    /// IE浏览器操作
    /// 
    public class IETool
    {
        /// 
        /// IE句柄
        /// 
        public int IEHandle { get; set; }
        /// 
        /// 浏览器
        /// 
        public IWebBrowser2 IEBrowser { get; set; }
        /// 
        /// 当前页面Document
        /// 
        public HTMLDocumentClass Document { get; set; }
        /// 
        /// 浏览器标头高度
        /// 
        public int BrowserH { get; set; }
        /// 
        /// 初始化是否成功
        /// 
        public bool Suc { get; set; }
        /// 
        /// 头部标题
        /// 
        public string HeadTitle { get; set; }
        /// 
        /// 失败头部标题
        /// 
        public string BadHeadTitle { get; set; }
        /// 
        /// 浏览器高度
        /// 
        public int HeighBro { get; set; }

        public IETool(string HeadTitleO,string BadTitle)
        {
            HeadTitle = HeadTitleO;
            BadHeadTitle = BadTitle;
            HeighBro = -1;
            if (GetHandleOfBrowser())
                Suc = true;
            else
                Suc = false;
            try
            {
                GetHtml(false);
            }
            catch { }
        }
        #region 系统API
        /// 
        /// 找窗口句柄
        /// 
        /// 
        /// 
        /// 
        [DllImport("user32", EntryPoint = "FindWindow")]
        public static extern int FindWindowA(string lpClassName, string lpWindowName);
        /// 
        /// 窗体发送消息
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        #endregion
        /// 
        /// 获取IE句柄
        /// 
        /// 
        private bool GetHandleOfBrowser()
        {
            IEHandle = FindWindowA("IEFrame", null);
            if (IEHandle == 0)
                return false;
            else
                return true;
        }
        /// 
        /// IE窗体最大化
        /// 
        public void SetMaxStyle(int Handle)
        {
            SendMessage(new IntPtr((Handle != 0 ? Handle : IEHandle)), 274, 61488, 0);
        }
        /// 
        /// 获取源代码
        /// 
        /// 
        public string GetHtml(bool Sacn)
        {
            string TempStr = "";
            //初始化所有IE窗口 
            IShellWindows sw = new ShellWindowsClass();
            for (int i = sw.Count - 1; i >= 0; i--)
            {
                //得到每一个IE的 IWebBrowser2 对象 
                IWebBrowser2 iwb2 = sw.Item(i) as IWebBrowser2;
                //比对 得到的 句柄是否符合查找的窗口句柄 
                if (iwb2!=null&&iwb2.HWND == IEHandle)
                {
                    Document = (HTMLDocumentClass)iwb2.Document;
                    if(Sacn)
                        if ((Document == null || Document.title == null || Document.title != HeadTitle) && !Document.title.Contains(BadHeadTitle))
                        {
                            continue;
                        }

                    iwb2.StatusBar = false;//状态栏
                    SendMessage(new IntPtr(iwb2.HWND), 274, 61488, 0);
                    if (Document == null)
                        return "";
                    if (Document.title == "百度一下,你就知道" || Document.title == "360搜索 - 干净、安全、可信任的搜索引擎")
                        TempStr = "" + ((HTMLDocumentClass)iwb2.Document).documentElement.outerHTML;
                    else
                        TempStr = ((HTMLDocumentClass)iwb2.Document).documentElement.outerHTML;
                    try
                    {
                        HeighBro = ((IHTMLElement2)Document.body).scrollHeight;
                    }
                    catch { }
                    break;
                }
            }
            return TempStr;
        }
        /// 
        /// 根据元素信息获取坐标
        /// 
        /// 元素ID
        /// 元素内容 格式: input|登 录
        /// 
    ///这个比较难。。。就不公布了。。。嘿嘿。。。如果想要就顶贴吧
    }
}


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IHtmlDocument
{
    public class HTMLHelper
    {
        /// 
        /// 通过指定URL地址,返回Document对象
        /// 
        /// LocationURL
        /// 
        public static mshtml.HTMLDocument GetHTMLDocumentByURL(String ieURL)
        {
            if (ieURL == String.Empty)
            {
                return null;
            }
            SHDocVw.ShellWindows sws = new SHDocVw.ShellWindows();
            //遍历ie进程
            foreach (SHDocVw.InternetExplorer iew in sws)
            {
                if (iew.LocationURL.Equals(ieURL))
                {
                    return iew.Document;
                }
            }
            return null;
        }

        /// 
        /// 通过窗口句柄获取Document对象
        /// 
        /// 
        /// 
        public static mshtml.HTMLDocument GetHTMLDocumentByIntPtr(int hWnd)
        {
            if (hWnd == 0)
            {
                return null;
            }
            SHDocVw.ShellWindows sws = new SHDocVw.ShellWindows();
            //遍历ie进程
            foreach (SHDocVw.InternetExplorer iew in sws)
            {
                if (iew.HWND == hWnd)
                {
                    return iew.Document;
                }
            }  
            return null;
        }

        ///   
        /// 通过进程名获得主窗口句柄
        ///   
        /// 进程名  
        /// 窗口句柄对象  
        public static IntPtr GetProcessMainFormHandle(string processName)
        {
            IntPtr m_WindowHandle = IntPtr.Zero;
            System.Diagnostics.Process pro = null;
            System.Diagnostics.Process[] pros = System.Diagnostics.Process.GetProcessesByName(processName);
            if (pros.Length > 0)
            {
                pro = pros[0];
                m_WindowHandle = pro.MainWindowHandle;
            }
            return m_WindowHandle;
        }

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();  //当前获得焦点窗口的句柄
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow); //nCmdShow 0关闭窗口 1正常大小窗口 2最小化窗口 3最大化窗口



    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IHtmlDocument
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int height = 0;
        private mshtml.HTMLDocument doc = null;
        private void button1_Click(object sender, EventArgs e)
        {
             System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\IEXPLORE.EXE", textBox1.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //获取Document对象
            doc = HTMLHelper.GetHTMLDocumentByURL(this.textBox1.Text);
            if (doc != null)
            {
                //获取body对象
                mshtml.IHTMLElement body = doc.body;
                this.textBox2.Text = doc.title; //获取标题
                this.textBox3.Text = body.outerHTML;  //获取body
                this.textBox4.Text = doc.documentElement.outerHTML; //获取html
                this.textBox5.Text = body.offsetHeight.ToString(); //网页内容高度
                height = body.offsetHeight;
            }
            else
            {
                MessageBox.Show("未找到对象。");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (doc != null)
            {
                doc.parentWindow.scrollTo(0, 0);
            }
            else
            {
                MessageBox.Show("未找到对象,先获取。");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (doc != null)
            {
                doc.parentWindow.scrollTo(0, height / 2);
            }
            else
            {
                MessageBox.Show("未找到对象,先获取。");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (doc != null)
            {
                doc.parentWindow.scrollTo(0, height);
            }
            else
            {
                MessageBox.Show("未找到对象,先获取。");
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (doc != null)
            {
                mshtml.IHTMLElementCollection inputs = doc.getElementsByTagName("input");
                foreach (mshtml.IHTMLElement b in inputs)
                {
                    if (b.className == this.textBox7.Text) //class="search-value"
                    {
                        b.innerText = this.textBox6.Text;  //文本框赋值
                    }
                }
            }
            else
            {
                MessageBox.Show("未找到对象,先获取。");
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (doc != null)
            {
                mshtml.IHTMLElementCollection inputs = doc.getElementsByTagName("span");
                foreach (mshtml.IHTMLElement b in inputs)
                {
                    if (b.className == "attention-sale d-attention") //class="attention-sale d-attention"  
                    {
                        b.click();
                    }
                }
            }
            else
            {
                MessageBox.Show("未找到对象,先获取。");
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (doc != null)
            {
                System.Threading.ThreadPool.QueueUserWorkItem((o) =>
                {
                    
                        int h = 0;
                        while (h < height)
                        {
                            h += 5;
                            mshtml.IHTMLElement body = doc.body;
                            height = body.offsetHeight;
                            this.BeginInvoke(new MethodInvoker(() => {
                                if (h >= height)
                                {
                                    doc.parentWindow.scrollTo(0, height);
                                }
                                else
                                {
                                    doc.parentWindow.scrollTo(0, h);
                                }
                            }));
                            System.Threading.Thread.Sleep(10);
                        }
                    
                });
                
            }
            else
            {
                MessageBox.Show("未找到对象,先获取。");
            }
        }


        //问题一:IE API没有选项卡属性支持,在多选项卡时无法判断当前活动选项卡。
        //问题二:获取滚动条当前位置,总是返回0
        private void button9_Click(object sender, EventArgs e)
        {
            //IntPtr whand = HTMLHelper.GetProcessMainFormHandle("iexplore");
            IntPtr whand = HTMLHelper.FindWindow("IEFrame", null);

            //获取Document对象
            doc = HTMLHelper.GetHTMLDocumentByIntPtr(whand.ToInt32());
            if (doc != null)
            {
                //获取body对象
                mshtml.IHTMLElement body = doc.body;
                this.textBox2.Text = doc.title; //获取标题
                this.textBox3.Text = body.outerHTML;  //获取body
                this.textBox4.Text = doc.documentElement.outerHTML; //获取html
                this.textBox5.Text = body.offsetHeight.ToString(); //网页内容高度
                height = body.offsetHeight;
            }
            else
            {
                MessageBox.Show("未找到对象。");
            }
        }
    }
}



 

 


 

你可能感兴趣的:(C#,WinForm)