Memo 控件光标定位


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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值