You are currently offline, waiting for your internet to reconnect

How To Change WebBrowser Properties Stored in Property Bag

This article was previously published under Q197921
This article has been archived. It is offered "as is" and will no longer be updated.
The WebBrowser control exposes certain properties such as LocationURL andRegisterAsBrowser through normal COM property methods. Other properties,such as AutoArrange, are stored only in the property bag and no methods areexposed for these properties. Therefore, these properties can only be setin the property bag.

This article describes how to set the WebBrowser properties stored in theproperty bag in Visual Basic and Visual C++ applications that are hostingthe WebBrowser control.
More information
In Visual Basic applications that are hosting the WebBrowser control,changing the values of the properties stored in the property bag isrelatively easy. Visual Basic sets the values of these properties in the.frm file for the form that contains the WebBrowser control.

Here is a portion of a .frm file for a Visual Basic application that ishosting the WebBrowser control:
Begin SHDocVwCtl.WebBrowser WebBrowser1   Height          =   3015   Left            =   240   TabIndex        =   0   Top             =   120   Width           =   5655   ExtentX         =   9975   ExtentY         =   5318   ViewMode        =   1   Offline         =   0   Silent          =   0   RegisterAsBrowser=   0   RegisterAsDropTarget=   1   AutoArrange     =   0  'false   NoClientEdge    =   0   'False   AlignLeft       =   0   'False   ViewID          =   "{0057D0E0-3573-11CF-AE69-08002B2E1262}"   Location        =   ""   End				
You will notice that some of the properties written to this file, such asRegisterAsBrowser and RegisterAsDropTarget, have associated COM propertymethods exposed by the WebBrowser control. You can change the values ofthese properties in the Visual Basic Property window or at run time usingWebBrowser methods. Others, such as AutoArrange and ViewMode can be changedonly by editing this .frm file. For information regarding which propertiescan be set through WebBrowser methods, please see the "Reusing BrowserTechnology" section of the MSDN Online Workshop that islisted in the REFERENCES section of this article.

In order to set properties stored in the property bag in a C++ application,there are a few things you have to do. (Note that the sample C++ code inthis article sets the AutoArrange property to true so that when navigatingto a folder, all folder items will be arranged automatically.)
  1. Your application must implement IPropertyBag. For example, if you are hosting the WebBrowser control in an ATL application you can implement this interface simply by inheriting from IPropertyBag, entering it into your COM map, and implementing its methods like this:
       class ATL_NO_VTABLE CAtlBrowser :     public CComObjectRootEx<CComSingleThreadModel>,     public CComCoClass<CAtlBrowser, &CLSID_CAtlBrowser>,     public CWindowImpl<CAtlBrowser>,     public IDispatchImpl<IAtlBrowser, &IID_IAtlBrowser,                       &LIBID_ATLBROWSERLib>,     public IOleClientSite,     public IOleInPlaceSite,     public IPropertyBag     //  <-- Inheriting from IPropertyBag   {   public:   // COM Map   BEGIN_COM_MAP(CAtlBrowser)      COM_INTERFACE_ENTRY(IAtlBrowser)      COM_INTERFACE_ENTRY(IDispatch)      COM_INTERFACE_ENTRY(IOleClientSite)      COM_INTERFACE_ENTRY(IOleWindow)      COM_INTERFACE_ENTRY(IOleInPlaceSite)      COM_INTERFACE_ENTRY(IPropertyBag)  // <-- COM map entry   END_COM_MAP()   // IPropertyBag Methods   STDMETHOD(Read)(LPCOLESTR pszPropName, VARIANT* pVar,                   IErrorLog* pErrorLog)   {      USES_CONVERSION;      switch(pVar->vt)      {      case VT_BOOL:         if (!strcmp(OLE2T(pszPropName), "AutoArrange"))            pVar->boolVal = VARIANT_TRUE;         break;      default:         break;      };      return S_OK;   }   STDMETHOD(Write)(LPCOLESTR pszPropName, VARIANT* pVar)   {      USES_CONVERSION;      ATLTRACE("%s ", OLE2T(pszPropName));      switch(pVar->vt)      {      case VT_I4:        ATLTRACE("%ld\n", pVar->lVal);        break;      case VT_BSTR:         ATLTRACE("%s\n", OLE2T(pVar->bstrVal));         break;      case VT_BOOL:         ATLTRACE("%hu\n", pVar->boolVal);         break;      default:         ATLTRACE("\n");         break;      };      return S_OK;   }   };					
  2. Immediately after creating the WebBrowser control, call QueryInterface to retrieve a pointer to the WebBrowser control's property bag - IPersistPropertyBag.
  3. After you retrieve a pointer to the property bag, tell the WebBrowser control to load its properties from your IPropertyBag interface by calling IPersistPropertyBag::Load() and passing it a pointer to your implementation of IPropertyBag.
