我正在开发使用WordCaptureX和WebBrowser组件的应用程序。
应用程序处理热键,从屏幕捕获单词并将WebBrowser控件导航到某个URL。期望用户可以从应用程序WebBrowser捕获一个单词,但是在几次捕获操作之后,控件在加载状态下挂起并且导航方法不起作用。我试图阻止WebBrowser甚至处理它并重新创建但没有任何帮助。
我创建了一个包含主要应用程序操作的演示。
public partial class Form1 : Form
{
private readonly WMonitorX _wMonitor = ComFactory.Instance.NewWMonitorX();
private readonly WCaptureX _wCapture = ComFactory.Instance.NewWCaptureX();
private readonly ManualResetEvent _runCaptureEvent = new ManualResetEvent(true);
private readonly ManualResetEvent _startRetrieveEvent = new ManualResetEvent(false);
private int _hwnd;
private int _x;
private int _y;
private delegate void Worker();
private readonly Worker _worker;
public Form1()
{
InitializeComponent();
_wMonitor.LineColor = 11119017;
_wMonitor.LineWidth = 10;
_wMonitor.WEvent += _wMonitor_WEvent;
_wMonitor.Start((int)W_KEY.wmKeyCtrl, (int)W_MOUSE.wmMouseRight, false);
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
_worker = RunCaptureAndNavigate;
_worker.BeginInvoke(null, null);
}
private void _wMonitor_WEvent(int hwnd, int x1, int y1, int x2, int y2)
{
_hwnd = hwnd;
_x = x1;
_y = y1;
_startRetrieveEvent.Set();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url == webBrowser1.Url)
{
_runCaptureEvent.Set();
toolStripStatusLabel1.Text = "Page loaded";
}
}
private void RunCaptureAndNavigate()
{
while (true)
{
_startRetrieveEvent.WaitOne();
_runCaptureEvent.WaitOne();
_runCaptureEvent.Reset();
_startRetrieveEvent.Reset();
Invoke((MethodInvoker) ExecuteCapturing);
Invoke((MethodInvoker) Activate);
toolStripStatusLabel1.Text = "Page loading";
webBrowser1.Navigate("google.com");
}
}
private void ExecuteCapturing()
{
var input = ComFactory.Instance.NewWInput();
input.Hwnd = _hwnd;
input.StartX = _x;
input.StartY = _y;
input.Options = (int) W_CAPTURE_OPTIONS.wCaptureOptionsHighlightWords |
(int) W_CAPTURE_OPTIONS.wCaptureOptionsUseWindowsWordBreaking;
var result = _wCapture.Capture(input);
var text = string.Empty;
if (result != null && string.IsNullOrEmpty(result.Text))
{
text = result.Text;
}
textBox1.Text = text;
}
}
不幸的是,支持WordCaptureX组件无法帮助我避免浏览器在加载时遇到麻烦的情况。
所以可能有人知道如何重新初始化浏览器?