I write down some test code to work with IHTMLDocument2. However, I found the IHTMLDocument2 can’t control the specific HTML document as expected. For example, I create the following code to change background color.BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam) { TCHAR buf[100]; ::GetClassName( hwnd, (LPTSTR)&buf, 100 ); if ( System::String::Compare(buf, "Internet Explorer_Server") == 0 ) { *(HWND*)lParam = hwnd; return FALSE; } else return TRUE; }; void html() { HWND hWnd =FindWindow(NULL, "C:\\Documents and Settings\\v-yhyang\\Desktop\\test.html - Microsoft Internet Explorer"); if ( hWnd != NULL ) { HWND hWndChild=NULL; ::EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&hWndChild ); if ( hWndChild ) { IHTMLDocument2* spDoc; IHTMLElement* ele; LRESULT lRes; HRESULT hr; UINT nMsg = ::RegisterWindowMessage( "WM_HTML_GETOBJECT"); ::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes ); hr = ObjectFromLresult( lRes, IID_IHTMLDocument, 0, (void**)&spDoc ); if ( SUCCEEDED(hr) ) { VARIANT v; v.vt=VT_BSTR; v.bstrVal=L"#ff0000"; IDispatch* spDisp; IHTMLWindow2* spWin; spDoc->get_Script(&spDisp ); spDisp->QueryInterface(IID_IHTMLWindow2,(void**)&spWin); spWin->get_document(&spDoc); spDoc->put_bgColor(v); } } // else document not ready } // else Internet Explorer is not running };And this is the test.html<html><body bgcolor=#3300ff>test</body></html> As you know, the code should change the document background color into “red”. However, I find the background is changed into “White” actually. What’s wrong with that code? If I add the following code at the tail, the background will be correct. spDoc->get_body(&ele);ele->setAttribute(L"bgcolor",v,1) The freak thing is if I comment “spDoc->put_bgColor(v);”, the “spDoc->get_body(&ele);ele->setAttribute(L"bgcolor",v,1)” doesn’t change anything.What’s wrong?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货