Solved

right click on a popup menu ?

Posted on 2002-06-05
7
576 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Expert Comment

by:mnasman
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:mnasman
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now