C#简单的日志打印控件
partial class DataGridViewInfo
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AllowUserToResizeColumns = false;
this.dataGridView1.AllowUserToResizeRows = false;
this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
this.dataGridView1.BackgroundColor = System.Drawing.Color.Black;
this.dataGridView1.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.ColumnHeadersVisible = false;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1});
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.Color.Black;
dataGridViewCellStyle1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle1;
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.GridColor = System.Drawing.Color.Black;
this.dataGridView1.Location = new System.Drawing.Point(0, 0);
this.dataGridView1.MultiSelect = false;
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.RowTemplate.Height = 100;
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.Size = new System.Drawing.Size(250, 160);
this.dataGridView1.TabIndex = 4;
//
// Column1
//
this.Column1.HeaderText = "Column1";
this.Column1.Name = "Column1";
this.Column1.ReadOnly = true;
this.Column1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
this.Column1.Width = 5;
//
// DataGridViewInfo
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Black;
this.Controls.Add(this.dataGridView1);
this.Name = "DataGridViewInfo";
this.Size = new System.Drawing.Size(250, 160);
this.Load += new System.EventHandler(this.DataGridViewInfo_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
}
public partial class DataGridViewInfo : UserControl
{
public DataGridViewInfo()
{
InitializeComponent();
this.BackColorChanged += DataGridViewInfo_BackColorChanged;
this.FontChanged += DataGridViewInfo_FontChanged;
}
private void DataGridViewInfo_FontChanged(object sender, EventArgs e)
{
this.dataGridView1.DefaultCellStyle.Font = this.Font;
}
private void DataGridViewInfo_BackColorChanged(object sender, EventArgs e)
{
this.dataGridView1.BackgroundColor = this.BackColor;
this.dataGridView1.DefaultCellStyle.BackColor = this.BackColor;
}
int _maxRowCount = 5000;
[Description("显示的最大行数"), Category("自定义属性")]
public int MaxRowCount
{
get
{
return _maxRowCount;
}
set
{
_maxRowCount = value;
}
}
[Description("选择一行时的背景颜色"), Category("自定义属性")]
public Color SelectBackColor
{
get
{
return this.dataGridView1.DefaultCellStyle.SelectionBackColor;
}
set
{
this.dataGridView1.DefaultCellStyle.SelectionBackColor = value;
}
}
[Description("行高度"), Category("自定义属性")]
public int RowHeight
{
get
{
return _rowHeight;
}
set
{
_rowHeight = value;
this.dataGridView1.RowTemplate.Height = _rowHeight;
}
}
int _rowHeight = 25;
/// <summary>
/// 向信息显示栏添加一行
/// </summary>
/// <param name="text">添加的信息</param>
/// <param name="color">信息的前景色</param>
public void WriteLine(string text, Color color)
{
while (this.dataGridView1.Rows.Count > MaxRowCount)
{
this.dataGridView1.Rows.RemoveAt(0);
}
int index = this.dataGridView1.Rows.Add(1);
DataGridViewCell cell = this.dataGridView1.Rows[index].Cells[0];
cell.Style.ForeColor = color;
cell.Value = text;
this.dataGridView1.CurrentCell = cell;
this.dataGridView1.ClearSelection();
}
[Description("新添加一行时发生"), Category("自定义事件")]
public event DataGridViewRowsAddedEventHandler RowsAddedEvent;
private void DataGridViewInfo_Load(object sender, EventArgs e)
{
this.dataGridView1.RowsAdded += RowsAddedEvent;
}
}