Delphi RichEdit根据鼠标位置定位光标的方法


本文整理自网络,侵删。

 

当RichEdit为可用状态时,是不用代码控制此操作的,TRichEdit本身就可以定位光标。但是当RichEdit开始不可用时,则此方法就可能用到了。本例中RichEdit1开始时是不可用的,当在ApplicationEvents1中接收到RichEdit1被双击时则设置RichEdit1的Enable := true;并且定位光标。

unit Unit1;

 

interface

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ComCtrls, RichEdit, AppEvnts;

 

type

  TForm1 = class(TForm)

    RichEdit1: TRichEdit;

    ApplicationEvents1: TApplicationEvents;

    procedure EnableRichEdit;

    procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

 

var

  Form1: TForm1;

  FLastMousePos: TPoint;

 

implementation

 

{$R *.dfm}

 

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;

  var Handled: Boolean);

var

  mPos: TPoint;

begin

  if msg.message = WM_LBUTTONDBLCLK then begin

    if msg.hwnd = RichEdit1.Parent.Handle then begin

      GetCursorPos(FLastMousePos);

      EnableRichEdit;

    end;

  end ;

end;

 

procedure TForm1.EnableRichEdit;

var

  cPos: TPoint;

  retCode: integer;

  mR, mC: Word;

begin

  RichEdit1.Enabled := True;

  RichEdit1.SetFocus ;

  RichEdit1.SelStart := 0;

 

  if (FLastMousePos.X = 0) and (FLastMousePos.Y = 0) then

    GetCursorPos(FLastMousePos);

  FLastMousePos := RichEdit1.ScreenToClient(FLastMousePos);

  retCode := SendMessage(RichEdit1.Handle, EM_CHARFROMPOS, 0, LPARAM(@FLastMousePos));

  mR := HiWord(retCode);

  mC := Loword(retCode);

  cPos.X := mC ;

  cPos.Y := mR ;

  RichEdit1.CaretPos := cPos;

 

  FLastMousePos.X := 0;

  FLastMousePos.Y := 0;

end;

 

end.

相关阅读 >>

Delphi 利用vclzip实现分卷压缩

Delphi xe6取得android智能手机的电话号码等的终端信息

Delphi readprocessmemory 输入进程id 输入读取地址

Delphi基于sobel算子的图像边缘检测

Delphi 判断文件是否存在

Delphi 获取计算机已运行时间

Delphi memo控件对粘贴板的支持

Delphi取得当前目录的上一级目录

Delphi 中的颜色

Delphi在设计时设置tstringgrid控件各列的列宽

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



打赏

取消

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

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

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

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

评论

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