问题来源: http://www.cnblogs.com/del/archive/2008/06/18/1083011.html#1229305

告诉 roy.flex 同学:
你的问题还是挺复杂的, 先要完成的就是这一步;
再往下做, 如果要捕获其他程序中的鼠标需要做 DLL;
不过还有个简单办法, 就是用 TTimer 定时获取, 这样也就不用钩子了;
但是还需要知道要操作对象的句柄, 这个可以参考: http://www.cnblogs.com/del/archive/2008/03/09/1097942.html

本例效果图:



代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Memo1: TMemo;
    RichEdit1: TRichEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{用 API 实现的获取文本容器中选择的文本的函数}
function GetEditSeleteText(h: HWND): string;
var
  len,sx,ex: Integer; {文本总长度, 选择的开始位置, 选择的结束位置}
  buf: PChar;         {所有文本}
begin
  {获取文本总长度}
  len := SendMessage(h, WM_GETTEXTLENGTH, 0, 0) + 1;
  {为接受所有文本的缓冲区分配内存}
  buf := GlobalAllocPtr(0, len); {这里没有使用 GetMem, 因为需要全局的, 不然无法面对其他程序}
  {获取所有文本}
  SendMessage(h, WM_GETTEXT, len, Longint(buf));
  {获取选择的开始位置和结束位置}
  SendMessage(h, EM_GETSEL, Longint(@sx), Longint(@ex));
  {截取选择的文本}
  Result := Copy(buf, sx+1, ex-sx);
  {释放内存}
  GlobalFreePtr(buf);
end;

{测试 TEdit, 同时与 VCL 的获取方法对比}
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(GetEditSeleteText(Edit1.Handle) + ' - ' + Edit1.SelText);
end;

{测试 TMemo, 同时与 VCL 的获取方法对比}
procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(GetEditSeleteText(Memo1.Handle) + ' - ' + Memo1.SelText);
end;

{测试 TRichEdit, 同时与 VCL 的获取方法对比}
procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(GetEditSeleteText(RichEdit1.Handle) + ' - ' + RichEdit1.SelText);
end;

end.

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 145
  ClientWidth = 181
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 95
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 8
    Top = 8
    Width = 81
    Height = 21
    TabOrder = 1
    Text = 'Edit1'
  end
  object Memo1: TMemo
    Left = 8
    Top = 35
    Width = 81
    Height = 46
    Lines.Strings = (
      'Memo1')
    TabOrder = 2
  end
  object RichEdit1: TRichEdit
    Left = 8
    Top = 87
    Width = 81
    Height = 50
    Font.Charset = GB2312_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    Lines.Strings = (
      'RichEdit1')
    ParentFont = False
    TabOrder = 3
  end
  object Button2: TButton
    Left = 95
    Top = 56
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 4
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 95
    Top = 112
    Width = 75
    Height = 25
    Caption = 'Button3'
    TabOrder = 5
    OnClick = Button3Click
  end
end

posted on 2008-06-19 00:32  万一  阅读(4922)  评论(15编辑  收藏  举报