处理ListView窗口消息

时间:2012-04-22 21:36:39

标签: c# listview

我正在尝试为从ListView添加或删除项目添加事件。以下是我到目前为止的情况:

protected override void OnNotifyMessage(Message m)
    {
        const int WM_ERASEBKGND = 0x14;
        const int LVM_FIRST = 0x1000;
        const int LVM_DELETEITEM = LVM_FIRST + 8;
        const int LVM_DELETEALLITEMS = LVM_FIRST + 9;
        const int LVM_INSERTITEMA = LVM_FIRST + 7;
        const int LVM_INSERTITEMW = LVM_FIRST + 77;

        //Filter out the WM_ERASEBKGND message to prevent flicker when redrawing
        if (m.Msg == WM_ERASEBKGND) return;

        base.OnNotifyMessage(m);

        if (m.Msg == LVM_DELETEITEM || m.Msg == LVM_DELETEALLITEMS)
        {
            if (ItemsRemoved == null) return;

            ItemsRemoved(null, EventArgs.Empty);
        }
        else if (m.Msg == LVM_INSERTITEMA || m.Msg == LVM_INSERTITEMW)
        {
            if (ItemsAdded == null) return;

            ItemsAdded(null, EventArgs.Empty);
        }
    }

问题是,当触发ItemsRemoved事件时,ListView的项目计数不变。例如,在执行ListView.Items.Clear()时,会处理LVM_DELETEALLITEMS消息,但是当触发ItemsRemoved事件时,项目计数仍显示为> 0

我在这里出错的任何想法?

0 个答案:

没有答案