Question

I've been using Delphi XE4 for a week now and I've just discovered the new TListGroups feature. It's easy to set the TlistItem's GroupID to switch/move between the groups with ease. Now, I would like to have a procedure that calls as soon as a TListItem LEAVES a Group and/or as soon as a TlistItem ENTERS a Group. I'm looking for something like this:

procedure TForm1.ListView1On(Before/After)ListGroupEnter(Sender: TObject; Item: TListItem);
begin
 // Do some stuff here like...
 // AllowedToDraw := TRUE/FALSE
 // I/O Code...
 // etc...
end;

procedure TForm1.ListView1On(Before/After)ListGroupLeave(Sender: TObject; Item: TListItem);
begin
 // Do some stuff here like...
 // This might be replaceable with OnDeletion...
 // I/O Code...
end;

The main problem here is to detect the switch between various groups if you set the GroupID property. So is there anything to detect the switch as soon as you change/set the GroupID property?

Was it helpful?

Solution

The TListItem.GroupID property setter sends a LVM_SETITEM message to the TListView's HWND. You can subclass the TListView and intercept that message before the TListView processes it, eg:

const
  APPWM_LISTVIEW_CHECKGROUP = WM_APP + 100;

var
  PrevLVWndProc: TWndMethod = nil;

procedure TForm1.FormCreate(Sender: TObject);
begin
  PrevLVWndProc := ListView1.WindowProc;
  ListView1.WindowProc := LVWndProc;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ListView1.WindowProc := PrevLVWndProc;
end;

procedure TForm1.LVWndProc(var Message: TMessage);
var
  LVItem: PLVItem;
  ListItem: TListItem;
begin
  case Message.Msg of
    LVM_SETITEM:
    begin
      LVItem := PLVItem(Message.LParam);
      if (LVItem^.mask and LVIF_GROUPID) <> 0 then
      begin
        ListItem := ListView1.Items[LVItem.iItem];

        if ListItem.GroupID <> LVItem.iGroupID then
        begin
          if ListItem.GroupID >= 0 then
            ListView1GroupLeave(ListView1, ListItem);

          PrevLVWndProc(Message);

          // this gives the TListItem time to actually update itself
          PostMessage(ListView1.Handle, APPWM_LISTVIEW_CHECKGROUP, 0, LVItem.iItem);

          Exit;
        end;
      end;
    end;
    APPWM_LISTVIEW_CHECKGROUP:
    begin
      ListItem := ListView1.Items[Message.LParam];
      if ListItem.GroupID >= 0 then
        ListView1GroupEnter(ListView1, ListItem);
      Exit;
    end;
  end;

  PrevLVWndProc(Message);
end;

procedure TForm1.ListView1GroupLeave(Sender: TObject; Item: TListItem);
begin
  // Item is about to leave from its current GroupID ...
end;

procedure TForm1.ListView1GroupEnter(Sender: TObject; Item: TListItem);
begin
  // Item has entered into its current GroupID ...
end;    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top