3

我想防止在我的TEdit. 我怎样才能做到这一点?

我尝试在按下控件时设置Key=NULLonKeyDown事件CTRL+V,但它不起作用。

4

6 回答 6

5

您需要阻止WM_CUTWM_COPYWM_PASTE消息发送到您的 TEdit。 此答案描述了如何仅使用 Windows API 来做到这一点。TEdit对于 VCL,子类化并更改其DefWndProc属性或覆盖其WndProc方法可能就足够了。

于 2009-05-08T15:10:45.830 回答
4

将此分配给TEdit.OnKeyPress

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
 if (Key=#22) or (Key=#3) then Key:=#0;   // 22 = [Ctrl+V] / 3 = [Ctrl+C]
end;
于 2014-08-11T17:14:09.327 回答
2

我知道这是一个老问题,但我会添加我发现的内容。原始海报几乎有解决方案。如果您在按键事件而不是按键事件中忽略剪切/复制/粘贴,则它可以正常工作。即(C++ 生成器)

void __fastcall Form::OnKeyPress(TObject *Sender, System::WideChar &Key)
{
   if( Key==0x03/*ctrl-c*/ || Key==0x16/*ctrl-v*/ || Key==0x018/*ctrl-x*/ )
      Key = 0;  //ignore key press
}
于 2013-01-29T18:10:33.907 回答
0

当 TEdit 窗口处于活动状态时,您可以使用一些全局程序来获取快捷方式并阻止 CV CC CX

于 2009-05-08T15:10:31.933 回答
0
Uses Clipbrd;

procedure TForm1.Edit1Enter(Sender: TObject);
begin
  Clipboard.AsText := '';
end;
于 2017-01-18T11:44:17.603 回答
0

一个老问题,但同样糟糕的答案仍在四处流传。

unit LockEdit;

// Version of TEdit with a property CBLocked that prevents copying, pasting,
// and cutting when the property is set.

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, StdCtrls, Windows;

type

  TLockEdit = class(TEdit)
  protected
      procedure WndProc(var msg: TMessage); override;
  private
      FLocked: boolean;
  public
      property CBLocked: boolean read FLocked write FLocked default false;
  end;

implementation

  procedure TLockEdit.WndProc(Var msg: TMessage);
  begin
  if ((msg.msg = WM_PASTE) or (msg.msg = WM_COPY) or (msg.msg = WM_CUT))
      and CBLocked
          then msg.msg:=WM_NULL;
  inherited;
  end;

end.                
于 2021-06-12T14:21:03.920 回答