Doug,

> Do you think, maybe, that without a parent, there is no way to recieve 
> the window messages, i.e. the mouse messages????

I was peeking in that direction too at the beginning, but then found 
that a) a TMenu doesn't have a parent; b) you cannot change/set the 
parent of a TMenuItem (and why do, since it is a valid parent and all of 
them behave well this way?)
The "problem" was lying somewhere else...

First, when I said "without any form" I meant to keep it as a function 
which doesn't need any form on which I was supposed to have dragged a 
popupmenu; not that I don't have any forms in my application.

Then, I need the dummy class only for the event handlers (and for the 
function's result...). The menu also belongs to this class.

to do the thing, I

1. create the dummy class
2. create and populate the menu
3. popu the menu up
4. set the result, free things and return...

Procedure TDummy.ClickHandler(Sender:TObject);
Begin
   FResult := (Sender As TMenuItem).Tag;
End;

Function MyFunc(WhereX, WhereY:Integer):Integer;
Var D:TDummy;
Begin
   D := TDummy.Create;
   D.FResult := -1;
   D.Menu := TPopupMenu.Create(Nil);
   D.OwnerDraw := True;
   For I := 0 To Pred(cMaxColors) Do
   Begin
     SM := TMenuItem.Create(D.Menu);
     SM.OnClick := ClickHandler;
     SM.OnAdvancedDrawItem := DrawHandler;
     SM.Tag := 1;
     SM.Caption := Format('%d', [I]);
   End;
   D.Popup(WhereX, WhereY);

   Application.ProcessMessages;

I JUST HAD TO ADD THE ABOVE, or the click message in the queue would 
have been discarded, as its handler was no longer existing at the time 
of being (later) processed (D was free'd).

   Result := D.FResult;
   FreeAndNil(D.Menu);
   FreeAndNil(D);
End;


Cheers, A.

Reply via email to