你们怎么啦?动不动就用钩子函数,真不愧是程序员出身。
其实这个问题仅仅是要在richedit里面实现,那就简单多了,我来写出原码把!
1、添加form,在上面添加一个richedit,名字叫rich1
2、在form的onload事件中往rich1里面随便添加一段文字做测试用:
procedure TForm1.FormCreate(Sender: TObject);
begin
rich1.Lines.Add('cAkk is a good boy');
end;
3、定义rich1的onmousemove事件:
procedure TForm1.rich1MouseMove(Sender: TObject;
Shift: TShiftState;
X,Y: Integer);
var txt:String;
begin
txt:= RichWordOver(rich1, X, Y);
//截词结果显示在form的caption上
If Caption <> txt then
Caption:= txt;
end;
4、定义函数RichWordOver
Function TForm1.RichWordOver(rch:trichedit;
X,Y:integer):string;
var pt:tpoint;
pos,
start_pos,
end_pos:Integer;
ch,txt:String;
txtlen:Integer;
begin
pt.X:= X;
pt.Y:= Y;
pos:=rch.Perform(EM_CHARFROMPOS, 0, longint(@pt));
If pos <= 0 then
Exit;
txt:= rch.Text;
For start_pos:= posdo
wnTo 1do
begin
ch:=copy(rch.text,start_pos, 1);
If Not (
( (ch >= '0') And (ch <= '9') ) Or
( (ch >= 'a') And (ch <= 'z') ) Or
( (ch >= 'A') And (ch <= 'Z') ) Or
(ch = '_')
) then
break;
end;
inc(start_pos);
txtlen:= Length(txt);
For end_pos:= pos To txtlendo
begin
ch:= copy(txt, end_pos, 1);
If Not (
( (ch >= '0') And (ch <= '9') ) Or
( (ch >= 'a') And (ch <= 'z') ) Or
( (ch >= 'A') And (ch <= 'Z') ) Or
(ch = '_')
) then
break;
end;
dec(end_pos);
If start_pos <= end_pos then
result:=copy(txt, start_pos, end_pos - start_pos + 1);
end;
其实很简单,不是吗?
200分到手啦!哈哈!!