将多行粘贴到TEdit中

时间:2013-06-21 03:13:44

标签: delphi-xe2 delphi-7 delphi-2010 delphi

对于TEdit组件,组件是否可以通过将换行符转换为空格来处理来自Windows剪贴板的多行粘贴?

换句话说,如果Windows剪贴板上有以下数据:

Hello
world
!

...并且用户将光标放在TEdit中然后按下CTRL + V,是否可以让TEdit显示输入为:

Hello world!

1 个答案:

答案 0 :(得分:12)

您需要使用插入器类对TEdit进行子类化,并为WM_PASTE消息添加处理程序:

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, adsdata, adsfunc, adstable;

type
  TEdit= class(StdCtrls.TEdit)
    procedure WMPaste(var Msg: TWMPaste); message WM_PASTE;
  end;

type
  TForm3 = class(TForm)
    AdsTable1: TAdsTable;
    Edit1: TEdit;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

uses
  Clipbrd;

{ TEdit }

procedure TEdit.WMPaste(var Msg: TWMPaste);
var
  TempTxt: string;
begin
  TempTxt := Clipboard.AsText;
  TempTxt := StringReplace(TempTxt, #13#10, #32, [rfReplaceAll]);
  Text := TempTxt;
end;

end.