C# 如何从Internet Explorer浏览器窗口获取URL

C# 如何从Internet Explorer浏览器窗口获取URL,c#,webautomation,C#,Webautomation,我有一个键盘钩子实现,可以在给定条件下更改某些文本的输出。要确定如何格式化输出,我需要能够看到哪个窗口处于焦点位置,如果Internet Explorer处于焦点位置,我需要能够确定哪个URL在特定选项卡上打开 我一直在使用Simon在以下帖子中发布的代码: 当我运行Internet Explorer(并为此打开网页)时,我希望/期望看到带有URL的某种输出,显示打开的URL。我没有将URL写入控制台,而是一无所获。在我尝试使用getProcessByName的情况下,我只得到以下输出,Sys

我有一个键盘钩子实现,可以在给定条件下更改某些文本的输出。要确定如何格式化输出,我需要能够看到哪个窗口处于焦点位置,如果Internet Explorer处于焦点位置,我需要能够确定哪个URL在特定选项卡上打开

我一直在使用Simon在以下帖子中发布的代码:


当我运行Internet Explorer(并为此打开网页)时,我希望/期望看到带有URL的某种输出,显示打开的URL。我没有将URL写入控制台,而是一无所获。在我尝试使用
getProcessByName
的情况下,我只得到以下输出,
System.Diagnostics.Process[]

要从Internet Explorer选项卡获取所有URL,可以执行以下操作:

1.添加对“Microsoft Internet控件”的引用

2.添加以下代码以获取URL:

SHDocVw.ShellWindows ShellWindows=new SHDocVw.ShellWindows();
列表URL=shellWindows
.Cast()
.Where(ie=>System.IO.Path.GetFileName WithoutExtension(ie.FullName).ToLower()==“iexplore”)
.选择(ie=>ie.LocationURL)
.ToList();
foreach(url中的字符串url)
{
Console.WriteLine(url);
}

此解决方案是一种很有技巧的工作方式

主旨
  • 发送ALT+D到浏览器以选择URL文本
  • 发送CTRL+C以复制URL
  • 要考虑的一些事情
  • 要向窗口发送关键帧,它必须是活动窗口(前景窗口)
  • 如果浏览器窗口状态已更改,最好将其恢复为原始状态
  • 如果使用剪贴板,最好将其恢复到原始状态
  • 获取活动选项卡URL的步骤
  • 查找带有主窗口标题的名为“iexplor”的进程
  • 记住原始活动窗口和原始浏览器窗口状态
  • 使浏览器窗口成为活动窗口
  • 记住剪贴板上的原始数据
  • 发送ALT+DCTRL+C
  • 复制剪贴板
  • 还原原始剪贴板数据
  • 如果浏览器窗口已最小化,请将其最小化
  • 恢复原始活动窗口
  • 缺点
  • 更改浏览器窗口状态是显而易见的
  • 如果浏览器窗口未最小化,将其设为活动窗口将使其位于最前面。即使恢复了原始活动窗口,它仍将是其后面的窗口
  • 规范要求
  • 此代码使用Win32 API包
  • 剪贴板
    发送键
  • Main
    需要具有
    [STAThread]
    属性才能使用
    Clipboard
  • 代码
    使用System.Windows.Forms;
    使用系统诊断;
    使用Vanara.PInvoke;
    …
    //使用窗口标题获取所有Internet Explorer进程
    Process[]ieProcs=Process.getProcessByName(“iexplore”)
    .Where(p=>!string.IsNullOrEmpty(p.MainWindowTitle))
    .ToArray();
    //初始化URL数组以保存活动选项卡URL
    string[]ieUrls=新字符串[ieProcs.Length];
    for(int i=0;i
    请不要引用我的话,但我似乎记得从外部程序读取url是故意被阻止的。在表单中嵌入active-x控件时,即使这样做也很难。使用哪种IE版本?如果我在IE版本(11)上用Inspect检查IE层次结构,我发现这段代码是错误的…所有需要检测的URL暂时都应该在IE11中。谢谢!这非常适用于在IE中打开所有URL(至少在IE11中),但我真正需要做的是确定其中一个URL
    Process[] localByName = Process.GetProcessesByName("iexplore");
    
    
    if((Keys)vkCode == Keys.LShiftKey)
    {
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }
    
    
    foreach (Process process in Process.GetProcessesByName("iexplore"))
    {
        string url = GetInternetExplorerUrl(process);
        if (url == null)
            continue;
    
        Console.WriteLine("IE Url for '" + process.MainWindowTitle + "' is " + url);
    }
    
    using System.Windows.Forms;
    using System.Diagnostics;
    using Vanara.PInvoke;
    
    …
    
    // Get all Internet Explorer processes with a window title 
    Process[] ieProcs = Process.GetProcessesByName("iexplore")
        .Where(p => !string.IsNullOrEmpty(p.MainWindowTitle))
        .ToArray();
    
    // Initialize a URL array to hold the active tab URL
    string[] ieUrls = new string[ieProcs.Length];
    
    for (int i = 0; i < ieProcs.Length; i++)
    {
        Process proc = ieProcs[i];
    
        // Remember the initial window style of the process window
        User32_Gdi.WindowStyles initialWndStyles = (User32_Gdi.WindowStyles)User32_Gdi.GetWindowLongPtr(proc.MainWindowHandle, User32_Gdi.WindowLongFlags.GWL_STYLE);
    
        // Remember the initial foreground window
        IntPtr initialFgdWnd = (IntPtr)User32_Gdi.GetForegroundWindow();
    
        // Show the process window.
        // If it is minimized, it needs to be restored, if not, just show
        if (initialWndStyles.HasFlag(User32_Gdi.WindowStyles.WS_MINIMIZE))
        {
            User32_Gdi.ShowWindow(proc.MainWindowHandle, ShowWindowCommand.SW_RESTORE);
        }
        else
        {
            User32_Gdi.ShowWindow(proc.MainWindowHandle, ShowWindowCommand.SW_SHOW);
        }
    
        // Set the process window as the foreground window
        User32_Gdi.SetForegroundWindow(proc.MainWindowHandle);
    
        ieUrls[i] = null;
    
        // Remember the initial data on the clipboard and clear the clipboard
        IDataObject dataObject = Clipboard.GetDataObject();
        Clipboard.Clear();
    
        // Start a Stopwatch to timeout if no URL found
        Stopwatch sw = Stopwatch.StartNew();
    
        // Try to copy the active tab URL
        while (string.IsNullOrEmpty(ieUrls[i]) && sw.ElapsedMilliseconds < 1000)
        {
            // Send ALT+D
            SendKeys.SendWait("%(d)");
            SendKeys.Flush();
    
            // Send CRTL+C
            SendKeys.SendWait("^(c)");
            SendKeys.Flush();
    
            ieUrls[i] = Clipboard.GetText();
        }
        // Return the initial clipboard data to the clipboard
        Clipboard.SetDataObject(dataObject);
    
        // If the process window was initially minimized, minimize it
        if(initialWndStyles.HasFlag(User32_Gdi.WindowStyles.WS_MINIMIZE))
        {
            User32_Gdi.ShowWindow(proc.MainWindowHandle, ShowWindowCommand.SW_MINIMIZE);
        }
    
        // Return the initial foreground window to the foreground
        User32_Gdi.SetForegroundWindow(initialFgdWnd);
    }
    
    // Print the active tab URL's
    for (int i = 0; i < ieUrls.Length; i++)
    {
        Console.WriteLine(ieUrls[i]);
    }