问题描述
我需要拦截 TEdit
组件的 WM_PASTE
消息,该组件位于 TFrame
的后代类中.
I need to intercept the WM_PASTE
message for a TEdit
component which is placed inside a TFrame
's descendant class.
如果不满足条件,我想执行粘贴操作.
If a condition is not satisfied, I want to iniby the paste operation.
有没有一种方法可以在帧级别执行此操作?(我的意思是,没有声明 TEdit
的后代)
Is there a way to do this at the frame level? (I mean, without declaring a TEdit
's descendant)
推荐答案
WM_PASTE
直接发送到 TEdit
窗口,而 TFrame
却看不到它,因此您必须将 TEdit 子类化.代码>直接以拦截消息.您可以:
WM_PASTE
is sent directly to the TEdit
window, the TFrame
never sees it, so you must subclass the TEdit
directly in order to intercept the message. You can either:
-
已将
TFrame
分配给TEdit
的WindowProc
属性一个处理程序.如果只有几个TEdit
可以子类化,那么这是一种简单的方法,但是如果要子类化的TEdit
越多,它就会变得越复杂:
have the
TFrame
assign a handler to theTEdit
'sWindowProc
property. This is a simple approach if you have only a fewTEdit
s to subclass, but it gets more complicated the moreTEdit
s you want to subclass:
type
TMyFrame = class(TFrame)
Edit1: TEdit;
...
procedure FrameCreate(Sender: TObject);
...
private
PrevWndProc: TWndMethod;
procedure EditWndProc(var Message: TMessage);
...
end;
procedure TMyFrame.FrameCreate(Sender: TObject);
begin
PrevWndProc := Edit1.WindowProc;
Edit1.WindowProc := EditWndProc;
...
end;
procedure TMyFrame.EditWndProc(var Message: TMessage);
begin
if Message.Msg = WM_PASTE then
begin
if SomeCondition then
Exit;
end;
PrevWndProc(Message);
end;
编写并安装从 TEdit
派生的新组件,类似于 TMemo 示例.
write and install a new component that is derived from TEdit
, similar to the TMemo
example you presented.
在 TFrame
类声明上方定义一个仅位于 TFrame
单元本地的插入器类,它将拦截 WM_PASTE 框上的每个
TEdit
的code>:
define an interposer class that is local to just the TFrame
's unit, above the TFrame
class declaration, which will intercept WM_PASTE
for every TEdit
on the Frame:
type
TEdit = class(Vcl.StdCtrls.TEdit)
procedure WMPaste(var Message: TMessage); message WM_PASTE;
end;
TMyFrame = class(TFrame)
Edit1: TEdit;
Edit2: TEdit;
...
end;
procedure TEdit.WMPaste(var Message: TMessage);
begin
if not SomeCondition then
inherited;
end;
这篇关于如何拦截和抑制TFrame子组件的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!