Solved

right click on a popup menu ?

Posted on 2002-06-05
7
601 Views
Last Modified: 2010-08-05
Hello all,

Is there a way to let a Popup menu react to a user-right-click ? i.e. an equivalent of OnMouseDown event .  Also I need to know on which menuitem user was positioned when he right-clicked

many thanks
0
Comment
Question by:fadyg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 7057011
add popup menu to ur form, then assign the Popmenu property of the from to ur popup menu
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 7057064
also you can view it manually in the OneMouseDown Event

  if Button = mbRight then
    PopupMenu1.Popup(X + Top,Y + Left);

0
 
LVL 1

Expert Comment

by:bes67
ID: 7057116
Hi

if you mean right-click in a popup menu that has been already activated I think that you have to look at TrackPopupMenu API function. TrackPopupMenu API function (that is used to show popup menu  in respond of user right-click) has TPM_RECURSE flag that is not used in Delphi. This flag allows to use context menu within a menu but only in Win98/me and win2000/xp. To use this flag you have to make your own procedure to invoke popup menu

Hope this will help
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:bes67
ID: 7057138
Small addition to my previous post:

you have to write WM_MENURBUTTONUP message handler to use nested popup menus
0
 

Author Comment

by:fadyg
ID: 7057282
Hello again guys,

sorry for not being very accurate in my description of the problem. I have already a popupmenu assigned to a control and this popup works very nicely when I right click on the control.  What is needed now is to be able to respond to right-clicks WHILE the popup is open and showing its items.  

for example, one of the item is "modem". When the mouse comes over it on the popup, it is highlighted. A normal click would activate the modem, while a right click would open the properties of the modem (of course this is just an example).

bes67 got it almost correct, but I don't need a menu when righ-cliking but rather call an event.

is it a bit clearer ?


0
 
LVL 1

Accepted Solution

by:
bes67 earned 300 total points
ID: 7058534
Hi

try this unit. To define right-click menu handle use OnMenuRightClick event

unit RCPopupMenu;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus;

type
  TMenuRightClickEvent = procedure (Sender: TObject; Item: TMenuItem) of object;

  TRCPopupList = class(TPopupList)
  protected
    procedure WndProc(var Message: TMessage); override;
  end;

  TRCPopupMenu = class(TPopupMenu)
  private
    { Private declarations }
    FOnMenuRightClick: TMenuRightClickEvent;
  protected
    { Protected declarations }
    function DispatchRC(aHandle: HMENU; aPosition: Integer): Boolean;
    procedure RClick(aItem: TMenuItem);
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Popup(X, Y: Integer); override;
  published
    { Published declarations }
    property OnMenuRightClick: TMenuRightClickEvent read FOnMenuRightClick
                                                   write FOnMenuRightClick;
  end;

procedure Register;

var RCPopupList: TRCPopupList;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TRCPopupMenu]);
end;

{ TRCPopupList }

procedure TRCPopupList.WndProc(var Message: TMessage);
var i: Integer;
    pm: TPopupMenu;
begin
  case Message.Msg of
    WM_MENURBUTTONUP:
      for I := 0 to Count - 1 do
      begin
        pm := TPopupMenu(Items[i]);
        if pm is TRCPopupMenu then
          if TRCPopupMenu(Items[i]).DispatchRC(Message.lParam, Message.wParam) then
            Exit;
      end;
  end;
  inherited WndProc(Message);
end;

{ TRCPopupMenu }

constructor TRCPopupMenu.Create(AOwner: TComponent);
begin
  inherited;
  PopupList.Remove(Self);
  RCPopupList.Add(Self);
end;

destructor TRCPopupMenu.Destroy;
begin
  RCPopupList.Remove(Self);
  PopupList.Add(Self);
  inherited;
end;

function TRCPopupMenu.DispatchRC(aHandle: HMENU;
  aPosition: Integer): Boolean;
begin
  Result := False;
  if Handle = aHandle then
  begin
    RClick(Items[aPosition]);
    Result := True;
  end;
end;

procedure TRCPopupMenu.Popup(X, Y: Integer);
const
  Flags: array[Boolean, TPopupAlignment] of Word =
    ((TPM_LEFTALIGN, TPM_RIGHTALIGN, TPM_CENTERALIGN),
     (TPM_RIGHTALIGN, TPM_LEFTALIGN, TPM_CENTERALIGN));
  Buttons: array[TTrackButton] of Word = (TPM_RIGHTBUTTON, TPM_LEFTBUTTON);
var
  AFlags: Integer;
begin
  DoPopup(Self);
  AFlags := Flags[UseRightToLeftAlignment, Alignment] {or Buttons[TrackButton]};
  if (Win32MajorVersion > 4) or
    ((Win32MajorVersion = 4) and (Win32MinorVersion > 0)) then
  begin
    AFlags := AFlags or (Byte(MenuAnimation) shl 10);
    AFlags := AFlags or TPM_RECURSE;
  end;
  TrackPopupMenuEx(Items.Handle, AFlags, X, Y, RCPopupList.Window, nil);
end;

procedure TRCPopupMenu.RClick(aItem: TMenuItem);
begin
  if Assigned (FOnMenuRightClick) then
    FOnMenuRightClick(Self, aItem);
end;

var oldPL: TPopupList;

initialization
    RCPopupList := TRCPopupList.Create;
finalization
  RCPopupList.Free;
end.

hope this will help
0
 

Author Comment

by:fadyg
ID: 7059319
Nice one !!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question