【发布时间】:2010-10-24 19:39:02
【问题描述】:
我想防止在我的TEdit 中复制、剪切和粘贴。我该怎么做?
当在控件上按下 CTRL+V 时,我尝试在 KeyDown 事件上设置 Key=NULL,但它不起作用。
【问题讨论】:
-
您尝试过 OnKeyUp 事件吗?
标签: clipboard c++builder vcl
我想防止在我的TEdit 中复制、剪切和粘贴。我该怎么做?
当在控件上按下 CTRL+V 时,我尝试在 KeyDown 事件上设置 Key=NULL,但它不起作用。
【问题讨论】:
标签: clipboard c++builder vcl
一个老问题,但仍然存在同样糟糕的答案。
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.
【讨论】:
Uses Clipbrd;
procedure TForm1.Edit1Enter(Sender: TObject);
begin
Clipboard.AsText := '';
end;
【讨论】:
将此分配给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++ builder)
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
}
【讨论】:
您需要阻止 WM_CUT、WM_COPY 和 WM_PASTE 消息发送到您的 TEdit。 This answer 描述了如何仅使用 Windows API 来做到这一点。对于 VCL,子类化 TEdit 并更改其 DefWndProc 属性或覆盖其 WndProc 方法可能就足够了。
【讨论】:
您可以使用一些全局程序来获取快捷方式并在 TEdit 窗口处于活动状态时阻止 C-V C-C C-X
【讨论】: