ronnix
asked on
Panel as popup menu
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?
is possible to add components to a popup menu?
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(Sende r).GetPare ntComponen t).PopupCo mponent is TControl then
if TControl(TPopupMenu(TMenuI tem(Sender ).GetParen tComponent ).PopupCom ponent).Vi sible = True then
TControl(TPopupMenu(TMenuI tem(Sender ).GetParen tComponent ).PopupCom ponent).Vi sible:= False
else
TControl(TPopupMenu(TMenuI tem(Sender ).GetParen tComponent ).PopupCom ponent).Vi sible:= True;
end;
procedure TForm1.ShowAll1Click(Sende r: 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
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(Sende
if TControl(TPopupMenu(TMenuI
TControl(TPopupMenu(TMenuI
else
TControl(TPopupMenu(TMenuI
end;
procedure TForm1.ShowAll1Click(Sende
var
I: Integer;
begin
for I:= 0 to Panel1.ControlCount - 1 do
if not Panel1.Controls[I].Visible
Panel1.Controls[I].Visible
end;
Shane
ASKER
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
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
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(Sende r: 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[TLi stBox(Send er).ItemIn dex]
TListBox(Sender).Free;
end;
Shane
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(Sende
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[TLi
TListBox(Sender).Free;
end;
Shane
So do it like this
procedure TForm1.FormClick(Sender: TObject);
begin
Panel1.Visible := False;
end;
Or you could create a Borderless form with a Listbox on it,
then do this
procedure TForm1.FormMouseDown(Sende r: 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[MyListBo x.ItemInde x]
end;
end;
end;
Shane
then do this
procedure TForm1.FormMouseDown(Sende
Shift: TShiftState; X, Y: Integer);
begin
if ssRight in Shift then
begin
if MyForm.ShowModal = mrOK then
begin
// do something with selection
//MyListBox.Items[MyListBo
end;
end;
end;
Shane
ASKER
:)
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.
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.
Did You try code from my the very first post?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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
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
yw
procedure TForm1.Panel1Exit(Sender: TObject);
begin
Panel1.Visible := False;
end;