如何捕获点击事件在Web浏览器控件内部的任何按钮? [英] How to capture click event for any button inside in web browser control?

查看:371
本文介绍了如何捕获点击事件在Web浏览器控件内部的任何按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我的网页浏览器显示一个HTML页面,许多按钮都在那里。我只是想知道我怎么能捕获点击Web浏览器控件内部的任何按钮,从我的C#取胜的应用程序。



如果有可能然后从该事件中,我想捕捉按钮的名称,高度和宽度,任何自定义属性。 。等等。请指引我


解决方案

这会,如果你想只捕捉鼠标点击会有所帮助:

  web浏览器_BROWSER; 
this._browser.DocumentCompleted + =新WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
...
私人无效browser_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
{
this._browser.Document.Body.MouseDown + =新HtmlElementEventHandler(Body_MouseDown);
}
...
无效Body_MouseDown(对象发件人,HtmlElementEventArgs E)
{
开关(e.MouseButtonsPressed)
{
案MouseButtons.Left:
的HtmlElement元素= this._browser.Document.GetElementFromPoint(e.ClientMousePosition);
如果(元素= NULL&放大器;!&安培;提交.Equals(element.GetAttribute(类型),StringComparison.OrdinalIgnoreCase)
{
}
中断;
}
}




u能请告诉我我怎么可以读取Web浏览器控件加载里面的任何HTML
元素的自定义属性。感谢




如果你不想链接到Microsoft.mshtml,你可以尝试使用此示例方法,但你无法读取所有成员直通反思:

 公共静态字符串GetElementPropertyValue(的HtmlElement元素,String属性)
{
如果(元素== NULL)
抛出新的ArgumentNullException(元素);
如果(字符串.IsNullOrEmpty(财产))
抛出新的ArgumentNullException(房产);

字符串结果= element.GetAttribute(财产);
如果(String.IsNullOrEmpty(结果))
{//ВMSIE 9получитьсвойствочерез一个DOMElementнеполучается。 Т.к. тамонComObject。
VAR objProperty = element.DomElement.GetType()的getProperty(属性)。
如果(objProperty!= NULL)
{
对象值= objProperty.GetValue(element.DomElement,NULL);
结果=值== NULL?的String.Empty:value.ToString();
}
}
返回结果;
}


Suppose my web browser is showing a html page where many buttons are there. I just like to know how could I capture click on any button inside web browser control from my c# win apps.

If it is possible then from that event i want to capture button name,height and width and any custom property. etc. Please guide me.

解决方案

This will be helpful if you want to capture only mouse clicks:

WebBrowser _browser;
this._browser.DocumentCompleted+=new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
...
private void browser_DocumentCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e)
{
    this._browser.Document.Body.MouseDown += new HtmlElementEventHandler(Body_MouseDown);
}
...
void Body_MouseDown(Object sender, HtmlElementEventArgs e)
{
    switch(e.MouseButtonsPressed)
    {
    case MouseButtons.Left:
        HtmlElement element = this._browser.Document.GetElementFromPoint(e.ClientMousePosition);
        if(element != null && "submit".Equals(element.GetAttribute("type"),StringComparison.OrdinalIgnoreCase)
        {
        }
    break;
    }
}

can u please tell me how can i read custom attribute of any html element loaded inside web browser control. thanks

If You don't want to link to "Microsoft.mshtml", You can try to use this sample method. But you can't read all members thru reflection:

public static String GetElementPropertyValue(HtmlElement element, String property)
{
    if(element == null)
        throw new ArgumentNullException("element");
    if(String.IsNullOrEmpty(property))
        throw new ArgumentNullException("property");

    String result = element.GetAttribute(property);
    if(String.IsNullOrEmpty(result))
    {//В MSIE 9 получить свойство через DomElement не получается. Т.к. там он ComObject.
        var objProperty = element.DomElement.GetType().GetProperty(property);
        if(objProperty != null)
        {
            Object value = objProperty.GetValue(element.DomElement, null);
            result = value == null ? String.Empty : value.ToString();
        }
    }
    return result;
}

这篇关于如何捕获点击事件在Web浏览器控件内部的任何按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