Click here to Skip to main content
15,886,547 members
Articles / Desktop Programming / Windows Forms

How To Tell What is Clicked in a WebBrowser Control

Rate me:
Please Sign up or sign in to vote.
4.65/5 (18 votes)
6 Jan 2009Public Domain2 min read 64.9K   2.2K   51   10
This article explains how to grab the HTML Element that was clicked in a WebBrowser control

Introduction

This is my first submission. It is small and will be useful for beginners.

This article will demonstrate how you can tell what is selected under the cursor inside a WebBrowser control, using your own custom ContextMenu. Have you ever wanted to replace the default Internet Explorer ContextMenu? This should help you fully achieve that goal.

Background

I am currently writing a WebBrowser application for kids. Everything worked great, and I assumed I was near completion... except for that pesky - clearly Internet Explorer - ContextMenu.

Getting a custom ContextMenu to work is not the focus of this article, as it is accomplished in a few easy steps. The focus of this article is being able to provide functionality close to what Internet Explorer presents, by providing the user with a different menu for different elements on the page. 

For example, "Save Picture" when an image is clicked, or "Copy Shortcut" when a link is clicked. 

Using the Code 

I have included a sample project of what I will outline here.

First off, we need to translate the mouse coordinates on the screen, into a Point object:

C#
Point ScreenCoord = new Point(MousePosition.X, MousePosition.Y); 

Now, we must create the coordinates of the browser, based off the coordinates of the screen:

C#
Point BrowserCoord = webBrowser1.PointToClient(ScreenCoord);

Now we can use the WebBrowser documents GetElementFromPoint method to retrieve the element that has been clicked:

C#
HtmlElement elem = webBrowser1.Document.GetElementFromPoint(BrowserCoord);

Now, we can use this element to see what has been clicked:

C#
switch (elem.TagName)
{
    case "A":
        //! We have clicked a link
        break;
    case "IMG":
        //! We have clicked an image
        break;
    default:
        //! This is anywhere else
        break;
}

Points of Interest

I pulled my hair out for a fairly long time searching for a way to do this. I couldn't find anything, even here on The Code Project. So I figured this would be helpful for some people. Note that once you have the element, you can do anything with it, for example, using GetAttribute to retrieve the SRC of an image to download it, etc.

History

  • 1/7/09 - First submission - all that's needed, to be honest!
  • 1/8/09 - Attempted to clarify the goal of the article

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior) RSPCA
Australia Australia
I am a young developer from Australia. I have been programming since I was 11, when I started with Visual Basic. I have since moved onto a degree in C++ (with an emphasis on game development) and I am currently the Application/Web Developer for RSPCA Victoria (http://www.rspcavic.org), which is a not-for-profit organisation for the protection of animals.

I enjoy: my young family - my partner and my daughter - Mortal Kombat, Aussie Beer, C++, C#, any form of music not including Opera and my dog "Buddy".

Comments and Discussions

 
QuestionGood work Pin
mat_odtu24-Feb-19 1:06
mat_odtu24-Feb-19 1:06 
Questionu have done a fentastic job. Pin
Member 127599716-Oct-16 2:15
Member 127599716-Oct-16 2:15 
for these iam searching for long finally stopped with you.thanks
great job.
GeneralMy vote of 5 Pin
User 118311247-May-16 15:35
User 118311247-May-16 15:35 
QuestionVote of 5 Pin
kingPuppy22-Mar-14 14:17
kingPuppy22-Mar-14 14:17 
QuestionEpub Reader Pin
Member 848098020-May-13 23:46
Member 848098020-May-13 23:46 
GeneralThanks Pin
shihab_leo18-Aug-11 10:04
shihab_leo18-Aug-11 10:04 
GeneralMy vote of 5 Pin
rutstyle21-Jan-11 5:20
rutstyle21-Jan-11 5:20 
Generala slight clarification ... ? and thanks ... Pin
BillWoodruff6-Jan-09 15:50
professionalBillWoodruff6-Jan-09 15:50 
GeneralRe: a slight clarification ... ? and thanks ... Pin
Simon_Whitehead7-Jan-09 11:18
Simon_Whitehead7-Jan-09 11:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.