Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

c# - How to capture click event for any button inside in web browser control?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...