62,046
社区成员
发帖
与我相关
我的任务
分享
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
}
private PerformanceCounter _oPerformanceCounter;
private Queue<float> queue;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this._oPerformanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
this.queue = new Queue<float>();
Timer timer = new Timer();
timer.Interval = 200;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
float _nVal = _oPerformanceCounter.NextValue();
this.queue.Enqueue(_nVal);
if (this.queue.Count > 50)
{
this.queue.Dequeue();
}
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.queue.Count > 1)
{
float[] fa = this.queue.ToArray();
for (int i = 1; i < fa.Length; i++)
{
e.Graphics.DrawLine(SystemPens.ControlText, i * 5, this.ClientRectangle.Height - fa[i] * this.ClientRectangle.Height / 100, (i - 1) * 5, this.ClientRectangle.Height - fa[i - 1] * this.ClientRectangle.Height / 100);
}
}
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
_oPerformanceCounter.Dispose();
}
...