HOWTO: Detect When Internet Explorer Holds Controls and Pages in Memory |
Q167230
Internet Explorer maintains a most-recently used memory cache of up to four viewed pages in order to optimize navigation back and forth between the same set of pages. What this means to the Web site programmer is that ActiveX controls on a cached page stay loaded in memory for as long as that page remains in the "memory cache." Likewise, ActiveX Document servers remain open as long as that page is still held in memory.
For most controls and documents, this is not an issue. This optimization
improves performance for browsing the Web site. However, an ActiveX control
might need to know when its host Web document is no longer visible. For
example, an ActiveX control that plays a background sound might want to
turn that sound off when its host page is no longer visible. All forms of
navigation demonstrate this behavior--clicking the Back or Forward buttons,
typing a URL directly, and clicking on a link.
For ActiveX documents, the recommended method for determining that a page
has just been hidden is to respond to the OLECMDID_STOP command sent
through the IOleCommandTarget interface. MFC ActiveX document servers make
it easy to detect this event: just add an entry to your OLECMD_MAP for
OLECMDID_STOP. Use this entry to map OLECMDID_STOP to a command such as
ID_OLECMD_STOP, which you can handle as usual.
When the ActiveX document is refreshed from the memory cache,
OLECMDID_REFRESH is sent to IOleCommandTarget::Exec. This can be handled in
an analogous fashion to OLECMDID_STOP. Here is an MFC example of using
both:
BEGIN_MESSAGE_MAP(CYourDoc, COleServerDoc)
// ... other stuff
// Add the two following entries to your control's message map.
// Then, define ID_OLECMD_STOP and ID_OLECMD_REFRESH command
// IDs in resource.h file. They are for OLECMDID_STOP
// and OLECMDID_REFRESH command.
ON_COMMAND(ID_OLECMD_STOP, OnStop)
ON_COMMAND(ID_OLECMD_REFRESH, OnRefresh)
END_MESSAGE_MAP()
// Add the following OLECMD_MAP to the .cpp file of your
// COleServerDoc-derived class:
BEGIN_OLECMD_MAP(CYourDoc, COleServerDoc)
ON_OLECMD(NULL, OLECMDID_STOP, ID_OLECMD_STOP)
ON_OLECMD(NULL, OLECMDID_REFRESH, ID_OLECMD_REFRESH)
END_OLECMD_MAP()
// Add DECLARE_OLECMD_MAP() to the .h file of your
// COleServerDoc-derived class, and declarations and
// definitions for the following two functions:
oid CYourDoc::OnStop()
{
// This control's page was navigated away from, but still remains in
// the memory cache. Stop playing background sounds, animations,
// timers.
TRACE("Stopped!\n");
}
oid CYourDoc::OnRefresh()
{
// This control's page was navigated to and is being displayed
// again. Do whatever is necessary: start playing sounds,
// animations, timers
TRACE("Refreshed!\n");
}
BEGIN_MESSAGE_MAP(CYourOcxCtrl, COleControl)
// ... other stuff
// Add the two following entries to your control's message map
ON_STDOLEVERB(OLEIVERB_SHOW, OnVerbShow)
ON_STDOLEVERB(OLEIVERB_HIDE, OnVerbHide)
END_MESSAGE_MAP()
// Add definitions and declarations for the following two functions.
BOOL CYourOcxCtrl::OnVerbShow(LPMSG lpMsg, HWND hWndParent,
LPCRECT lpRect)
{
// Do what COleControl would have done in default case
// taken from COleControl::XOleObject::DoVerb in CTLOBJ.CPP
HRESULT hr = OnOpen(TRUE, lpMsg); // Try in-place first
// This control's page was navigated to and is being displayed
// Do whatever is necessary: start playing sounds,
// animations, timers
TRACE("VERB: SHOW\n");
return !(FAILED(hr));
}
BOOL CYourOcxCtrl::OnVerbHide(LPMSG lpMsg, HWND hWndParent,
LPCRECT lpRect)
{
// This control's page was navigated away from, but still remains in
// the memory cache. Stop playing background sounds, animations,
// timers.
TRACE("VERB: HIDE\n");
// Do what COleControl would have done in default case
// taken from COleControl::XOleObject::DoVerb in CTLOBJ.CPP
HRESULT hr = m_xOleInPlaceObject.UIDeactivate();
return !(FAILED(hr));
} For information on ActiveX Document and ActiveX Control, search the following topics in Microsoft Developer Studio:
Q177269 PRB: Internet Explorer 4.0 Keeps Active Document Servers Running
© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Jason Strayer, Microsoft Corporation
Additional query words:
Keywords : kbprogramming msiew95 msient kbActiveX kbDSupport kbieFAQ
Issue type : kbhowto
Technology : kbIEsearch kbAudDeveloper kbSDKIESearch kbSDKIE300 kbSDKIE301 kbSDKIE302
|
Last Reviewed: February 2, 2001 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |