在WebBrowser控件中获取鼠标在网页上点击的位置
在WebBrowser控件中获取鼠标在网页(不是浏览器窗口)上点击的位置,只有代码了,没有讲解:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown);
}
void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
IHTMLElement2 element = document.body as IHTMLElement2;
int scrolltop = webBrowser1.Document.Body.ScrollTop - element.clientTop;
int scrollLeft = webBrowser1.Document.Body.ScrollLeft - element.clientLeft;
if (document.body.parentElement != null)
{
IHTMLElement2 parent = document.body.parentElement as IHTMLElement2;
scrolltop += parent.scrollTop - parent.clientTop;
scrollLeft += parent.scrollLeft - parent.clientLeft;
}
int positionX = e.ClientMousePosition.X + scrollLeft;
int positionY = e.ClientMousePosition.Y + scrolltop;
Debug.WriteLine(string.Format("positionX: {0}, positionY: {1}",
positionX, positionY));
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步