如何检测LMB,RMB点击TMainMenu?

时间:2013-12-12 09:50:22

标签: delphi

我想在MainMenu1组件上检测鼠标单击(鼠标右键和鼠标左键)。 怎么做?

我目前的代码:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    Zakadki11: TMenuItem;
    Gry1: TMenuItem;
    Forum1: TMenuItem;
    Button1: TButton;
    PopupMenu1: TPopupMenu;
    Dodajnowyadres1: TMenuItem;
    N1: TMenuItem;
    Usu1: TMenuItem;
    Edytuj1: TMenuItem;
    Info1: TMenuItem;
    Warez1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    Otwrzwszystkiestrony1: TMenuItem;
    Zmienazwkategorii1: TMenuItem;
    procedure Info1Click(Sender: TObject);
    procedure Dodajnowyadres1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure PopupMenuItemsClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Dodajnowyadres1Click(Sender: TObject); {Add New Item to MainMenu1}
var
  NewItem: TMenuItem;
  nr: Integer;
begin
  Randomize;
  nr := Random(1000);

  {add new item}
  NewItem := TMenuItem.Create(Self);
  NewItem.Caption := 'Item ' + IntToStr(nr);
  NewItem.OnClick := PopupMenuItemsClick; //event click to show PopupMenu1
  MainMenu1.Items[TMenuItem(Pointer(PopupMenu1.Tag)).Parent.MenuIndex].Add(NewItem);
end;

procedure TForm1.Info1Click(Sender: TObject); {Show Info about clicked item}
begin
  Form1.Caption := TMenuItem(Pointer(PopupMenu1.Tag)).Caption;
end;

procedure TForm1.PopupMenuItemsClick(Sender: TObject); {Show PopupMenu1 - Event}
begin
    //maybe here i will add detection code click the mouse button (LMB, RMB dectection)
  PopupMenu1.Tag := Integer(Pointer(Sender));
  PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y); //show popupmenu1
end;

end.

0 个答案:

没有答案