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 firedac 下的 sqlite [9] - 关于排序

Delphi with 语句的妙用

Delphi runtime error 216 程序退出后弹出错误

Delphi根据url获取缓存文件的方法

Delphi 判断网络链接文件是否存在

ttreeview的两个事件ondragdrop、ondragover 实现自动拖放功能

Delphi 在 webservice 中采用 tsoapattachment 传输文件

Delphi 的tjpegimage跟image区别

Delphi 分离网址链接最后一层文件目录

Delphi winexec 执行cmd

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



打赏

取消

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

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

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

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

评论

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