首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从聚焦的Internet Explorer浏览器窗口中获取URL

如何从聚焦的Internet Explorer浏览器窗口中获取URL
EN

Stack Overflow用户
提问于 2019-06-19 03:14:48
回答 2查看 1.3K关注 0票数 3

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

我一直在使用西蒙在以下帖子中发布的代码:Retrieve current URL from C# windows forms application

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);
}

由于我正在运行Internet Explorer (并为此打开了网页),我希望/期望看到某种带有URL的输出,显示打开的URL。我没有得到写入控制台的URL,而是什么也得不到。在我尝试使用GetProcessesByName的情况下,我只得到以下输出:System.Diagnostics.Process[]

EN

回答 2

Stack Overflow用户

发布于 2019-06-19 04:31:17

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

1.添加对"Microsoft Internet Controls“的引用

URL2.url添加以下代码以获取的:

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();

List<string> urls = shellWindows
    .Cast<SHDocVw.InternetExplorer>()
    .Where(ie => System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower() == "iexplore")
    .Select(ie => ie.LocationURL)
    .ToList();

foreach (string url in urls)
{
    Console.WriteLine(url);
}
票数 2
EN

Stack Overflow用户

发布于 2019-06-21 20:16:13

这种解决方案是一种老生常谈的工作方式。

主要思想

  1. ALT+D发送到浏览器以选择URL文本
  2. 发送CTRL+C以复制URL

需要考虑的几件事

  1. 为了将键发送到窗口,该窗口必须是活动窗口(前景窗口)。
  2. 如果浏览器窗口状态已更改,最好将其恢复为原始状态。
  3. 如果使用了剪贴板,则最好将其恢复为原始状态。

获取活动选项卡URL的步骤

为原始活动窗口和原始浏览器窗口状态的主窗口查找名为“CTRL+C

  • Copy”的进程。使浏览器窗口处于活动状态,使浏览器窗口处于活动状态,并显示原始剪贴板数据。

  • 如果浏览器窗口已最小化,则将其最小化。

<>H139>还原原始活动窗口。

缺点

  1. 更改浏览器窗口状态是明显的noticeable.
  2. If浏览器窗口没有最小化,使它成为活动窗口将把它带到前面。即使还原了原始的活动窗口,它仍将是其后面的窗口。

代码需求

  1. 此代码将Vanara NuGet包用于Win32
  2. 为了使用Clipboard

ClipboardSendKeys

  • The Main必须具有[STAThread]属性才能引用System.Windows.Forms

代码

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]);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56655649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档