从2011年到2013年7月,我一直在使用FindWindowEx从Chrome浏览器获取有关当前网址的数据。今天25.09.2013,我注意到班级Chrome_AutocompleteEditView不见了...我目前的Chrome版本是29.0.1547.76你们中有谁知道我现在如何阅读这个网址?
在我的代码
下面谢谢
IntPtr handle = getforegroundWindow();IntPtr urlHandle = FindWindowEx(handle, IntPtr.Zero, "Chrome_AutocompleteEditView", null);
我的问题已经解决了
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Automation;
namespace ui_automation
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
foreach (Process process in Process.GetProcessesByName("chrome"))
{
string url = GetChromeUrl(process);
if (url == null)
continue;
MessageBox.Show(url);
}
}
public static string GetChromeUrl(Process process)
{
string out_url = null;
if (process == null) {
out_url = null;
} else if (process.MainWindowHandle == IntPtr.Zero) {
out_url = null;
} else {
AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
if (element == null)
return null;
Condition conditions = new AndCondition(
new PropertyCondition(AutomationElement.ProcessIdProperty, process.Id),
new PropertyCondition(AutomationElement.IsControlElementProperty, true),
new PropertyCondition(AutomationElement.IsContentElementProperty, true),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)
);
AutomationElement elementx = element.FindFirst(TreeScope.Descendants, conditions);
out_url = ((ValuePattern)elementx.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}
return out_url;
}
}
}
但这不是我想要的。这段代码有效,但它仍然从 chrome 获取 URL 以变慢......2秒,有时甚至3秒。
我注意到,当我将 TreeScope.Descendant 更改为 TreeScope.Children 时,这段代码开始像闪存一样运行:)但返回 null - 未找到任何内容。
有什么想法吗?