protected override void OnDrawItem(DrawItemEventArgs e) { Brush fontBrush = null; if (e.Index > -1) { SListBoxItem item = (SListBoxItem)Items[e.Index]; if (item.Type == ItemType.Info) { fontBrush = Brushes.DarkGreen; } else if (item.Type == ItemType.Warn) { fontBrush = Brushes.Goldenrod; } else if (item.Type == ItemType.Error) { fontBrush = Brushes.Red; } e.DrawBackground(); e.Graphics.DrawString(item.Text, e.Font, fontBrush, e.Bounds); e.DrawFocusRectangle(); } base.OnDrawItem(e); }
//主线程中在窗口打印日志信息 private void LogCommentM(CommentType commentType, string comment) { string mark = null; ItemType itemType = ItemType.Error; if (commentType == CommentType.Info) { mark = "消息"; itemType = ItemType.Info; } else if (commentType == CommentType.Warn) { mark = "警告"; itemType = ItemType.Warn; } else if (commentType == CommentType.Error) { mark = "错误"; itemType = ItemType.Error; } string message = String.Format("{0} [{1}] {2}", DateTime.Now.ToString(), mark, comment); SListBoxItem item = new SListBoxItem(message, itemType); //添加滚动效果,在添加记录前,先计算滚动条是否在底部,从而决定添加后是否自动滚动 bool scoll = false; if (logsBox.TopIndex == logsBox.Items.Count - (int)(logsBox.Height / logsBox.ItemHeight)) { scoll = true; } //添加记录 logsBox.Items.Add(item); //滚动到底部 if (scoll) { logsBox.TopIndex = logsBox.Items.Count - (int)(logsBox.Height / logsBox.ItemHeight); } }
//主线程中在窗口打印日志信息 private void LogCommentM(CommentType commentType, string comment) { string mark = null; ItemType itemType = ItemType.Error; if (commentType == CommentType.Info) { mark = "消息"; itemType = ItemType.Info; } else if (commentType == CommentType.Warn) { mark = "警告"; itemType = ItemType.Warn; } else if (commentType == CommentType.Error) { mark = "错误"; itemType = ItemType.Error; } string message = String.Format("{0} [{1}] {2}", DateTime.Now.ToString(), mark, comment); SListBoxItem item = new SListBoxItem(message, itemType); //添加滚动效果,在添加记录前,先计算滚动条是否在底部,从而决定添加后是否自动滚动 bool scoll = false; if (logsBox.TopIndex == logsBox.Items.Count - (int)(logsBox.Height / logsBox.ItemHeight)) scoll = true; //添加记录 logsBox.Items.Add(item); //滚动到底部 if (scoll) logsBox.TopIndex = logsBox.Items.Count - (int)(logsBox.Height / logsBox.ItemHeight); }