/// <summary> /// 根据索引位置插入一个列表项 /// </summary> /// <param name="index">索引位置</param> /// <param name="item">要插入的列表项</param> public void Insert(int index, ChatListItem item) { if (index < 0 || index >= this.count) { throw new IndexOutOfRangeException("Index was outside the bounds of the array"); } if (item == null) { throw new ArgumentNullException("Item cannot be null"); } this.EnsureSpace(1); for (int i = this.count; i > index; i--) { m_arrItem[i] = m_arrItem[i - 1]; } item.OwnerChatListBox = this.owner; m_arrItem[index] = item; this.count++; this.owner.Invalidate(); }
public ChatListSubItemCollection(ChatListItem owner) { this.owner = owner; }
protected override void OnMouseMove(MouseEventArgs e) { m_ptMousePos = e.Location; if (chatVScroll.IsMouseDown) { //如果滚动条的滑块处于被点击 那么移动 chatVScroll.MoveSliderFromLocation(e.Y); return; } if (chatVScroll.ShouldBeDraw) { //如果控件上有滚动条 判断鼠标是否在滚动条区域移动 if (chatVScroll.Bounds.Contains(m_ptMousePos)) { ClearItemMouseOn(); ClearSubItemMouseOn(); if (chatVScroll.SliderBounds.Contains(m_ptMousePos)) { chatVScroll.IsMouseOnSlider = true; } else { chatVScroll.IsMouseOnSlider = false; } if (chatVScroll.UpBounds.Contains(m_ptMousePos)) { chatVScroll.IsMouseOnUp = true; } else { chatVScroll.IsMouseOnUp = false; } if (chatVScroll.DownBounds.Contains(m_ptMousePos)) { chatVScroll.IsMouseOnDown = true; } else { chatVScroll.IsMouseOnDown = false; } return; } else { chatVScroll.ClearAllMouseOn(); } } m_ptMousePos.Y += chatVScroll.Value; //如果不在滚动条范围类 那么根据滚动条当前值计算虚拟的一个坐标 for (int i = 0, Len = items.Count; i < Len; i++) { //然后判断鼠标是否移动到某一列表项或者子项 if (items[i].Bounds.Contains(m_ptMousePos)) { if (items[i].IsOpen) { //如果展开 判断鼠标是否在某一子项上面 for (int j = 0, lenSubItem = items[i].SubItems.Count; j < lenSubItem; j++) { if (items[i].SubItems[j].Bounds.Contains(m_ptMousePos)) { if (m_mouseOnSubItem != null) { //如果当前鼠标下子项不为空 if (items[i].SubItems[j].HeadRect.Contains(m_ptMousePos)) { //判断鼠标是否在头像内 if (!m_bOnMouseEnterHeaded) { //如果没有触发进入事件 那么触发用户绑定的事件 OnMouseEnterHead(new ChatListEventArgs(this.m_mouseOnSubItem, this.selectSubItem)); m_bOnMouseEnterHeaded = true; } } else { if (m_bOnMouseEnterHeaded) { //如果已经执行过进入事件 那触发用户绑定的离开事件 OnMouseLeaveHead(new ChatListEventArgs(this.m_mouseOnSubItem, this.selectSubItem)); m_bOnMouseEnterHeaded = false; } } } if (items[i].SubItems[j].Equals(m_mouseOnSubItem)) { return; } ClearSubItemMouseOn(); ClearItemMouseOn(); m_mouseOnSubItem = items[i].SubItems[j]; this.Invalidate(new Rectangle( m_mouseOnSubItem.Bounds.X, m_mouseOnSubItem.Bounds.Y - chatVScroll.Value, m_mouseOnSubItem.Bounds.Width, m_mouseOnSubItem.Bounds.Height)); //this.Invalidate(); return; } } ClearSubItemMouseOn(); //循环做完没发现子项 那么判断是否在列表上面 if (new Rectangle(0, items[i].Bounds.Top - chatVScroll.Value, this.Width, 20).Contains(e.Location)) { if (items[i].Equals(m_mouseOnItem)) { return; } ClearItemMouseOn(); m_mouseOnItem = items[i]; this.Invalidate(new Rectangle( m_mouseOnItem.Bounds.X, m_mouseOnItem.Bounds.Y - chatVScroll.Value, m_mouseOnItem.Bounds.Width, m_mouseOnItem.Bounds.Height)); //this.Invalidate(); return; } } else { //如果列表项没有展开 重绘列表项 if (items[i].Equals(m_mouseOnItem)) { return; } ClearItemMouseOn(); ClearSubItemMouseOn(); m_mouseOnItem = items[i]; this.Invalidate(new Rectangle( m_mouseOnItem.Bounds.X, m_mouseOnItem.Bounds.Y - chatVScroll.Value, m_mouseOnItem.Bounds.Width, m_mouseOnItem.Bounds.Height)); //this.Invalidate(); return; } } }//若循环结束 既不在列表上也不再子项上 清空上面的颜色 ClearItemMouseOn(); ClearSubItemMouseOn(); base.OnMouseMove(e); }
/// <summary> /// 判断一个列表项是否在集合内 /// </summary> /// <param name="item">要判断的列表项</param> /// <returns>是否在列表项</returns> public bool Contains(ChatListItem item) { return(this.IndexOf(item) != -1); }
/// <summary> /// 获取列表项所在的索引位置 /// </summary> /// <param name="item">要获取的列表项</param> /// <returns>索引位置</returns> public int IndexOf(ChatListItem item) { return(Array.IndexOf <ChatListItem>(m_arrItem, item)); }