1 (edited by thezimguy 2018-08-13 19:03:05)

Topic: [SOLVED] PopupMenu on Button Left Click

Hello Dmitry,
I'm having a little challenge in making the popupMenu on a button appear on just a 'Left Click'.
The popupMenu only appears on 'Right Click'

How do I make this work on just a 'Left Click' on the button


procedure Form1_OnShow (Sender: string; Action: string);
var
  PopupMenu: TPopupMenu;
  MyItem: TMenuItem;
begin
  PopupMenu := TPopupMenu.Create(Form1);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Add';
  MyItem.OnClick := @popupMenuOnClick;
  PopupMenu.Items.Add(MyItem);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Edit';
  MyItem.OnClick := @popupMenuOnClick;
  PopupMenu.Items.Add(MyItem);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Delete';
  MyItem.OnClick := @popupMenuOnClick;
  PopupMenu.Items.Add(MyItem);

  Form1.btnPopup.PopupMenu := PopupMenu;
end;

procedure popupMenuOnClick (Sender: string);
begin
  //
end;

begin
end.

Find the example project attached
Thank you.

Post's attachments

Attachment icon popupMenu.zip 333.11 kb, 525 downloads since 2018-08-11 

@thezimguy

Re: [SOLVED] PopupMenu on Button Left Click

Hello thezimguy

By default, the right mouse button calls an action when the left button performs an action.

So, to reverse the roles, Delphi needs to know the X and Y positions of the cursor:

This example could help you

procedure TForm1.Button1Click(Sender: TObject);
begin
  PopUpMenu.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;

Can this help you ?

JB

3 (edited by thezimguy 2018-08-11 17:34:48)

Re: [SOLVED] PopupMenu on Button Left Click

Thanks jean.brezhonek
I tried using the above code earlier today before my post but did not work.
Error: Undeclared identity: 'Mouse'

@thezimguy

Re: [SOLVED] PopupMenu on Button Left Click

Hello the zimguy

And If you add this code

TForm1.Button1Click(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); ?

JB

Re: [SOLVED] PopupMenu on Button Left Click

I'm having the same problem.
I'm doing this in MVD and not Delphi.
Thank you

@thezimguy

Re: [SOLVED] PopupMenu on Button Left Click

Hello thezimguy

I quoted Delphi because MVD was written in this language (Embarcadero RAD XE3).

I can solve some MVD problems by going to the Embarcadero website.

JB

7 (edited by thezimguy 2018-08-13 16:38:06)

Re: [SOLVED] PopupMenu on Button Left Click

jean.brezhonek wrote:

Hello thezimguy

I quoted Delphi because MVD was written in this language (Embarcadero RAD XE3).

I can solve some MVD problems by going to the Embarcadero website.

JB


Thank you so much.
I'm very much grateful for your help

@thezimguy

Re: [SOLVED] PopupMenu on Button Left Click

Hello.


Please download latest beta version
https://www.dropbox.com/s/wutj7mnux7f3a … a.zip?dl=0


I have added new procedure GetCursorPos, example for you

var
  PopupMenu: TPopupMenu; // global variable

procedure Form1_btnPopup_OnMouseUp (Sender: TObject; MouseLeft, MouseRight, MouseMiddle: boolean; Shift, Alt, Ctrl: boolean; X, Y: Integer);
var
    mX, mY: integer;
begin
    if MouseLeft then
    begin
        GetCursorPos(mX, mY);
        PopupMenu.Popup(mX, mY);
    end;

end;

procedure Form1_OnShow (Sender: string; Action: string);
var
  MyItem: TMenuItem;
begin
  PopupMenu := TPopupMenu.Create(Form1);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Add';
  MyItem.OnClick := @popupMenuOnClick;
  PopupMenu.Items.Add(MyItem);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Edit';
  MyItem.OnClick := @popupMenuOnClick;
  PopupMenu.Items.Add(MyItem);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Delete';
  MyItem.OnClick := @popupMenuOnClick;
  PopupMenu.Items.Add(MyItem);
end;
Dmitry.

Re: [SOLVED] PopupMenu on Button Left Click

DriveSoft wrote:

Hello.


Please download latest beta version
https://www.dropbox.com/s/wutj7mnux7f3a … a.zip?dl=0


I have added new procedure GetCursorPos, example for you

var
  PopupMenu: TPopupMenu; // global variable

