是啊,分析一个链接好麻烦的,这是freenet控件中包的一个显示邮件内容的控件的代码
,在程序员大本营光盘中可以找到。在显示一个链接时是用判断http://这样来做,然后再在这行字的下面画一条线。
procedure TStringsViewer.PaintCanvas(Sender: TObject);
var
line,x,y,z,maxl,tx,t: integer;
i,j: integer;
ocb,ocf: TColor;
ofs: TFontStyles;
ps,s: string;
Switched: Boolean;
begin
for i:=1 to NumLinks do FreeMem(Links,Sizeof(TSVLink));
NumLinks:=0;
if (MkL1<MkL0) or ((MkL1=MkL0) and (Mkc1<Mkc0)) then
begin
t:=MkL0;
MkL0:=MkL1;
MkL1:=t;
t:=Mkc0;
Mkc0:=Mkc1;
Mkc1:=t;
Switched:=True;
end
else Switched:=False;
with Graphic.Canvas do
begin
Font.Assign(FFont);
Brush.Color:=FColor;
Line:=VScrollBar.Position;
y:=0;
z:=0;
x:=-HScrollBar.Position*fdx;
maxl:=0;
while (Line<FLines.Count) and (y<Graphic.Height) do
begin
s:=Lines[Line];
if not (CanMark or Marked) then TabbedTextOut(Graphic.Canvas.Handle,x,y,@s[1],Length(s),0,z,0)
else
begin
if (Line<MkL0) or (Line>MkL1) then
TabbedTextOut(Graphic.Canvas.Handle,x,y,@s[1],Length(s),0,z,0);
tx:=x;
if Line=MkL0 then
begin
if MkL1=Line then
begin
ps:=Copy(s,1,Mkc0-1);
TabbedTextOut(Graphic.Canvas.Handle,tx,y,@ps[1],Length(ps),0,z,0);
Inc(tx,LOWORD(GetTabbedTextExtent(Graphic.Canvas.Handle,@ps[1],
Length(ps),0,z)));
ocb:=Brush.Color;
ocf:=Font.Color;
Brush.Color:=clHighlight;
Font.Color:=clHighlightText;
ps:=Copy(s,Mkc0,Mkc1-Mkc0);
TabbedTextOut(Graphic.Canvas.Handle,tx,y,@ps[1],Length(ps),0,z,0);
Inc(tx,LOWORD(GetTabbedTextExtent(Graphic.Canvas.Handle,@ps[1],
Length(ps),0,z)));
Brush.Color:=ocb;
Font.Color:=ocf;
ps:=Copy(s,Mkc1,999);
TabbedTextOut(Graphic.Canvas.Handle,tx,y,@ps[1],Length(ps),0,z,0);
end
else
begin
ps:=Copy(s,1,Mkc0-1);
TabbedTextOut(Graphic.Canvas.Handle,tx,y,@ps[1],Length(ps),0,z,0);
Inc(tx,LOWORD(GetTabbedTextExtent(Graphic.Canvas.Handle,@ps[1],
Length(ps),0,z)));
ocb:=Brush.Color;
ocf:=Font.Color;
Brush.Color:=clHighlight;
Font.Color:=clHighlightText;
ps:=Copy(s,Mkc0,999);
TabbedTextOut(Graphic.Canvas.Handle,tx,y,@ps[1],Length(ps),0,z,0);
Brush.Color:=ocb;
Font.Color:=ocf;
end;
end;
if (Line>MkL0) and (Line<MkL1) then
begin
ocb:=Brush.Color;
ocf:=Font.Color;
Brush.Color:=clHighlight;
Font.Color:=clHighlightText;
TabbedTextOut(Graphic.Canvas.Handle,tx,y,@s[1],Length(s),0,z,0);
Brush.Color:=ocb;
Font.Color:=ocf;
end;
if ((Line=MkL1) and (Line<>MkL0)) then
begin
ocb:=Brush.Color;
ocf:=Font.Color;
Brush.Color:=clHighlight;
Font.Color:=clHighlightText;
ps:=Copy(s,1,Mkc1);
TabbedTextOut(Graphic.Canvas.Handle,tx,y,@ps[1],Length(ps),0,z,0);
Inc(tx,LOWORD(GetTabbedTextExtent(Graphic.Canvas.Handle,@ps[1],Length(ps),0,z)));
Brush.Color:=ocb;
Font.Color:=ocf;
ps:=Copy(s,Mkc1+1,999);
TabbedTextOut(Graphic.Canvas.Handle,tx,y,@ps[1],Length(ps),0,z,0);
end;
end;
{draw link}
s:=Lines[Line];
tx:=x;
while Pos('http://',s)<>0 do
begin
i:=Pos('http://',s);
ps:=Copy(s,1,i-1);
Inc(tx,LOWORD(GetTabbedTextExtent(Graphic.Canvas.Handle,@ps[1],Length(ps),0,z)));
Delete(s,1,i-1);
i:=Pos(' ',s);
j:=Pos(#9,s);
if (j<>0) and ((j<i) or(i=0)) then i:=j;
if i=0 then i:=Length(s)+1;
ps:=Copy(s,1,i-1);
ocb:=Font.Color;
Font.color:=clBlue;
ofs:=Font.Style;
Font.Style:=[fsUnderline];
TextOut(tx,y,ps);
Inc(NumLinks);
GetMem(Links[NumLinks],Sizeof(TSVLink));
Links[NumLinks]^.Link:=ps;
Links[NumLinks]^.X0:=tx;
Links[NumLinks]^.Y0:=y;
Inc(tx,TextWidth(ps));
Links[NumLinks]^.X1:=tx;
Links[NumLinks]^.Y1:=y+fdy;
Font.Color:=ocb;
Font.Style:=ofs;
Delete(s,1,i-1);
end;
if Length(s)>maxl then maxl:=Length(s);
Inc(y,fdy);
Inc(Line);
end;
HScrollBar.Max:=maxl;
end;
if Switched then
begin
t:=MkL0;
MkL0:=MkL1;
MkL1:=t;
t:=Mkc0;
Mkc0:=Mkc1;
Mkc1:=t;
end;
end;