Click here to Skip to main content
11,410,584 members (56,498 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hi
I am developing a wpf application. The browser is embedded in wpf application using webbrowser control.
One of my requirement is to inspect element. Similar functionality is present in IE Developer tool , Firefox's firebug.
For Eg. If i click on any element like "Button" or "Link", it should give me details (id, tagName, Xpath) of selected object but it should not perform any action like POST or Navigate. I have referred this link

http://msdn.microsoft.com/en-us/library/ie/ms536913%28v=vs.85%29.aspx[^]

My current code is working for Basic HTML Page

private void wpfWebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
 
            mshtml.HTMLDocument doc1 = this.wpfWebBrowser.Document as mshtml.HTMLDocument;
	    mshtml.HTMLDocumentEvents2_Event iEvent;
            iEvent = (mshtml.HTMLDocumentEvents2_Event)doc1;
            iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(iEvent_onclick);
            iEvent.onmouseup += new mshtml.HTMLDocumentEvents2_onmouseupEventHandler(iEvent_onmouseup);
	}
 
bool iEvent_onclick(mshtml.IHTMLEventObj pEvtObj)
        {
	    pEvtObj.cancelBubble = true; 
            pEvtObj.returnValue = false;        
            return false;
        }
 
private void wpfWebBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            
            e.Cancel = true;
            
        }

Same code is not working for IFrames present in webpage. for eg. Gmail mailbox is developed using Iframes.
To test it for IFrames you have to register events for each iframe present in DOM. Because IE treats each Iframe as separate window. Here is my code for IFrames


private void wpfWebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
 
            mshtml.HTMLDocument doc1 = this.wpfWebBrowser.Document as mshtml.HTMLDocument;
	    if (doc1.parentWindow.frames.length > 0)
            {
                List<mshtml.HTMLDocumentEvents2_Event> iFrameEventList = new List<mshtml.HTMLDocumentEvents2_Event>(doc1.parentWindow.frames.length);
                
                for (int index = 0; index < doc1.parentWindow.frames.length; index++)
                {
                    try
                    {
                        object refIndex = index;
                        mshtml.HTMLWindow2 win = doc1.parentWindow.frames.item(ref refIndex) as mshtml.HTMLWindow2;
                        if (win != null)
                        {
                            mshtml.HTMLDocument innerdoc = win.document as mshtml.HTMLDocument;
                            
                            mshtml.HTMLDocumentEvents2_Event iframeEventItem = (mshtml.HTMLDocumentEvents2_Event)innerdoc;
                            iFrameEventList.Add(iframeEventItem);
                            iFrameEventList[index].onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(iFrameEvent_onclick);
                            iFrameEventList[index].onmouseup += new mshtml.HTMLDocumentEvents2_onmouseupEventHandler(MainWindow_onmouseup);
                            
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);                        
                    }
                }
            }
	}
 

void MainWindow_onmouseup(mshtml.IHTMLEventObj pEvtObj)
        {
	    pEvtObj.cancelBubble = true; 
            pEvtObj.returnValue = false;          
        }
 
bool iFrameEvent_onclick(mshtml.IHTMLEventObj pEvtObj)
        {
            pEvtObj.cancelBubble = true; 
            pEvtObj.returnValue = false;
            return true;
        }
 

If i try to click on any HTML Element on Gmail loggedIn page, i can capture the event in "iFrameEvent_onclick" or "MainWindow_onmouseup" event handler.
But i cant stop the action performed. For eg. When i click on any received mail it will open the mail instead of that I want to stop that action.

Please help me in solving my problem or let me know if i am missing something.


Thank you
Bipin
Posted 27-Jul-12 4:41am
Edited 27-Jul-12 7:55am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,885
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 27 Jul 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100