【问题标题】:How to intercept (detect) a Paste command into a TMemo?如何拦截(检测)粘贴命令到 TMemo 中?
【发布时间】:2012-04-15 01:26:11
【问题描述】:

在粘贴到TMemo之前,如何捕捉粘贴命令并更改剪贴板的文本,但是粘贴后,剪贴板中的文本必须与更改前相同?

例如,剪贴板中有文本'Simple Question',进入TMemo 的文本是'Симплe Qуeстиoн',之后剪贴板中的文本就像更改前一样,'Simple Question'

【问题讨论】:

    标签: delphi detect paste intercept memo


    【解决方案1】:

    派生一个从“TMemo”派生的新控件来拦截WM_PASTE消息:

    type
      TPastelessMemo = class(TMemo)
      protected
        procedure WMPaste(var Message: TWMPaste); message WM_PASTE;
      end;
    
    uses
      clipbrd;
    
    procedure TPastelessMemo.WMPaste(var Message: TWMPaste);
    var
      SaveClipboard: string;
    begin
      SaveClipboard := Clipboard.AsText;
      Clipboard.AsText := 'Simple Question';
      inherited;
      Clipboard.AsText := SaveClipboard;
    end;
    

    如果您想完全禁止任何粘贴操作,请清空 WMPaste 处理程序。

    【讨论】:

    • :) 我想你已经搞定了。 +1,并删除我的 cmets。
    【解决方案2】:

    这是 Sertac 出色答案的替代方法,即覆盖控件的 WndProc:

    // For detecting WM_PASTE messages on the control
    OriginalMemoWindowProc: TWndMethod;
    procedure NewMemoWindowProc(var Message: TMessage);
    //...
    
    // In the form's OnCreate procedure:
    // Hijack the control's WindowProc in order to detect WM_PASTE messages
    OriginalMemoWindowProc := myMemo.WindowProc;
    myMemo.WindowProc := NewMemoWindowProc;
    //...
    
    procedure TfrmMyForm.NewMemoWindowProc(var Message: TMessage);
    var
        bProcessMessage: Boolean;
    begin
        bProcessMessage := True;
        if (Message.Msg = WM_PASTE) then
            begin
            // Data pasted into the memo!
            if (SomeCondition) then
                bProcessMessage := False;   // Do not process this message any further!
            end;
    
        if (bProcessMessage) then
            begin
            // Ensure all (valid) messages are handled!
            OriginalMemoWindowProc(Message);
            end;
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 2010-09-10
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多