抄cAkk的<br>1、添加form,在上面添加一个richedit,名字叫rich1<br>2、在form的onload事件中往rich1里面随便添加一段文字做测试用:<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> rich1.Lines.Add('cAkk is a good boy');<br>end;<br><br>3、定义rich1的onmousemove事件:<br>procedure TForm1.rich1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);<br>var txt:String;<br>begin<br> txt:= RichWordOver(rich1, X, Y);<br> //截词结果显示在form的caption上<br> If Caption <> txt Then<br> Caption:= txt;<br>end;<br><br>4、定义函数RichWordOver<br><br>Function TForm1.RichWordOver(rch:trichedit; X,Y:integer):string;<br>var pt:tpoint;<br> pos,<br> start_pos,<br> end_pos:Integer;<br> ch,txt:String;<br> txtlen:Integer;<br>begin<br><br> pt.X:= X;<br> pt.Y:= Y;<br> pos:=rch.Perform(EM_CHARFROMPOS, 0, longint(@pt));<br> If pos <= 0 Then Exit;<br> txt:= rch.Text;<br> For start_pos:= pos downTo 1 do<br> begin<br> ch:=copy(rch.text,start_pos, 1);<br> If Not (<br> ( (ch >= '0') And (ch <= '9') ) Or<br> ( (ch >= 'a') And (ch <= 'z') ) Or<br> ( (ch >= 'A') And (ch <= 'Z') ) Or<br> (ch = '_')<br> ) Then break;<br> end;<br><br> inc(start_pos);<br><br> txtlen:= Length(txt);<br> For end_pos:= pos To txtlen do<br> begin<br> ch:= copy(txt, end_pos, 1);<br> If Not (<br> ( (ch >= '0') And (ch <= '9') ) Or<br> ( (ch >= 'a') And (ch <= 'z') ) Or<br> ( (ch >= 'A') And (ch <= 'Z') ) Or<br> (ch = '_')<br> ) Then break;<br> end;<br> dec(end_pos);<br><br> If start_pos <= end_pos Then<br> result:=copy(txt, start_pos, end_pos - start_pos + 1);<br>End;<br>