» Help

How to add an OnMouseDown event to a TPopupMenu

   
Question
I need to add a right-click capability to a popup menu. I created a component that inherits from TPopupMenu.

I suggest that you write the OnMouseDown event similar to the following:

{ ... }
  TNewPopupMenu = class(TPopupMenu)
  private
    FOnMouseDown: TMouseEvent;
  protected
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
      X, Y: Integer); dynamic;
    procedure DoMouseDown(var Message: TWMMouse;
      Button: TMouseButton; Shift: TShiftState);
  published
    property OnMouseDown: TMouseEvent
      read FOnMouseDown write FOnMouseDown;
  end;

procedure TNewPopupMenu.MouseDown(Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Assigned(FOnMouseDown) then
    FOnMouseDown(Self, Button, Shift, X, Y);
end;

procedure TNewPopupMenu.DoMouseDown(var Message: TWMMouse;
  Button: TMouseButton; Shift: TShiftState);
begin
  with Message do
    MouseDown(Button, KeysToShiftState(Keys) + Shift, XPos, YPos);
end;
Original resource: The Delphi Pool
Author: Kalman Hadarics
Added: 2009-10-26
Last updated: 2009-10-26

« Return to contents »