Delphi中有控件MEMO,当使用应控件输入内容或编辑时,如何确定光标当前的位置呢?这时就可以调用WINDOWS API中的EM_LINEFROMCHAR和EM_LINEINDEX消息来确定MEMO控件中光标的当前行和列,具体调用方法的过程如下:
procedure TForm1.Button1Click(Sender: TObject);
var
LineNum:longint;
CharsBeforeLine:longint;
begin
LineNum := SendMessage(Memo1.Handle,EM_LINEFROMCHAR,
Memo1.SelStart,0);
CharsBeforeLine:=SendMessage(Memo1.Handle,EM_LINEINDEX,
LineNum,0);
Label1.Caption:='Line: '+IntToStr(LineNum + 1);
Label2.Caption:='Position: '+IntToStr((Memo1.SelStart -
CharsBeforeLine)+1);
end;
//另一种方法
定义变量 iLine: Integer;
{ 使用 em_LineFromChar 消息 }
iLine := Memo1.Perform(em_LineFromChar, $FFFF, 0);
{ 第一行为 0 }
messageDlg(' 行号 : ' + IntToStr(iLine), mtInformation,[mbOK], 0 ) ;