我有一个键盘挂钩实现,可以在给定条件下更改某些文本的输出。为了确定输出的格式,我需要能够看到哪个窗口处于焦点,并且如果Internet Explorer处于焦点,则需要能够确定在该特定选项卡上打开了哪个URL。
我一直在使用Simon在以下帖子中发布的代码: 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[]
答案 0 :(得分:0)
要从Internet Explorer选项卡获取所有URL,可以执行以下操作:
1。。添加对“ Microsoft Internet控件”的引用
2。。添加以下代码以获取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);
}
答案 1 :(得分:0)
此解决方案是完成这项工作的一种简单方法。
System.Windows.Forms
和Clipboard
需要引用SendKeys
Main
必须具有[STAThread]
属性才能使用Clipboard
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]);
}