public int CompareTo(object obj) { TextSubItem item = obj as TextSubItem; if (item == null) { throw new NotImplementedException(""); } return(this.Text.CompareTo(item.Text)); }
/// <summary> /// 清理鼠标所在子项的区域的阴影. /// </summary> private void ClearSubItemMouseOn() { if (m_mouseOnSubItem != null) { this.Invalidate(new Rectangle( m_mouseOnSubItem.Bounds.X, m_mouseOnSubItem.Bounds.Y - chatVScroll.Value, m_mouseOnSubItem.Bounds.Width, m_mouseOnSubItem.Bounds.Height)); m_mouseOnSubItem = null; } }
protected virtual void DrawSubItem(Graphics g, TextSubItem subItem, ref Rectangle rectSubItem, SolidBrush sb) { //设置子项的颜色. if (subItem.Equals(SelectSubItem)) { sb.Color = this.subItemSelectColor; } else if (subItem.Equals(m_mouseOnSubItem)) { sb.Color = this.subItemMouseOnColor; } else { sb.Color = this.subItemColor; } //设置文本绘制的方式. StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Near; sf.FormatFlags = StringFormatFlags.FitBlackBox; //计算子项的区域的大小 rectSubItem.Y = rectSubItem.Bottom + 1; Rectangle rect = rectSubItem; rect.Width -= 20; //测量文本绘制区域的大小. SizeF size = g.MeasureString(subItem.Text, this.Font, rect.Width, sf); rectSubItem.Height = (int)size.Height + 8;//调整子项区域的高度. //填充该区域. g.FillRectangle(sb, rectSubItem); //绘制文本. rect = TextRect(rectSubItem);//计算文本绘制区域. g.DrawString(subItem.Text, this.Font, Brushes.Black, rect, sf); subItem.Bounds = new Rectangle(rectSubItem.Location, rectSubItem.Size); }
/// <summary> /// 鼠标移动 处理 /// </summary> /// <param name="e"></param> 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; #region 新算法 foreach (var item in Items) { if (item.Bounds.Contains(m_ptMousePos)) { if (item.IsOpen) { //子项是展开的.查找子项. foreach (var subitem in item.SubItems) { //鼠标在子项内. if (subitem.Bounds.Contains(m_ptMousePos)) { if (subitem.Equals(m_mouseOnSubItem))//与上次的子项一致. { return; } else { //引发鼠标在子项上的事件. OnMouseOnSubItem(new GroupListBoxEventArgs(item, subitem)); } ClearSubItemMouseOn(); ClearItemMouseOn(); m_mouseOnSubItem = subitem; this.Invalidate(new Rectangle( subitem.Bounds.X, subitem.Bounds.Y - chatVScroll.Value, subitem.Bounds.Width, subitem.Bounds.Height)); return; } } ClearSubItemMouseOn(); //子项没有发现 那么判断是否在列表上面 if (new Rectangle(0, item.Bounds.Top - chatVScroll.Value, this.Width, 20).Contains(e.Location)) { //在原先的项上. if (item.Equals(m_mouseOnItem)) { return; } //不再原先的项上 ClearItemMouseOn(); m_mouseOnItem = item; this.Invalidate(new Rectangle( m_mouseOnItem.Bounds.X, m_mouseOnItem.Bounds.Y - chatVScroll.Value, m_mouseOnItem.Bounds.Width, m_mouseOnItem.Bounds.Height)); return; } } else { if (item.Equals(m_mouseOnItem)) { return; } ClearItemMouseOn(); ClearSubItemMouseOn(); m_mouseOnItem = item; //绘制指定的区域. this.Invalidate(new Rectangle( m_mouseOnItem.Bounds.X, m_mouseOnItem.Bounds.Y - chatVScroll.Value, m_mouseOnItem.Bounds.Width, m_mouseOnItem.Bounds.Height)); return; } } } #endregion #region 老算法 //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; // } // } //} #endregion //若循环结束 既不在列表上也不再子项上 清空上面的颜色 ClearItemMouseOn(); ClearSubItemMouseOn(); base.OnMouseMove(e); }
public GroupListBoxEventArgs(TextGroupItem mouseOnSubItem, TextSubItem selectSubItem) { _SelectSubItem = selectSubItem; _MouseOnsubItem = mouseOnSubItem; }