在 WebBrowser 文档中获取鼠标点击坐标 [英] Getting mouse click coordinates in a WebBrowser Document
问题描述
我在 PictureBox 中捕获点击坐标,但我想用 WebBrowser 实现同样的目标.经过一番研究,我发现无法订阅 WebBrowser 控件的 Mouse Click
事件.
捕获点击的可能方法有哪些?是否有一种元素可以让我浏览页面但仍然捕获点击?
I am capturing click coordinates within a PictureBox but I want to achieve the same thing with a WebBrowser. After some research I found out that it is not possible to subscribe to the Mouse Click
event of a WebBrowser control.
What are the possible ways to capture the clicks? Is there a sort of element which would allow me to navigate through the page but still capture the click?
我尝试创建一个透明面板,但透明颜色并不意味着像我看到的那样透视,并且当元素在后面时也不会捕获点击,能够在面板位于 WebBrowser 后面时捕获点击也可以.
I tried to create a transparent panel but transparent color doesn't mean see through as I see and when the element is in the back also doesn't capture the click, being able to capture the clicks with panel being behind the WebBrowser would also work.
图片框代码:
private void uploadedPNG_MouseClick(object sender, MouseEventArgs e)
{
if(uploadedPNG.Image != null && !string.IsNullOrEmpty(deviceHeight.Text) && !string.IsNullOrEmpty(deviceWidth.Text))
{
mouseX = e.X;
mouseY = e.Y;
targetHeight = Int32.Parse(deviceHeight.Text);
targetWidth = Int32.Parse(deviceWidth.Text);
int outPutWidth = (mouseX * targetWidth) / uploadedPNG.Width;
int outPutHeight = (mouseY * targetHeight) / uploadedPNG.Height;
ConsoleText.Text = "Clicked X coordinate " + outPutWidth + " Clicked Y coordinate " + outPutHeight;
}
}
推荐答案
WebBrowser 本身不提供鼠标点击坐标:您实际上不是在单击控件客户区,而是在单击 HtmlDocument 的内容.
The WebBrowser itself doesn't provide Mouse Click coordinates: you're not actually clicking on the Control client area, you're clicking on the content of the HtmlDocument.
您可以使用 HtmlDocument.Click 或 HtmlDocument.MouseDown 事件以在 初始化 HtmlDocument.
You can use the HtmlDocument.Click or HtmlDocument.MouseDown events to retrieve the Mouse pointer coordinates on an initialized HtmlDocument.
注意:
HtmlElementEventArgs 对象返回鼠标坐标e.ClientMousePosition 和相对于点击的 HtmlElement
,在 e.OffsetMousePosition.
Note:
The HtmlElementEventArgs object returns the Mouse coordinates in both absolute coordinates (the whole Document area), in e.ClientMousePosition and relative to the clicked HtmlElement
, in e.OffsetMousePosition.
这可能很棘手,因为您需要在当前 HtmlDocument 已经创建时订阅 Click
事件:您无法订阅默认 Document 对象的事件:
即,订阅 Form.Load
中的事件:
This can be tricky, because you need to subscribe to the Click
event when the current HtmlDocument is already created: you cannot subscribe to the event of the default Document object:
i.e., subscribing to the event in Form.Load
with:
webBrowser1.Document.Click += (obj, evt) => { /*Do something */ };
不会完成任何事情.永远不会引发该事件:文档是 null
因此,当然,它不是任何当前/活动 HtmlDocument 的引用.
will not accomplish anything. The event will never be raised: the Document is null
thus, of course, it's not referency any current/active HtmlDocument.
HtmlDocument
在 WebBrowser.DocumentCompleted 事件被引发并且它的 ReadyState 设置为 WebBrowserReadyState.Complete
.
An HtmlDocument
is ready when the WebBrowser.DocumentCompleted event is raised and its ReadyState is set to WebBrowserReadyState.Complete
.
您可以在文档完全加载时订阅 Document.Click
事件,然后在 WebBrowser 导航到新页面、创建新文档之前移除该事件.
You can subscribe to the Document.Click
event when the Document is fully loaded, then remove the event before the WebBrowser navigates to a new page, creating a new document.
此外,对于单个 HTML
页面,DocumentCompleted
事件可能会多次引发,因此您需要确保不会多次订阅同一事件次:
Also, the DocumentCompleted
event may be raised multiple times for a single HTML
page, so you need to be sure that you don't subscribe to the same event multiple times:
注意:
一个 HtmlDocument
可能包含多个 Frame/IFrame
并且每个 Frame
可能有自己的 HtmlDocument代码>;
IFrames
肯定有一个.有关此问题的更多信息,请参阅此问题中的注释:
如何在 Frames/IFrames 中获取 HtmlElement 值?
Note:
A HtmlDocument
may contain more than one Frame/IFrame
and each Frame
may have its own HtmlDocument
; IFrames
have one each for sure. Refer to the notes in this question for more informations on this matter:
How to get an HtmlElement value inside Frames/IFrames?
示例:
bool WebBrowserDocumentEventSet = false;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = (sender as WebBrowser);
if (wb.ReadyState == WebBrowserReadyState.Complete && WebBrowserDocumentEventSet == false)
{
WebBrowserDocumentEventSet = true;
wb.Document.MouseDown += this.OnHtmlDocumentClick;
}
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
(sender as WebBrowser).Document.MouseDown -= this.OnHtmlDocumentClick;
WebBrowserDocumentEventSet = false;
}
protected void OnHtmlDocumentClick(object sender, HtmlElementEventArgs e)
{
Console.WriteLine(e.ClientMousePosition);
}
这篇关于在 WebBrowser 文档中获取鼠标点击坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!