本文介绍了如何拦截和抑制TFrame子组件的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要拦截 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 the TEdit's WindowProc property. This is a simple approach if you have only a few TEdits to subclass, but it gets more complicated the more TEdits 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子组件的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    05-19 04:48