本文整理自网络,侵删。
LRESULT SendMessage (
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
)
SendMessage( Memo1.Handle, EM_LINEFROMCHAR, Memo1.SelStart, 0); //光标所在的行号
SendMessage( Memo1.Handle, EM_LINEINDEX, row_pos, 0); //光标所在的字符位置
SendMessage( Memo1.Handle, EM_LINELENGTH, col_pos, 0); //这行的字符数.

unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Windows, Classes, SysUtils, FileUtil,
Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Memo1: TMemo;
StatusBar1: TStatusBar;
procedure Memo1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure Memo1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
var
row_pos, col_pos, line_len : Integer;
procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
row_pos := SendMessage( Memo1.Handle, EM_LINEFROMCHAR, Memo1.SelStart, 0 );
col_pos := SendMessage( Memo1.Handle, EM_LINEINDEX, row_pos, 0 );
line_len := SendMessage( Memo1.Handle, EM_LINELENGTH, col_pos, 0 );
col_pos := Memo1.SelStart - col_pos;
Inc( row_pos );
Inc( col_pos );
StatusBar1.SimpleText := '行: ' + IntToStr( row_pos ) + ' ' +
'列: ' + IntToStr( col_pos ) + ' ' +
'此行字数: ' + IntToStr( line_len );
end;
procedure TForm1.Memo1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
row_pos := SendMessage( Memo1.Handle, EM_LINEFROMCHAR, Memo1.SelStart, 0 );
col_pos := SendMessage( Memo1.Handle, EM_LINEINDEX, row_pos, 0 );
line_len := SendMessage( Memo1.Handle, EM_LINELENGTH, col_pos, 0 );
col_pos := Memo1.SelStart - col_pos;
Inc( row_pos );
Inc( col_pos );
StatusBar1.SimpleText := '行: ' + IntToStr( row_pos ) + ' ' +
'列: ' + IntToStr( col_pos ) + ' ' +
'此行字数: ' + IntToStr( line_len );
end;
end.
关于 SelStart, SelLength, SelText:

unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Windows, Classes, SysUtils, FileUtil,
Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Memo1: TMemo;
procedure Memo1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Memo1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
Edit1.Text := IntToStr( Memo1.SelStart );
Edit2.Text := IntToStr( Memo1.SelLength );
Edit3.Text := Memo1.SelText;
end;
end.
来源:https://blog.csdn.net/pandora_madara/article/details/39083865
相关阅读 >>
Delphi 系统对话框(如浏览目录)被隐藏到主窗体后面造成程序无法操作的临时处理方式
Delphi用mapfileandchecksum 函数检测 exe 或 dll 是否被修改
Delphi d10.x 并行库ppl编程之 futures
更多相关阅读请进入《Delphi》频道 >>