When you call the Load method, the WebBrowser control will call yourIPropertBag::Read() method once for every property that it is storing inits property bag. It is in your Read method where you can set the values ofproperties stored in the property bag as illustrated in number 1 above.

Note that IPersistPropertyBag::Load can be called only once. Any subsequentcalls to Load will fail with an HRESULT of E_UNEXPECTED.

You can instruct the WebBrowser control to save some or all of itsproperties to your IPropertyBag implementation by callingIPersistPropertyBag::Save(). When you call this function, the WebBrowsercontrol will call your IPropertyBag::Write() method once for every propertyin the property bag. You can call IPersistPropertyBag::Save() at any timeto instruct the WebBrowser control to save it's properties to yourIPropertyBag implementation.

The following is ATL source code that creates an instance of the WebBrowsercontrol, retrieves the WebBrowser control's IPersistPropertyBag, andinstructs the WebBrowser control to load its properties and then save them.
   //    // Create the WebBrowser control   //    CComPtr<IOleObject> spOleObject;   HRESULT hr = CoCreateInstance(CLSID_WebBrowser, NULL, CLSCTX_INPROC,                              IID_IOleObject, (void**)&spOleObject);   if (FAILED)   return hr;   // Set the WebBrowser's properties   IPersistPropertyBag* pPPBag;   hr = spOleObject->QueryInterface(IID_IPersistPropertyBag,                                 (void**)&pPPBag);   if (SUCCEEDED(hr))    {   hr = pPPBag->Load(this, NULL);   hr = pPPBag->Save(this, FALSE, TRUE);   pPPBag->Release();   }				
NOTE: Setting the WebBrowser properties stored in the property bag from an MFC application is possible, but extremely difficult. As mentioned prior,IPersistPropertyBag::Load() can be called only once. Also, this methodcannot be called after IPersistPropertyBag::InitNew(). When hosting theWebBrowser control in an Microsoft Foundation Classes (MFC) applicationusing a wrapper class (CWebBrowser2) or CHTMLView, the MFC libraries willcall either InitNew() or Load() when creating the WebBrowser control. Whichmethod is called depends on a number of conditions. This means that whenyou call IPersistPropertyBag::Load from your application, it fails with anHRESULT of E_UNEXPECTED.

Therefore, if you wish to set the WebBrowser properties that are stored inthe property bag, you must create and site the WebBrowser control manually.You cannot use a wrapper class (CWebBrowser2) or CHTMLView.
IPersistPropertyBag and IPropertyBag in the Microsoft Developer Network(MSDN).

"Reusing Browser Technology" in the MSDN Online Web Workshop:(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Scott Roberts, Microsoft Corporation.


Article ID: 197921 - Last Review: 11/02/2013 22:38:00 - Revision: 4.0

Microsoft Internet Explorer 4.01 Service Pack 1

  • kbnosurvey kbarchive kbhowto kbwebbrowser KB197921