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中quotedstr介绍及使用

Delphi 一个call应该如何写?

Delphi随机字符(密码生成)函数

Delphi firedac 下的 sqlite [1] - 前言

Delphi根据网络链接截取域名

Delphi 获取系统当前活动窗口的句柄及对应的进程名

Delphi 如何用程序控制禁止和起用上网?

Delphi加载驱动的代码演示

Delphi 读写文本

Delphi 读取utf-8格式的文件内容

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



打赏

取消

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

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

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

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

评论

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