Delphi XE4检测TListGroups之间的项目切换

时间:2013-05-15 10:53:24

标签: delphi listview delphi-xe4

我一直在使用Delphi XE4一周,我刚刚发现了新的TListGroups功能。您可以轻松设置TlistItem的{​​{1}}以便在群组之间切换/移动。现在,我希望有一个程序,只要GroupID离开一个组和/或TListItem输入一个组即可立即调用。我正在寻找这样的东西:

TlistItem

这里的主要问题是如果设置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; 属性,则检测各组之间的切换。 因此,一旦更改/设置GroupID属性,有什么可以检测到开关吗?

1 个答案:

答案 0 :(得分:4)

TListItem.GroupID属性设置器向LVM_SETITEM HWND发送TListView's消息。您可以继承TListView并在TListView处理之前截取该消息,例如:

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;