201910121570870650203830.png

C#获取系统自带浏览器IE的当前进程打开的URL网站以及激活的IE界面(IE多标签或多窗口状态下被激活状态的网站)。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

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

        [DllImport("user32", EntryPoint = "FindWindow")]
        public static extern int FindWindowA(string lpClassName, string lpWindowName);
        [DllImport("User32.dll")]
        static extern int GetWindowText(int hwnd, StringBuilder buf, int nMaxCount);

        private void button1_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();

            SHDocVw.InternetExplorer browser;
            string myLocalLink;
            mshtml.IHTMLDocument2 myDoc;
            SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
            string filename;
            foreach (SHDocVw.InternetExplorer ie in shellWindows)
            {
                filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
                if ((filename == "iexplore"))
                {
                    browser = ie;

                    myDoc = browser.Document;
                    myLocalLink = myDoc.url;

                    int iehwnd = FindWindowA("IEFrame", null);
                    StringBuilder str = new StringBuilder(512);
                    GetWindowText(iehwnd, str, str.Capacity);
                    Log.Info(str.ToString());
                    this.listBox1.Items.Add("【browser】" + str.ToString());
                    this.listBox1.Items.Add(myLocalLink);
                }
            }
        }
    }
}
最后修改:2020 年 11 月 22 日 09 : 20 PM
如果觉得我的文章对你有用,请随意赞赏