🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.delphi.vcl.components.using.general
3 messages
3 total messages Started by diktyo Fri, 18 Jul 2008 17:31
OnContextPopUp
#168
Author: diktyo
Date: Fri, 18 Jul 2008 17:31
25 lines
1533 bytes
I have 2 labels, label1 and label2. I also have a PopupMenu1 with one
menuitem (for the time being) N1 with the caption RefreshData.
I want to right click on either label1 or label2 , popup the menu and
click on RefreshData, then do whatever I need depending on which label
I right-clicked.
I have a handler for label1ContextPopUp (same for label2) , set Handled
to True and popup the menu with PopupMenu1.Popup(MousePos.X,
MousePos.Y).

2 problems :

1) small. The coordinates MousePos.X, MousePos.Y must be converted to
something else (relative to cursor position ?) . I think I will manage
to do it (someday !).

2) bigger. From inside the popupmenu handler I can find through
PopupMenu1.PopUpComponent which label I am on, but not from the
menuitem N1 click handler (which does the job or calls a RefreshData
procedure).
How am I to do this ?

Thanks,

Petros
Re: OnContextPopUp
#170
Author: Remy Lebeau (TeamB)
Date: Fri, 18 Jul 2008 19:00
83 lines
3667 bytes
"diktyo" <diktyo@axd.forthnet.gr> wrote in message 
news:4880d372$1@newsgroups.borland.com...

> I want to right click on either label1 or label2 , popup the menu
> and click on RefreshData, then do whatever I need depending
> on which label I right-clicked.

Use the TPopupMenu.PopupComponent property to know which TLabel was clicked 
on.

> I have a handler for label1ContextPopUp (same for label2) ,
> set Handled to True and popup the menu with
> PopupMenu1.Popup(MousePos.X, MousePos.Y).

Why not just let the TLabel components display the PopupMenu automatically 
for you?

> The coordinates MousePos.X, MousePos.Y must be converted
> to something else (relative to cursor position ?) .

The MousePos parameter is expressed in client coordinates relative to the 
calling TLabel's top-left corner, but Popup() expects screen coordinates 
instead.  Use the calling TLabel's ClientToScreen() method for the 
conversion, ie:

    procedure TForm1.LabelContextPopup(Sender: TObject; const MousePos: 
TPoint; var Handled: Boolean);
    var
        P: TPoint;
    begin
        P := (Sender as TLabel).ClientToScreen(MousePos);
        PopupMenu1.Popup(P.X, P.Y);
        Handled := True;
    end;

> From inside the popupmenu handler I can find through
> PopupMenu1.PopUpComponent which label I am on

The TPopupMenu.PopupComponent property is not filled in if you use the 
OnContextPopup event to call Popup() manually.  You have to let the TLabel 
components display the TPopup for you in order for the PopupComponent 
property to be meaningful.

> but not from the menuitem N1 click handler (which does the job
> or calls a RefreshData procedure).

Had you let the TLabel components display the TPopupMenu automatically, the 
PopupComponent property should retain its value while the selected TMenuItem 
is executing.  However, since you are calling Popup() manually, you will 
have to use an alternative approach.  I suggest using the TPopupMenu's Tag 
property for that, ie:

    procedure TForm1.LabelContextPopup(Sender: TObject; const MousePos: 
TPoint; var Handled: Boolean);
    var
        Label: TLabel;
        P: TPoint;
    begin
        Label := Sender as TLabel;
        P := Label.ClientToScreen(MousePos);
        PopupMenu1.Tag := Integer(Label);
        try
            PopupMenu1.Popup(P.X, P.Y);
        finally
            PopupMenu1.Tag := 0;
        end;
        Handled := True;
    end;

    procedure TForm1.N1Click(Sender: TObject);
    var
        Label: TLabel;
    begin
        Label := TLabel(PopupMenu1.Tag);
        if Label <> nil then
        begin
            // use Label as needed...
        end;
    end;


Gambit 
Re: OnContextPopUp
#172
Author: diktyo
Date: Sat, 19 Jul 2008 06:13
14 lines
949 bytes
Remy Lebeau (TeamB) wrote:

>
> 
> Gambit

-- 

Thanks Remy,

It works either way. GREAT !


Petros
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads