/// <summary> /// 获取列表项所在的索引位置 /// </summary> /// <param name="item">要获取的列表项</param> /// <returns>索引位置</returns> public int IndexOf(ChatListItem item) { return Array.IndexOf<ChatListItem>(m_arrItem, item); }
/// <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(); }
/// <summary> /// 添加一个列表项的数组 /// </summary> /// <param name="items">要添加的列表项的数组</param> public void AddRange(ChatListItem[] items) { if (items == null) throw new ArgumentNullException("Items cannot be null"); this.EnsureSpace(items.Length); try { foreach (ChatListItem item in items) { if (item == null) throw new ArgumentNullException("Item cannot be null"); if (-1 == this.IndexOf(item)) { //重复数据不添加 item.OwnerChatListBox = this.owner; m_arrItem[this.count++] = item; } } } finally { this.owner.Invalidate(); } }
/// <summary> /// 判断一个列表项是否在集合内 /// </summary> /// <param name="item">要判断的列表项</param> /// <returns>是否在列表项</returns> public bool Contains(ChatListItem item) { return this.IndexOf(item) != -1; }
private void ClearItemMouseOn() { if (m_mouseOnItem != null) { this.Invalidate(new Rectangle( m_mouseOnItem.Bounds.X, m_mouseOnItem.Bounds.Y - chatVScroll.Value, m_mouseOnItem.Bounds.Width, m_mouseOnItem.Bounds.Height)); m_mouseOnItem = null; } }
/// <summary> /// 添加一个列表项 /// </summary> /// <param name="item">要添加的列表项</param> public void Add(ChatListItem item) { if (item == null) //空引用不添加 throw new ArgumentNullException("Item cannot be null"); this.EnsureSpace(1); if (-1 == this.IndexOf(item)) { //进制添加重复对象 item.OwnerChatListBox = this.owner; m_arrItem[this.count++] = item; this.owner.Invalidate(); //添加进去是 进行重绘 } }
/// <summary> /// 绘制列表项 /// </summary> /// <param name="g">绘图表面</param> /// <param name="item">要绘制的列表项</param> /// <param name="rectItem">该列表项的区域</param> /// <param name="sb">画刷</param> protected virtual void DrawItem(Graphics g, ChatListItem item, Rectangle rectItem, SolidBrush sb) { StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; sf.SetTabStops(0.0F, new float[] { 20.0F }); if (item.Equals(m_mouseOnItem)) //根据列表项现在的状态绘制相应的背景色 sb.Color = this.itemMouseOnColor; else sb.Color = this.itemColor; g.FillRectangle(sb, rectItem); if (item.IsOpen) { //如果展开的画绘制 展开的三角形 sb.Color = this.arrowColor; g.FillPolygon(sb, new Point[] { new Point(2, rectItem.Top + 11), new Point(12, rectItem.Top + 11), new Point(7, rectItem.Top + 16) }); } else { //如果没有展开判断该列表项下面的子项闪动的个数 if (item.TwinkleSubItemNumber > 0) { //如果列表项下面有子项闪动 那么判断闪动状态 是否绘制或者不绘制 if (item.IsTwinkleHide) //该布尔值 在线程中不停 取反赋值 return; } sb.Color = this.arrowColor; g.FillPolygon(sb, new Point[] { new Point(5, rectItem.Top + 8), new Point(5, rectItem.Top + 18), new Point(10, rectItem.Top + 13) }); } string strItem = "\t" + item.Text; sb.Color = this.ForeColor; sf.Alignment = StringAlignment.Near; g.DrawString(strItem, this.Font, sb, rectItem, sf); sf.Alignment = StringAlignment.Far; g.DrawString("[" + item.SubItems.GetOnLineNumber() + "/" + item.SubItems.Count + "]", this.Font, sb, new Rectangle(rectItem.X, rectItem.Y, rectItem.Width - 15, rectItem.Height), sf); }
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); }
//确认存储空间 private void EnsureSpace(int elements) { if (m_arrItem == null) m_arrItem = new ChatListItem[Math.Max(elements, 4)]; else if (this.count + elements > m_arrItem.Length) { ChatListItem[] arrTemp = new ChatListItem[Math.Max(this.count + elements, m_arrItem.Length * 2)]; m_arrItem.CopyTo(arrTemp, 0); m_arrItem = arrTemp; } }
/// <summary> /// 移除一个列表项 /// </summary> /// <param name="item">要移除的列表项</param> public void Remove(ChatListItem item) { int index = this.IndexOf(item); if (-1 != index) //如果存在元素 那么根据索引移除 this.RemoveAt(index); }
private void Showcontacts() { chatListBox1.Items.Clear(); if (userlistDept != null && userlistDept.Count > 0) { foreach (WkTDept o in userlistDept) { ChatListItem item = new ChatListItem(o.KdName.Trim()); if (userlistUser != null && userlistUser.Count > 0) { #region 二层循环 foreach (WkTUser oo in userlistUser) { if (oo.Kdid.Id == o.Id) { if (oo.Id != user.Id) { ChatListSubItem subItem = new ChatListSubItem("", oo.KuName.Trim(), ""); subItem.userid = int.Parse(oo.Id.ToString()); if (oo.KuOnline == 1) { subItem.Status = (ChatListSubItem.UserStatus)(1); } else { subItem.Status = (ChatListSubItem.UserStatus)(5); } item.SubItems.AddAccordingToStatus(subItem); } } } #endregion end 二层循环 item.SubItems.Sort(); chatListBox1.Items.Add(item); } } } this.timerOfRefreshOnline.Enabled = true; ; this.timerOfRefreshOnline.Start(); }
public ChatListSubItemCollection(ChatListItem owner) { this.owner = owner; }