procedure Form1_btnPopup_OnMouseUp (Sender: TObject; MouseLeft, MouseRight, MouseMiddle: boolean; Shift, Alt, Ctrl: boolean; X, Y: Integer);
var
    mX, mY: integer;
begin
    if MouseLeft then
    begin
        GetCursorPos(mX, mY);
        PopupMenu.Popup(mX, mY);
    end;

end;

procedure Form1_OnShow (Sender: string; Action: string);
var
  MyItem: TMenuItem;
begin
  PopupMenu := TPopupMenu.Create(Form1);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Add';
  MyItem.OnClick := @popupMenuOnClick;
  PopupMenu.Items.Add(MyItem);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Edit';
  MyItem.OnClick := @popupMenuOnClick;
  PopupMenu.Items.Add(MyItem);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Delete';
  MyItem.OnClick := @popupMenuOnClick;
  PopupMenu.Items.Add(MyItem);
end;

This is PERFECT. Thank you Dmitry
You are always at our aid.
MVD, the best

@thezimguy

Re: [SOLVED] PopupMenu on Button Left Click

Hi Dmitry,
Please the GetCursorPos is still not working.
I have downloaded the version 4.6 beta but it still did not work.
Any help?
Thank you.

@thezimguy

Re: [SOLVED] PopupMenu on Button Left Click

Hello thezimguy, Dmitri

Did you get an error  message 'Event handler must be a procedure' ?

If yes, that's mean you have not implemented instruction for your menu (procedure Form1.OnShow.)

It fit to this instruction in your code : MyItem.OnClick := @popupMenuOnClick;

Add this code at the end of your script (before BEGIN ... END.).

procedure PopUpMenuOnClick (Sender: string);
begin
  openurl('http://www.google.co.uk');
end;

So, when you will click on item Add of your menu, a Google page will open.

As it states, for every click on an item of your menu, you will always open a Google Page.

You have to modify the event for each item o trigger a different action.

Hope this can helps you.

JB

Re: [SOLVED] PopupMenu on Button Left Click

Hello jean.brezhonek,
Thank you for you response.
I did all what you have specified above but the error message I get is

Undeclared identifier:'GetCursorPos', (meaning GetCursorPos procedure does not exist).

Thank you.

@thezimguy

Re: [SOLVED] PopupMenu on Button Left Click

Hello theguimzy

Are you sure to get the very last version of MVD ?

I use MVD 6.beta downloaded on the 13/08/2018

[info]
version=4
minorversion=2

from the link below :

https://www.dropbox.com/s/wutj7mnux7f3a … a.zip?dl=0

and all is ok for me.

And Ii modified Dmitry's code as below to get three differents answers for the popup menu :

var PopupMenu: TPopupMenu; // global variable



procedure Form1_OnShow (Sender: TObject; Action: string);
var  MyItem: TMenuItem;
begin
  PopupMenu := TPopupMenu.Create(Form1);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Add';
  MyItem.OnClick := @popupMenuOnClick1;
  PopupMenu.Items.Add(MyItem);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Edit';
  MyItem.OnClick := @popupMenuOnClick2;
  PopupMenu.Items.Add(MyItem);

  MyItem := TMenuItem.Create (PopupMenu);
  MyItem.Caption := 'Delete';
  MyItem.OnClick := @popupMenuOnClick;
  PopupMenu.Items.Add(MyItem);
end;

procedure Form1_Button1_OnMouseUp (Sender: TObject; MouseLeft, MouseRight, MouseMiddle: boolean; Shift, Alt, Ctrl: boolean; X, Y: Integer);
var     mX, mY: integer;
begin
    if MouseLeft then
    begin
        GetCursorPos(mX, mY);
        PopupMenu.Popup(mX, mY);
    end;
end;

procedure PopUpMenuOnClick (Sender: string);
begin
  openurl('http://www.google.co.uk');
end;


procedure PopUpMenuOnClick1 (Sender: string);
begin
  ShowMessage('Procedure Ajouter');
end;

procedure PopUpMenuOnClick2 (Sender: string);
begin
  ShowMessage('Procedure Editer');
end;


begin

end.

JB

14 (edited by thezimguy 2018-08-16 09:07:21)

Re: [SOLVED] PopupMenu on Button Left Click

Ohk
Then I'm redownloading it.
Thank you.

It's working after doing the download.
Thank you so much

@thezimguy