unit WinForm;

interface

uses
  System.Drawing, System.Collections, System.ComponentModel,
  System.Windows.Forms, System.Data;

type
  TWinForm = class(System.Windows.Forms.Form)
  strict private
    procedure InitializeComponent;
  {$REGION 'Designer Managed Code'}
  public
    /// 
    /// Required designer variable.
    /// 
    Components: System.ComponentModel.Container;
    Label1: System.Windows.Forms.Label;
  {$ENDREGION}
  strict protected
    /// 
    /// Clean up any resources being used.
    /// 
    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;

// This is a slightly modified MS sample code from .NET help
  // (nice job MS for using hardcoded constants!)
  //
  // Create message filter
type  TestMessageFilter = class (System.Object, IMessageFilter)
    private FOwner: TWinForm;
    const WM_KEYDOWN = $100;
    const WM_KEYUP = $101;
    const WM_LEFTMOUSEDOWN = $201;
    const WM_LEFTMOUSEUP = $202;
    const WM_LEFTMOUSEDBL = $203;    public constructor Create(aOwner:TWinForm);

    public function PreFilterMessage(var m: Message): Boolean;
  end;

  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]

implementation
{$ENDREGION}

procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;

constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  Application.AddMessageFilter(TestMessageFilter.Create(self));
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;

procedure TWinForm.InitializeComponent;
begin
  Self.Label1 := System.Windows.Forms.Label.Create;
  Self.SuspendLayout;
  // 
  // Label1
  // 
  Self.Label1.Location := System.Drawing.Point.Create(48, 40);
  Self.Label1.Name := 'Label1';
  Self.Label1.Size := System.Drawing.Size.Create(224, 64);
  Self.Label1.TabIndex := 0;
  Self.Label1.Text := 'Label1';
  // 
  // TWinForm
  // 
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 273);
  Self.Controls.Add(Self.Label1);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Self.ResumeLayout(False);
end;

constructor TestMessageFilter.Create(aOwner:TWinForm);
begin
  inherited Create();
  FOwner := aOwner;
end;

function TestMessageFilter.PreFilterMessage(var m: Message): Boolean;
var
  KeyCode: Keys;
begin
      // Blocks all the messages relating to the left mouse button.
      KeyCode := Keys(m.WParam.ToInt32()) and Keys.KeyCode;

      if (m.Msg = WM_KEYDOWN) and
         ((KeyCode = Keys.Left) or (KeyCode = Keys.Right))  then
      begin
        FOwner.label1.Text := 'Processing the messages : ' +
          Convert.ToString(m.Msg) + ' ' +
          Enum(KeyCode).ToString('G');
        result := true;
        exit;
      end;
      result := false;
end;

end.