你可能没有真正明白我的意思。我把我的示例程序贴在上面。我的意思是:当你选择一个元素时,
可以把Panel1定位到你所选择的元素的正确位置。比如:你所选择的是“测试7”这个元素时,
可以把Panel1定位到“测试7”这个地方。现在我的问题是:当元素很少时,我的控件不会出现
滚动条,这时候用你现在的方法,可以做到我所要求的功能。但如果元素互很多,出现了滚动条时,
用现在的方法定位的话,定位会出现偏差。
//-----------------------------------------------------------------------------
示例源码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, DHTMLEDLib_TLB,MSHTML_TLB, ExtCtrls;
type
TForm1 = class(TForm)
DH: TDHTMLEdit;
Button1: TButton;
Button2: TButton;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
sString,s:string;
doc:IHTMLdocument2;
R:IHTMLTxtRange;
v:variant;
begin
sString :='测试6';
try
// doc :=IHTMLdocument2(webbrowser1.Document) ;
R :=(DH.DOM.selection.createRange) as IHTMLTxtRange;
R.findText(sString,1,0);
R.select;
v:=r;
panel1.Top :=Integer(v.boundingTop);
panel1.Left :=Integer(v.boundingLeft)+100;
s:=format('Left:%d Top:%d Width:%d Height:%d',
[integer(v.boundingLeft),integer(v.boundingTop),integer(v.boundingWidth),integer(v.boundingHeight)]);
showmessage(s);
except
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Dh.DocumentHTML :='<head>TEST SAMPLE</head>'
+'<body>'
+'问题分类<p>'
+'测试1<p>'
+'测试2<p>'
+'测试3<p>'
+'测试4<p>'
+'测试5<p>'
+'测试6<p>'
+'测试7<p>'
+'</body>';
end;
end.