加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何从剪贴板粘贴到编辑框中的文本超过最大长度时获取

发布时间:2020-12-15 04:21:36 所属栏目:大数据 来源:网络整理
导读:当从剪贴板粘贴到TEdit控件中的文本超过其允许的最大长度时,我需要向用户显示一条消息.但是我不希望每次输入新字母时都要检查程序,只有在粘贴文本时才会检查. 我该怎么做? 解决方法 将TEdit子类化以处理 EN_MAXTEXT 通知: Sent when the current text inse
当从剪贴板粘贴到TEdit控件中的文本超过其允许的最大长度时,我需要向用户显示一条消息.但是我不希望每次输入新字母时都要检查程序,只有在粘贴文本时才会检查.

我该怎么做?

解决方法

将TEdit子类化以处理 EN_MAXTEXT通知:

Sent when the current text insertion has exceeded the specified number of characters for the edit control. The text insertion has been truncated.

这适用于键入和粘贴,并为您考虑文本选择.例如:

unit Unit1;

interface

uses
  Winapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls;

type
  TEdit = class(Vcl.StdCtrls.TEdit)
  private
    procedure CNCommand(var Message: TWMCommand); message CN_COMMAND;
  end;

  TForm1 = class(TForm)
    Edit1: TEdit;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TEdit.CNCommand(var Message: TWMCommand);
begin
  inherited;
  if Message.NotifyCode = EN_MAXTEXT then
    ShowMessage('Too much text!');
end;

end.

如果您只想处理粘贴,则在调用inherited之前捕获WM_PASTE以设置标志,然后在继承退出时清除该标志,并且如果在设置该标志时发出EN_MAXTEXT,则相应地执行:

unit Unit1;

interface

uses
  Winapi.Windows,Vcl.StdCtrls;

type
  TEdit = class(Vcl.StdCtrls.TEdit)
  private
    FIsPasting: Boolean;
    procedure CNCommand(var Message: TWMCommand); message CN_COMMAND;
    procedure WMPaste(var Message: TMessage); message WM_PASTE;
  end;

  TForm1 = class(TForm)
    Edit1: TEdit;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TEdit.CNCommand(var Message: TWMCommand);
begin
  inherited;
  if (Message.NotifyCode = EN_MAXTEXT) and FIsPasting then
    ShowMessage('Too much text!');
end;

procedure TEdit.WMPaste(var Message: TMessage);
begin
  FIsPasting := True;
  try 
    inherited;
  finally
    FIsPasting := False;
  end;
end;

end.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读