5,530,111 members and growing! (17,172 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Advanced

Detecting the IE Refresh button using IWebBrowser2 and DWebBrowserEvents2 events.

By SteveFox

An article explaining how to capture a click on Internet Exporer's Refresh button, using DWebBrowserEvents2.
C++/CLI, VC6, C++, .NET, Windows, Win2K, WinXP, MFC, VS6, Visual Studio, Dev

Posted: 5 Feb 2003
Updated: 14 Oct 2004
Views: 108,099
Bookmarked: 20 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 4.85 Rating: 4.24 out of 5
1 vote, 7.1%
1
0 votes, 0.0%
2
1 vote, 7.1%
3
4 votes, 28.6%
4
8 votes, 57.1%
5

Introduction

If you have ever tried to capture an event from the Internet Explorer web browser when a user clicks the Refresh button, you will know it is not easy. Capturing Internet Explorer events is generally not easy using MFC or COM. I have created a class that I use to capture DWebBrowserEvents2 events and the logic needed to detect a page refresh. There are no events specifically sent for a page refresh and the normal events you check for do not work. So you have to use a work around. My class is called CIEComCtrlSink. It is derived from CCmdTarget and is used to capture DWebBrowserEvents2 events. You can also use the logic to solve this problem in JavaScript etc.

Background

I did some searching on the Internet for a solution to this Microsoft bug/issue and only found some talk of a workout in Google newsgroups. So after I got it working, I thought I better throw the solution up on CodeProject.

Using the code

In the sample application, I just used an MFC Wizard to create a new Dialog project. In the file IERefreshSampleDlg.cpp I respond to a button click and create a new web browser object and advise it we want events from it.

// create the browser and event sink

BOOL CIERefreshSampleDlg::CreateMyIEBrowser()
{
    if(m_pMyIESink != NULL)
    {
        AfxMessageBox("Sorry just one browser in this sample :)");
        return false;
    }

    m_pMyIESink = new CIEComCtrlSink();
    m_pMyIESink->m_pParent = this;
    BOOL bOK = m_pMyIESink->MyAdviseSink();

    return bOK;
}

The CIEComCtrlSink class does all the work. It is derived from CCmdTarget so we can get events from DWebBrowserEvents2 object.

// start capture of DWebBrowserEvents2 events

BOOL CIEComCtrlSink::MyAdviseSink()
{
    // create the web browser using IWebBrowser2

    HRESULT hr = CoCreateInstance(CLSID_InternetExplorer,
                               NULL,
                               CLSCTX_LOCAL_SERVER,
                               IID_IWebBrowser2,
                               (void**)&m_pWebBrowser2);

    if(!SUCCEEDED(hr))
    {
        CString strMsg;
        strMsg.Format("Failed to create object error = %d",(int)hr);
        AfxMessageBox(strMsg);
        return false;
    }
    // get an interface to IWebBrowserApp so we can set the toolbars we want

    hr = m_pWebBrowser2->QueryInterface(IID_IWebBrowserApp,
                                              (void**)&m_pIEApp);
    hr = m_pIEApp->put_StatusBar(true);
    hr = m_pIEApp->put_ToolBar(true);
    hr = m_pIEApp->put_MenuBar(true);

    long hIE;
    // get a window handle so we can resize and show the browser window.

    hr = m_pWebBrowser2->get_HWND(&hIE);
    if(!SUCCEEDED(hr))
    {
        CString strMsg;
        strMsg.Format("Failed to create object error = %d",(int)hr);
        AfxMessageBox(strMsg);
        return FALSE;
    }
    // use CWnd to make showing the window etc easy

    m_wndWebBrowser.Attach((HWND)hIE);
    m_wndWebBrowser.EnableScrollBar(SB_BOTH, ESB_DISABLE_BOTH);
    m_wndWebBrowser.ShowWindow(SW_NORMAL);

    // navigate to a URL

    Navigate2("http://www.stevefoxover.com");

    // advise the browser to send events here

    LPUNKNOWN pUnkSink = GetIDispatch(FALSE);
    AfxConnectionAdvise((LPUNKNOWN)m_pWebBrowser2, 
      DIID_DWebBrowserEvents2,pUnkSink,FALSE,&m_dwCookie); 

    return TRUE;
}

Points of interest

The tricky part is actually working out if the refresh button was hit. The logic goes as follows:

  • Each time BeforeNavigate2() is called we add 1 to a page counter m_nPageCounter ++;
  • Each call to DocumentComplete(), we take 1 from the page counter m_nPageCounter --;
  • Each call to DownloadBegin(), we check if m_nPageCounter == 0. If so we know it's a refresh page call.
  • We also add to an object counter here m_nObjCounter ++;
  • Also we set a member variable to true to note it's a refresh call, m_bIsRefresh = true;
  • On DownloadEnd() we decrease the counter from DownloadBegin. m_nObjCounter --;
  • If m_nObjCounter is zero and we are in refresh mode we know that the refreshed page has loaded. if(m_bIsRefresh && m_nObjCounter == 0)

Clear as mud? Just compile the sample application and put break points in:

  • CIEComCtrlSink::BeforeNavigate2()
  • CIEComCtrlSink::DocumentComplete()
  • CIEComCtrlSink::DownloadBegin()
  • CIEComCtrlSink::DownloadEnd()

You will then see what is called and when from Internet Explorer. IE never calls BeforeNavigate2() or DocumentComplete() on a page refresh. (Bad IE...)

History

My first release...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

SteveFox


I have spent many years working on projects involving data and large file transportation via the Internet. For the last 5 years I have also worked with digital video. I have created applications that encode, transcode, publish, deliver via progressive download with download acceleration, track and protect digital video.
Occupation: Web Developer
Location: United States United States

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 38 (Total in Forum: 38) (Refresh)FirstPrevNext
Subject  Author Date 
Answersolutionmembermojo.ssp8:59 4 May '08  
Generalhow to detect the event for ie? [modified]memberArmandopoulos3:01 30 Nov '07  
Generalcatches javascript image downloadsmembersunwuong87:43 1 Nov '07  
GeneralWorks well, but not perfectly.memberZinkyu13:44 9 Oct '07  
GeneralRe: Works well, but not perfectly.memberconman1109:37 22 Oct '07  
GeneralRe: Works well, but not perfectly.memberZinkyu15:29 22 Oct '07  
GeneralRe: Works well, but not perfectly.memberconman1102:16 23 Oct '07  
GeneralInsert a dummy stylesheetmemberMingaliu16:05 24 Aug '07  
GeneralRe: Insert a dummy stylesheetmemberZinkyu13:48 9 Oct '07  
Questionhow to detect the download events?memberkamalraaja20:05 23 Jul '06  
Generalslightly off-topic questionmemberIrek Zielinski11:38 18 Mar '06  
Generalanother (common) solutionmemberMike Melnikov21:19 19 Oct '04  
GeneralRe: another (common) solutionmemberJIANG, Sheng[MVP]15:39 10 Jul '05  
GeneralRe: another (common) solutionmemberjeremy.w12:09 10 Aug '05  
GeneralRe: another (common) solutionmemberJIANG, Sheng[MVP]4:49 19 Apr '06  
GeneralCould it be work on an ActiveXmemberrc28610115:17 25 Mar '04  
GeneralRe: Could it be work on an ActiveXmemberSteveFox13:10 28 Mar '04  
GeneralChange Headers in BeforeNavigate2memberjjohnston19:15 6 Jan '04  
GeneralRe: Change Headers in BeforeNavigate2memberMarmari6:51 24 Jan '04  
GeneralRe: Change Headers in BeforeNavigate2sussAnonymous11:13 24 Jan '04  
GeneralRe: Change Headers in BeforeNavigate2memberTimWallace8:09 14 Oct '04  
GeneralRe: Change Headers in BeforeNavigate2memberjjohnston11:08 14 Oct '04  
GeneralRe: Change Headers in BeforeNavigate2memberekatz12318:50 25 Mar '05  
GeneralRe: Change Headers in BeforeNavigate2sussVyacheslav Kazarinov22:58 15 Jun '05  
GeneralCan we capture the button click on a web page opened from the Internet Explorer?membershashike17:28 30 Dec '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Oct 2004
Editor: Nishant Sivakumar
Copyright 2003 by SteveFox
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project