I am using the IE webbrowser control to load a web page we install with our app. I want to dynamically change some of the styles the page uses. Our htm disk-based file has a link of type "text/css" with a href that is a relative path to a css file that is also installed with our app. That style sheet in turn has a "@import url(../../css/theme.css)" entry in it.
Sounded simple enough.
Once I have the document loaded by calling the Navigate method on the web browser control I get the IHTMLDocument2 interface and call get_styleSheets. I get back a style sheet collection and walk the collection. I call to get each item in the collection and get the IHTMLStyleSheet interface on the returned item. I then call IHTMLStyleSheet::get_href to get the href and examine it to see if it has "theme.css" in it.
Since one style imported others, I also call IHTMLStyleSheet::get_imports to get any imported sheets and call the same routine (recursively) to walk the sheets and examine the URLs.
When I find the URL I want to replace, I call IHTMLStyleSheet::put_href with the relative path to the style sheet I want (it is in the same directory as theme.css so I just replace the name leaving the path intact).
When I am done and the page displays, the style sheet used is still the original style sheet. So after I called put_href, which is returning a HRESULT of S_OK, I have another line of code for debugging where I call get_href again. I expected to see the new href I just set. Instead, the same href comes back.
I tried calling the web browser control's Refresh() method but that made no difference. I also obtained the IHTMLDocument3 interface and called recalc( VARIANT_TRUE ). Still the href is returned as the original href. I have walked all the style sheets and there is only one "theme.css" so I am replacing the only one with the sheet I want. I have examined the string I am passing in to put_href and it is the url that I want.
So why doesn't put_href work as advertised? Why would get_href not return what I set via put_href?
HRESULT WalkStyles( IHTMLStyleSheetsCollectionPtr pStyles ) { if( pStyles ) { IHTMLStyleSheetPtr pSheet; long lNumStyleSheets = 0; pStyles->get_length( &lNumStyleSheets ); for( long index = 0; index < lNumStyleSheets; ++index ) { IDispatchPtr pDispStyle; _variant_t vIndex(index); _variant_t vDisp; pStyles->item( &vIndex, &vDisp ); if( pDispStyle = vDisp ) { pSheet = pDispStyle; BSTR bstrhref = 0; BSTR bstrnewhref = 0; pSheet->get_href( &bstrhref ); CString strThisCSS = bstrhref; if( !strThisCSS.IsEmpty() ) { strThisCSS.MakeLower(); int FindIndex = strThisCSS.Find(L"theme.css"); if( -1 != FindIndex ) { strThisCSS.Replace(L"theme.css", L"themeblack.css"); _bstr_t bstrNewStyleURL = strThisCSS; pSheet->put_href( bstrNewStyleURL ); // No change! Debug code - call again to get the href. pSheet->get_href( &bstrnewhref ); } } IHTMLStyleSheetsCollectionPtr pImportStyles; pSheet->get_imports( &pImportStyles ); if( pImportStyles ) { WalkStyles( pImportStyles ); } if( bstrhref ) SysFreeString( bstrhref ); bstrhref = 0; if( bstrnewhref ) SysFreeString( bstrnewhref ); bstrnewhref = 0; } } } return S_OK; }
R.D. Holland