我只是把性能计数器加到了上次的折线统计图控件上,就发现运行时窗体上不显示它了,这是IDE的问题还是代码的问题啊
private int[] datas = new int[40];
public int[] Datas
{
get
{
return datas;
}
set
{
if (datas.Length > 1)
{
datas = value;
}
this.Invalidate();
panel1.Invalidate();
points = new Point[datas.Length];
}
}
private Point[] points = new Point[10];
private int maxValue = 100;
private int minValue = 0;
public int MaxValue
{
get
{
return maxValue;
}
set
{
maxValue = value;
this.Invalidate();
panel1.Invalidate();
}
}
public int MinValue
{
get
{
return minValue;
}
set
{
minValue = value;
this.Invalidate();
panel1.Invalidate();
}
}
private int numberOfHorizontalLines = 5;
public int NumberOfHorizontalLines
{
get
{
return numberOfHorizontalLines;
}
set
{
numberOfHorizontalLines = Math.Abs(value);
this.Invalidate();
panel1.Invalidate();
}
}
private int numberOfVerticalLines = 5;
public int NumberOfVerticalLines
{
get
{
return numberOfVerticalLines;
}
set
{
numberOfVerticalLines = Math.Abs(value);
this.Invalidate();
panel1.Invalidate();
}
}
private Color backLineColor = Color.DarkGray;
private Color lineColor = Color.DodgerBlue;
public Color BackLineColor
{
get
{
return backLineColor;
}
set
{
backLineColor = value;
this.Invalidate();
panel1.Invalidate();
}
}
public Color LineColor
{
get
{
return lineColor;
}
set
{
lineColor = value;
this.Invalidate();
panel1.Invalidate();
}
}
private bool isUsingSizeControl = true;
public bool IsUsingSizeControl
{
get
{
return isUsingSizeControl;
}
set
{
isUsingSizeControl = value;
this.Invalidate();
panel1.Invalidate();
}
}
private byte fillColorA = 100;
public byte FillColorA
{
get
{
return fillColorA;
}
set
{
fillColorA = value;
this.Invalidate();
panel1.Invalidate();
}
}
private DashStyle lineStyle = DashStyle.Solid;
public DashStyle LineStyle
{
get
{
return lineStyle;
}
set
{
lineStyle = value;
this.Invalidate();
panel1.Invalidate();
}
}
private SmoothingMode smoothingMode = SmoothingMode.HighQuality;
public SmoothingMode SmoothingMode
{
get
{
return smoothingMode;
}
set
{
smoothingMode = value;
this.Invalidate();
panel1.Invalidate();
}
}
private string categoryname = "Processor Information";
public string CategoryName
{
get
{
return categoryname;
}
set
{
categoryname = value;
performanceCounter1.CategoryName = categoryname;
}
}
private string instancename = "_Total";
public string InstanceName
{
get
{
return instancename;
}
set
{
instancename = value;
performanceCounter1.InstanceName = instancename;
}
}
private string countername = "% Processor Utility";
public string CounterName
{
get
{
return countername;
}
set
{
countername = value;
performanceCounter1.CounterName = countername;
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Datas[Datas.Length - 1] = (int)performanceCounter1.NextValue();
for (int i = 0; i < datas.Length - 1; i++)
{
Datas[i] = Datas[i + 1];
}
for (int i = 0; i < datas.Length; i++)
{
points[i] = new Point(panel1.Width / (datas.Length - 1) * i, panel1.Height - (panel1.Height / (maxValue - minValue - 1) * (datas[i] - minValue)));
}
Color realfillcolor = Color.FromArgb(fillColorA, lineColor.R, lineColor.G, lineColor.B);
Graphics graphics = e.Graphics;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = smoothingMode;
Pen pen = new Pen(backLineColor, 1);
Pen linepen = new Pen(lineColor, 2);
linepen.DashStyle = lineStyle;
Brush brush = new SolidBrush(realfillcolor);
graphics.DrawLine(pen, 0, 0, panel1.Width - 1, 0);
graphics.DrawLine(pen, 0, panel1.Height - 1, panel1.Width - 1, panel1.Height - 1);
graphics.DrawLine(pen, panel1.Width - 1, panel1.Height - 1, panel1.Width - 1, 0);
graphics.DrawLine(pen, 0, 0, 0, panel1.Height - 1);
for (int i = 1; i <= numberOfHorizontalLines; i++)
{
graphics.DrawLine(pen, 0, (panel1.Height - 1) / (numberOfHorizontalLines + 1) * i, panel1.Width - 1, (panel1.Height - 1) / (numberOfHorizontalLines + 1) * i);
}
for (int i = 1; i <= numberOfVerticalLines; i++)
{
graphics.DrawLine(pen, (panel1.Width - 1) / (numberOfVerticalLines + 1) * i, 0, (panel1.Width - 1) / (numberOfVerticalLines + 1) * i, panel1.Height - 1);
}
for (int i = 0; i < datas.Length - 1; i++)
{
graphics.DrawLine(linepen, points[i], points[i + 1]);
}
if (fillColorA != 0)
{
Point[] polygonpoints = new Point[points.Length + 2];
polygonpoints.SetValue(new Point(0, panel1.Height - 1), 0);
points.CopyTo(polygonpoints, 1);
polygonpoints.SetValue(new Point(panel1.Width - 1, panel1.Height - 1), polygonpoints.Length - 1);
graphics.FillPolygon(brush, polygonpoints);
}
if (isUsingSizeControl == true)
{
this.Height = (maxValue - minValue - 1) * ((this.Height - 1) / (maxValue - minValue - 1)) + 6;
this.Width = (datas.Length - 1) * ((this.Width - 1) / (datas.Length - 1)) + 6;
}
}
private void LineChart_Load(object sender, EventArgs e)
{
points = new Point[datas.Length];
if (isUsingSizeControl == true)
{
this.Height = (maxValue - minValue - 1) * ((this.Height - 1) / (maxValue - minValue - 1)) + 6;
this.Width = (datas.Length - 1) * ((this.Width - 1) / (datas.Length - 1)) + 6;
}
}
private void LineChart_Resize(object sender, EventArgs e)
{
if (isUsingSizeControl == true)
{
this.Height = (maxValue - minValue - 1) * ((this.Height - 1) / (maxValue - minValue - 1)) + 6;
this.Width = (datas.Length - 1) * ((this.Width - 1) / (datas.Length - 1)) + 6;
}
panel1.Width = this.Width - 6;
panel1.Height = this.Height - 6;
}