I have a TPopupMenu associated with a TTreeView, but i now want to invoke (popup) the menu only when the user click in a particular node. So how i can popup a context menu in a treeview but just in some particular treenodes?
Use the Handled
parameter from the OnContextPopup
event. By setting this parameter to True you will suppress the context menu to display. The following code shows how to get the TTreeNode
from the cursor position passed into the OnContextPopup
event and it displays the popup menu only when you right click over the TTreeNode
different from the top one.
procedure TForm1.TreeView1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
begin
if TreeView1.GetNodeAt(MousePos.X, MousePos.Y) = TreeView1.TopItem then
Handled := True;
end;
This might be helpful:
procedure TForm1.TreeView1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
var node : TTreeNode;
begin
node := TreeView1.GetNodeAt(MousePos.X, MousePos.Y);
if not Assigned (node) then
Abort;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With