在Delphi RIO应用程序表单上有一个Align = alClient的Webbrowser1。滚动浏览Web浏览器的内容,因此显示的第一行不是Web浏览器内容的第一行。在这种情况下,当调整表单大小时,Web浏览器的第一个显示行会更改,这带来了不便。 在调整大小之前,我得到了第一个显示的htmlelement,并在调整大小后尝试将其滚动到视图中。 但是调整大小时,第一条显示的行仍在更改。
FirstDisplayedElement1:IHTMLElement;
// is declared in private section of TFormText
procedure TFormText.FormCanResize(Sender: TObject; var
NewWidth, NewHeight: Integer; var Resize: Boolean);
begin
Resize:=True;
FirstDisplayedElement1 :=
GetFirstDisplayedElement(WebBrowser1);
end;
Function TFormText.GetFirstDisplayedElement(WebBr:TWebBrowser):IHTMLELEMENT;
var
ChildElems :IHTMLElementCOLLECTION;
Element:IHTMLElement;
iHTMLDoc: IHtmlDocument2;
Element2:IHTMLElement2;
ElemRect:IHTMLRect;
i:integer;
begin
Result:=Nil;
if Supports(WebBr.Document, IHtmlDocument2, iHTMLDoc)
then
begin
ChildElems:=(WebBr.Document as IHTMLDocument2).all;
for i := 0 to ChildElems.length - 1 do
begin
Element := ChildElems.item(i, varEmpty) as IHTMLElement;
Element2 := ChildElems.item(i, varEmpty) as
IHTMLElement2;
ElemRect:=Element2.getBoundingClientRect;
if ElemRect.Top > 0 then
begin
Result:=Element;
Exit;
end;
end;
end;
end;
procedure TFormText.FormResize(Sender: TObject);
begin
if FirstDisplayedElement1<> NIL then
FirstDisplayedElement1.scrollIntoView(True);
end;
我们如何保持显示的第一行不变?
答案 0 :(得分:0)
在发生大小调整事件时,将过程FormCanResize调用三遍。 ResizeStep变量仅在FormCanResize首次执行时用于一次调用GetFirstDisplayedElement(WebBrowser1)。 这样就行了。
ResizeStep:Integer;
FirstDisplayedElement1:IHTMLElement;
// are declared in private section of TFormText
// and are set to 0 and Nil in FormCreate
procedure TFormText.FormCanResize(Sender: TObject; var NewWidth,
NewHeight: Integer; var Resize: Boolean);
begin
Resize:=True;
if ResizeStep = 0 then
begin
inc(ResizeStep);
FirstDisplayedElement1:=GetFirstDisplayedElement(WebBrowser1);
end;
if ResizeStep > 1 then ResizeStep := 0;
end;
procedure TFormText.FormResize(Sender: TObject);
begin
inc(ResizeStep);
if FirstDisplayedElement1<> NIL then FirstDisplayedElement1.scrollIntoView(True);
end;
Function TFormText.GetFirstDisplayedElement(WebBr:TWebBrowser):IHTMLELEMENT;
var
ChildElems :IHTMLElementCOLLECTION;
Element:IHTMLElement;
i:integer;
begin
Result:=Nil;
if Supports(WebBr.Document, IHtmlDocument2, iHTMLDoc) then
begin
ChildElems:=(iHTMLDoc.body.children AS IHTMLElementCOLLECTION);
for i := 0 to ChildElems.length - 1 do
begin
Element := ChildElems.item(i, varEmpty) as IHTMLElement;
if ((Element.offsetTop +Element.offsetHeight) > ((IHTMLDoc.body AS HTMLBODY).scrolltop)) then
begin
Result:=Element;
Exit;
end;
end;
end;
end;