Я не могу дать вам версию WinForms напрямую, но я могу дать вам версию, которую мы использовали из управляемого кода.
Но я действительно хотел сказать, что вы должны быть осторожны, чтобы знать, находится ли ваш документ в
- Режим стандартов : то есть Internet Explorer 6 или новее
- Режим причуда : т.е. режим, совместимый с Internet Explorer 5.5
Причина, по которой вы хотите это знать, заключается в том, что атрибут, который вы хотите запросить, зависит от того, находится ли браузер в режиме стандартов или в режиме причуд. Документ в режиме причуд не поддерживает documentElement.scrollHight
(т.е. он возвращает ноль).
returns the height of...
Expression Quirks Mode Standards Mode
===================================== ========================== ==========================
document.body.scrollHeight document body
document.documentElement.scrollHeight (not supported) document
Вы хотите высоту документа , поэтому способ возврата таков:
- Режим стандартов:
document.documentElement.scrollHeight
(возвращает высоту документа)
- Режим причуд:
document.body.scrollHeight
(фактически возвращает высоту документа, а не тела)
Итак, у меня есть две вспомогательные функции:
int WebBrowserScrollHeightQuirksMode(WebBrowser wb);
int WebBrowserScrollHeightStandardsMode(WebBrowser wb);
Что заставляет вас выяснить, находится ли ваш html в Netscape 2.0 совместимом режиме, и выяснить, какой из них вы хотите вызвать:
function WebBrowserScrollHeightQuirksMode(const WebBrowser: TWebBrowser): Integer;
var
doc: IHTMLDocument2;
begin
{
Expression Mode Returns ScrollHeight of
document.body.scrollHeight Quirks document
document.body.scrollHeight Standards body
document.documentElement.scrollHeight Standards document
We *want* the scroll height of the document, which means we should have been
using document.documentElement.scrollHeight
WARNING: A document in quirks mode does not support documentElement[scrollHight]
i.e. it returns zero
}
doc := (WebBrowser.Document as IHTMLDocument2);
if not Assigned(doc) then
Result := 0
else if not Assigned(doc.body) then
Result := 0
else
Result := doc.body.getAttribute('scrollHeight', 0);
end;
И версия, когда HTML находится в стандартном режиме (т. Е. Internet Explorer 6 или новее):
function WebBrowserScrollHeightStandardsmode(const WebBrowser: TWebBrowser): Integer;
var
doc: IHTMLDocument2; //ie4, has doc.body
doc3: IHTMLDocument3; //ie5, has doc.documentElement
begin
//Use this for IE6 or newer, with the browser in standards mode
Result := 0;
{
Expression Mode Returns ScrollHeight of
===================================== ========= =======================
document.body.scrollHeight Quirks document
document.body.scrollHeight Standards body
document.documentElement.scrollHeight Standards document
We *want* the scroll height of the document, which means we should have been
using document.documentElement.scrollHeight
WARNING: A document in quirks mode does not support documentElement[scrollHight]
i.e. it returns zero
}
doc := (WebBrowser.Document as IHTMLDocument2);
if not Assigned(doc) then
Exit;
//Should use doc3.documentElement scrollHeight
if Supports(doc, IHTMLDocument3, doc3) then //Requires Windows 95, IE5
begin
if Assigned(doc3.documentElement) then
begin
Result := VarAsInteger(doc3.documentElement.getAttribute('scrollHeight', 0));
Exit;
end;
end;
end;