internal void SetSelectionStartItem(Point p) { //清除其他的选择状态 this.Clear(); MessageListItem item = _owner.GetItemAtPosition(p) as MessageListItem; if (item == null) { return; } //设定文本选中 if (item.Message.Type == MessageType.Text) { this._selectingTextItem = item; Rectangle rect = item.Bounds; for (int i = 0; i < this._selectingTextItem.DrawingObjects.Count; i++) { DrawingObject dobj = item.DrawingObjects[i]; if (dobj.Type == DrawingObjectType.TextBlock) { Rectangle bounds = dobj.Offset(rect.X, rect.Y); if (bounds.Contains(p)) { TextBlockObj tb = dobj.Tag as TextBlockObj; StringPart sp = tb.StringPart; using (Graphics g = _owner.CreateGraphics()) { tb.SelectionStart = StringMeasurer.GetCharIndex(g, sp.Font, p.X - (int)dobj.X, sp.String); this._selectStartDoIndex = i; break; } } } } } }
internal void SetSelectionEndItem(Point location) { if (!this.HasSelection) { return; } MessageListItem hoverItem = _owner.GetItemAtPosition(location) as MessageListItem; if (hoverItem == null) { //鼠标移动到其他地方了,算了,不选了 this.Clear(); return; } if (hoverItem != this._selectingTextItem) { //当前的选择的不是上面一个 this.Clear(); return; } Rectangle rect = this._selectingTextItem.Bounds; for (int i = 0; i < this._selectingTextItem.DrawingObjects.Count; i++) { DrawingObject dobj = this._selectingTextItem.DrawingObjects[i]; if (dobj.Type == DrawingObjectType.TextBlock) { Rectangle bounds = dobj.Offset(rect.X, rect.Y); TextBlockObj tb = dobj.Tag as TextBlockObj; if (bounds.Contains(location)) { StringPart sp = tb.StringPart; using (Graphics g = _owner.CreateGraphics()) { tb.SelectionEnd = StringMeasurer.GetCharIndex(g, sp.Font, location.X - (int)dobj.X, sp.String); this._selectEndDoIndex = i; break; } } } } //开始与结束直接的所有TextBlock都设定为全选中 int startIndex = Math.Min(this._selectStartDoIndex, this._selectEndDoIndex); int endIndex = Math.Max(this._selectStartDoIndex, this._selectEndDoIndex); List <TextBlockObj> selObj = new List <TextBlockObj>(); for (int i = startIndex; i <= endIndex; i++) { DrawingObject dobj = this._selectingTextItem.DrawingObjects[i]; if (dobj.Type == DrawingObjectType.TextBlock) { TextBlockObj tbObj = dobj.Tag as TextBlockObj; if (this._selectStartDoIndex < this._selectEndDoIndex) { //从上往下选择时 if (i == this._selectStartDoIndex) { tbObj.SelectionEnd = tbObj.Length - 1; } else if (i == this._selectEndDoIndex) { tbObj.SelectionStart = 0; } } else if (this._selectStartDoIndex > this._selectEndDoIndex) { //从下往上选择时 if (i == this._selectStartDoIndex) { tbObj.SelectionEnd = 0; } else if (i == this._selectEndDoIndex) { tbObj.SelectionStart = tbObj.Length - 1; } } if (i != this._selectStartDoIndex && i != this._selectEndDoIndex) { tbObj.SelectAll(); } selObj.Add(tbObj); this._prevSelection.Remove(tbObj); } } //清除选择 foreach (TextBlockObj item in this._prevSelection) { item.ClearSelection(); } this._prevSelection = selObj; }