Webbrowser Control - Button coordinates

Posted on 2006-03-28
Last Modified: 2011-10-03


I have a dialog that contain a webbrowser control. The html loaded in the control display a button (<input type=button id=mybutton>).

I can get the coordinate of the webbrowser control using GetWindowRect, however I can't figure any way to find the coordinate of the button.

Can anyone suggest a way to accomplish that?

Question by:gammae
    LVL 1

    Accepted Solution

    Hello Karl,

    You need something like this,
    the code is not final and not tested, but i believe you can change it for your needs.

    // replace with your WebBrowser pointer
    CComPtr<IWebBrowser2> spBrowser;

    long top = 0;
    long left = 0;

    CComPtr<IDispatch> spDocDisp;
    spBrowser->get_Document( &spDocDisp );
    if( !spDocDisp )
          return false;

    CComQIPtr<IHTMLDocument2> spDoc( spDocDisp );
    if( !spDoc )
          return false;

    CComPtr<IHTMLElementCollection> spElements;
    spDoc->get_all( &spElements );
    if( !spElements )
          return false;

    CComBSTR bsTagName( "button" );
    CComVariant vtButton( bsTagName );
    CComPtr<IDispatch> spButtonsDisp;
    spElements->tags( vtButton, &spButtonsDisp );
    if( !spButtonsDisp )
          return false;
    CComQIPtr<IHTMLElementCollection> spButtonElements( spButtonsDisp );
    if( !spButtonElements )
          return false;

    CComBSTR bsID;

    long count = 0;
    spButtonElements->get_length( &count );
    for( int i = 0; i < count; ++i )
          CComVariant vtIndex( i );
          CComPtr<IDispatch> spButtonDisp;
          if( SUCCEEDED( spButtonElements->item( vtIndex, vtIndex, &spButtonDisp ) ) )
                CComQIPtr<IHTMLButtonElement> spButton( spButtonDisp );
                if( spButton )
                      CComQIPtr<IHTMLElement> spButtonEl1( spButtonDisp );
                      spButtonEl1->get_id( &bsID );
                      if( bsID == "mybutton" )
                            CComQIPtr<IHTMLElement2> spButtonEl2( spButtonDisp );
                            spButtonEl2->get_clientTop( &top );
                            spButtonEl2->get_clientLeft( &left );

    and a link about elements measuring and positioning:

    Good luck,

    Author Comment

    Thanks a lot ofr the solution, I'll try to implement that in my project. The points were well deserved.


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now