Solved

right click on a popup menu ?

Posted on 2002-06-05
7
581 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: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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to center only a line in richedit? 4 52
Intraweb download file link ? 1 108
Find and Replace Stream with 0s 8 56
Base1 Encode/Decode 3 67
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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