Solved

Panel as popup menu

Posted on 2004-04-24
12
1,699 Views
Last Modified: 2012-06-27
I want to add to my application a panel with some components on it and I want this panel have a popup menu behaviour (when a mouse button is pressed over any component except my panel its visible property will be changed to false). how can I know when the user clicks a components (and the component name) without writing the OnClick events for every component.

is possible to add components to a popup menu?
0
Comment
Question by:ronnix
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10906798
Try
procedure TForm1.Panel1Exit(Sender: TObject);
begin
Panel1.Visible := False;
end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10907197
Not sure if this is what you are looking for or not:

for this example, i added a TPopupmenu, and assigned each of the components (on the panel) PopupMenu property to this popup menu.

Then I added two Popup menu items

Hide
Show All

And their events

procedure TForm1.Hide1Click(Sender: TObject);
begin
if TPopupMenu(TMenuItem(Sender).GetParentComponent).PopupComponent is TControl then
  if TControl(TPopupMenu(TMenuItem(Sender).GetParentComponent).PopupComponent).Visible = True then
   TControl(TPopupMenu(TMenuItem(Sender).GetParentComponent).PopupComponent).Visible:= False
  else
   TControl(TPopupMenu(TMenuItem(Sender).GetParentComponent).PopupComponent).Visible:= True;
end;

procedure TForm1.ShowAll1Click(Sender: TObject);
var
 I: Integer;
begin
 for I:= 0 to Panel1.ControlCount - 1 do
  if  not Panel1.Controls[I].Visible then
   Panel1.Controls[I].Visible:= True;
end;


Shane
0
 

Author Comment

by:ronnix
ID: 10908611
Sorry, but this is not what I'm looking for. It's my fault because I didn't formulated my question corectly.
Because I don't know if it is possible to insert some components into a popupmenu (such as a listbox and some buttons) I put a panel on my form with some components, this panel "being" my popupmenu. How can I know if the user clicks any component on the screen except my panel and its components so that I can hide it. I want to do something like the list in Delphi that popups and you can select any property or method of a component or something else. It is visible while you select any item in the list but when you click any area on the screen it disappears.

Hope you understand what I mean
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10908695
Like this maybe

TForm1 = class(TForm)
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    procedure SelectItem(Sender: Tobject);
  public
    { Public declarations }
  end;



procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
 ListBox1: TListBox;
begin
 if ssRight in Shift then
 begin
   ListBox1:= TListBox.Create(Self);
   ListBox1.Parent:= Self;
   ListBox1.Left:= X;
   ListBox1.Top:= Y;
   ListBox1.Items.Add('Item1');
   ListBox1.OnClick:= SelectItem;
 end;
end;

procedure TForm1.SelectItem(Sender: Tobject);
begin
 // do something with selection
 // TListBox(Sender).Items[TListBox(Sender).ItemIndex]
  TListBox(Sender).Free;
end;

Shane
0
 
LVL 17

Expert Comment

by:mokule
ID: 10908700

So do it like this

procedure TForm1.FormClick(Sender: TObject);
begin
Panel1.Visible := False;
end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10908702
Or you could create a Borderless form with a Listbox on it,

then do this

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 if ssRight in Shift then
 begin
  if MyForm.ShowModal = mrOK then
  begin
   // do something with selection
   //MyListBox.Items[MyListBox.ItemIndex]
  end;
 end;
end;


Shane
0
 

Author Comment

by:ronnix
ID: 10912779
:)
I see I was not competely understood.
You say I must write the OnMouseDown event for the form, but what if I click another object on the form? It will not call the main form onmousedown event. And then I must write the onmousedown code for every component on the form, so that when I click it my panel disappear (like a popup menu). So I repeat my question. Is it posible to know when a user clicks a component without writing the onclick event for every object on my form( because all surface of my form is covered with components and I cannot write the onmousedown event for my form bacause it would be vainly?

So, when I press a certain button my panel visible property will become true and when I click everywhere on the desktop except the area ocuppied by my panel its visible property will be changed to false.
0
 
LVL 17

Expert Comment

by:mokule
ID: 10912850

Did You try code from my the very first post?
0
 
LVL 17

Accepted Solution

by:
mokule earned 50 total points
ID: 10912918
I think that this code fulfill all Your requirements.

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnDeactivate := FormDeactivate;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Panel1.Visible := True;
  ActiveControl := Panel1;
end;

procedure TForm1.FormDeactivate(Sender: TObject);
begin
  Panel1.Visible := False;
end;

procedure TForm1.Panel1Exit(Sender: TObject);
begin
  Panel1.Visible := False;
end;

procedure TForm1.FormClick(Sender: TObject);
begin
  Panel1.Visible := False;
end;

0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10915790
yes there is a way .. no 2 ways :p


Prepare a windowProc for ur application Handle only WM_MouseButton event and do wot ever u want...

Or

Add a Procedure like

  private
   procedure onMyMouseDown(msg : Tmsg) ; message WM_MBUTTONDOWN;

in ur form object

  procedure tForm1.onMyMouseDown(msg : Tmsg) ;
   begin
     // do wot u want

   end;

get Details about  WM_Mbuttondown from the link below

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputmessages/wm_mbuttondown.asp


0
 

Author Comment

by:ronnix
ID: 10920178
for non zero
In your code I must declare the procedure like this

procedure OnMouseDown(VAR Msg:Tmsg); message WM_MBUTTONDOWN;
else I get this error: "Invalid message parameter list"

And for this code my application does nothing.
I replaced WM_MButtonDown with WM_LButtonDown and The onmousedown proc is called only when I click the form not when I click other object on it.
I think mokule's answer is the best(just my oppinion), but thanks to all for your answers
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10920561
yw
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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