Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set popup menu for ListView header bar together with items popup menu?

I have a ListView with ViewStyle = vsReport and two popup menus:

  1. Column popup menu, which I want to open when user right-clicking the header bar
  2. Item popup menu, must open when the user right-clicking any list item/subitem or whitespace below items.

What is the most correct way to show that menus? Which events should I handle?

The problem is when I set ListView.PopupMenu property, the popup menu appearing after right-clicking any point in ListView's client rectangle.

When I handle ListView.OnColumnRightClick event, if fires only after clicking on column header, excluding free space of the header bar (on the right of columns).

Event LisView.OnMouseUp fires only after right-clicking on whitespace below items.

like image 870
Andrew Avatar asked Jun 29 '12 09:06

Andrew


1 Answers

You don't have to use the PopupMenu property of the listview, leave it unset and you can attach a handler to OnContextPopup event and launch whatever popup menu you'd like depending on the position. Example:

procedure TForm1.ListViewContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
var
  HeaderRect: TRect;
  Pos: TPoint;
begin
  GetWindowRect(ListView_GetHeader(ListView.Handle), HeaderRect);
  Pos := ListView.ClientToScreen(MousePos);
  if PtInRect(HeaderRect, Pos) then
    PopupMenuColumns.Popup(Pos.X, Pos.Y)
  else
    PopupMenuItems.Popup(Pos.X, Pos.Y);
end;
like image 54
Sertac Akyuz Avatar answered Sep 22 '22 22:09

Sertac Akyuz