首页 > 代码库 > Memo 控件光标定位

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.


Memo 控件光标定位