Delphi 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.

来源:https://blog.csdn.net/pandora_madara/article/details/39083865

相关阅读 >>

Delphi 判断指定字符串是否开头 startstext用法

Delphi 列出所有可视窗口

Delphi实现url编码解码函数

Delphi获取内存及cpu信息的函数

Delphi system.sysutils.tmarshaller 与 system.tmarshal

Delphi获取本地全部盘符并存如combobox1

Delphi 实现检测线程类tthread是否结束

Delphi opendialog文件过滤类型

Delphi实现md5算法

Delphi写的验证身份证号有效性函数

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...