We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Webbrowser Control - Button coordinates

gammae asked
Medium Priority
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?

Watch Question

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;


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,

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.