INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Component Writing

How do I intercept the OnPaste event in a TMemo by Nordlund
Posted: 7 Apr 06

First, read the FAQ describing how to create a simple Delphi component. Then, you are ready to procced with this component.

CODE

unit MemoEx;

interface

uses
  ClipBrd, Windows, Messages, SysUtils, Classes, Controls, StdCtrls;

type
  TStringEvent = procedure(Sender: TObject; var s: String) of object;
  TMemoEx = class(TMemo)
  private
    FOnBeforePaste: TStringEvent;
    FOnAfterPaste: TNotifyEvent;
    procedure WMPaste(var Message: TMessage); message WM_PASTE;
  protected
  public
  published
    property OnBeforePaste: TStringEvent read FOnBeforePaste write FOnBeforePaste;
    property OnAfterPaste: TNotifyEvent read FOnAfterPaste write FOnAfterPaste;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Andreas Nordlund', [TMemoEx]);
end;

{ TMemoEx }

procedure TMemoEx.WMPaste(var Message: TMessage);
var
  strClpText: String;
begin
  // Check if the OnBeforePaste event is assigned and check if the clipboard contains plain text.
  if Assigned(FOnBeforePaste) and Clipboard.HasFormat(CF_TEXT) then
  begin
    // Save the clipboard text into a local variable
    strClpText := ClipBoard.AsText;
    // Fire the OnBeforePaste event
    FOnBeforePaste(self, strClpText);

    // Clear the clipboard and replace it with the new text
    Clipboard.Clear;
    Clipboard.AsText := strClpText;
  end;

  // Perform the actual paste
  inherited;
  
  // if the OnAfterPaste event is assigned, fire it.
  if Assigned(FOnAfterPaste) then
    FOnAfterPaste(self);
end;

end.

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close