2016-10-14 8 views
0

私はDelphi 2010でWebブラウザでテキストを見つけようとしています。コードはテキストを見つけてそれにスクロールしますが、テキストは最後の行のwebviewの下部にとどまります。私はテキストをwebviewの最初の行に表示したいと思います。Delphi Webbrowser scrollIntoView(true)が動作しません

私はこれが "scrollIntoView(true)"となると思いますが、私がやろうとしていることには何の影響もありません。

どうすればよいですか?ありがとう。ここに私のコードです

procedure TForm1.SpeedButton10Click(Sender: TObject); 
var 
    doc: IHTMLDocument2; 
    selection: IHTMLSelectionObject; 
    textRange: IHtmlTxtRange; 
    scrollpos: Integer; 
    Art : string; 
begin 

Doc := WebBrowser1.Document as IHTMLDocument2; 
Selection := Doc.Selection; 
TextRange := selection.createRange as IHTMLTxtRange; 

Art := edit2.Text; 

TextRange.collapse(false); 
if TextRange.findText(Art) then 
begin 
TextRange.select; 
TextRange.scrollIntoView(true); 

end; 
end; 

答えて

2

私はTextRange.scrollIntoView(True)を動作させることができませんでした。ただし、以下のコードは、形式

の文書のために働くように見えるライン1
回線2
LINE3
LINE4
LINE5
...
ライン100

提供

ドキュメントは、Line100がブラウザウィンドウの一番下の行にある点までスクロールされません。それはLine20を見つけ、それをブラウザーウィンドウの上部に置くために、f.i.でうまくいきます。

あなたが見ることができるように、それはTextRangeからIHTMLTextRangeMetricsインターフェイスを取得し、垂直doc2の親ウィンドウをスクロールするために、そのoffsetTopプロパティを使用して動作します。

コード:

// doc2 is a field of Form1 of type `IHTMLDocument2` 
procedure TForm1.FindText(Text : String); 
var 
    selection: IHTMLSelectionObject; 
    textRange: IHtmlTxtRange; 
    scrollpos: Integer; 
    Metrics : IHTMLTextRangeMetrics; 
begin 

    Selection := Doc2.Selection; 
    TextRange := selection.createRange as IHTMLTxtRange; 

    TextRange.collapse(false); 
    if TextRange.findText(Text, 1, 0) then begin 
    TextRange.select; 
    TextRange.scrollIntoView(True); 
    TextRange.QueryInterface(IHTMLTextRangeMetrics, Metrics); 
    if Metrics <> Nil then 
     doc2.parentWindow.scrollBy(0, Metrics.offsetTop); 
    end; 
end; 
+0

はどうもありがとうございました!私はなぜ、コードが2回実行された場合にのみ動作するのかわかりません。しかし、それは動作します!どうもありがとうございます! –

関連する問題