我想防止在我的TEdit
. 我怎样才能做到这一点?
我尝试在按下控件时设置Key=NULL
onKeyDown
事件CTRL+V,但它不起作用。
您需要阻止WM_CUT
、WM_COPY
和WM_PASTE
消息发送到您的 TEdit。 此答案描述了如何仅使用 Windows API 来做到这一点。TEdit
对于 VCL,子类化并更改其DefWndProc
属性或覆盖其WndProc
方法可能就足够了。
将此分配给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;
我知道这是一个老问题,但我会添加我发现的内容。原始海报几乎有解决方案。如果您在按键事件而不是按键事件中忽略剪切/复制/粘贴,则它可以正常工作。即(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
}
当 TEdit 窗口处于活动状态时,您可以使用一些全局程序来获取快捷方式并阻止 CV CC CX
Uses Clipbrd;
procedure TForm1.Edit1Enter(Sender: TObject);
begin
Clipboard.AsText := '';
end;
一个老问题,但同样糟糕的答案仍在四处流传。
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.