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的字符串与16进制的相互转换函数的汇编代码

Delphi dateutils.ispm - 判断时间是否是下午

Delphi 2009 中 tstrings 与 tstream 的增强

Delphi olevariant excel保存文件

Delphi中使用自定义字体

Delphi制作外挂的操作技巧

Delphi 如何确定windows安装的语言

Delphi cxgrid 通过字段名取得列

Delphi跨平台的字符串代码标准

Delphi dbgrideh 的分组统计 datagrouping

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



打赏

取消

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

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

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

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

评论

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