我做了一个测试程序 。功能是在控件里跟鼠标绘二条正交的线
程序如下:
当我鼠标移动的快一些的时候,CPU占用率都在30%左右有什么办法可以降低CPU的占用率呢!?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace Performance
{
public class Scale : Control
{
/// <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()
{
components = new System.ComponentModel.Container();
} #endregion public Scale()
{
//enable double buffering of graphics
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
} Point p1 = new Point(0, 0), p2 = new Point(0, 0), p3 = new Point(0, 0), p4 = new Point(0, 0);
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(Pens.Blue, p1, p2);
g.DrawLine(Pens.Green, p3, p4);
} protected override void OnMouseMove(MouseEventArgs e)
{
p1 = new Point(0, e.Y);
p2 = new Point(this.Width, e.Y);
p3 = new Point(e.X, 0);
p4 = new Point(e.X, this.Height);
Invalidate();
}
}
}
程序如下:
当我鼠标移动的快一些的时候,CPU占用率都在30%左右有什么办法可以降低CPU的占用率呢!?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace Performance
{
public class Scale : Control
{
/// <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()
{
components = new System.ComponentModel.Container();
} #endregion public Scale()
{
//enable double buffering of graphics
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
} Point p1 = new Point(0, 0), p2 = new Point(0, 0), p3 = new Point(0, 0), p4 = new Point(0, 0);
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(Pens.Blue, p1, p2);
g.DrawLine(Pens.Green, p3, p4);
} protected override void OnMouseMove(MouseEventArgs e)
{
p1 = new Point(0, e.Y);
p2 = new Point(this.Width, e.Y);
p3 = new Point(e.X, 0);
p4 = new Point(e.X, this.Height);
Invalidate();
}
}
}
{
private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion public Scale()
{
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
} Point p1 = new Point(0, 0), p2 = new Point(0, 0),
p3 = new Point(0, 0), p4 = new Point(0, 0); private void DrawGraphic(Graphics g)
{
g.FillRectangle(new SolidBrush(BackColor), g.ClipBounds);
g.DrawLine(Pens.Blue, p1, p2);
g.DrawLine(Pens.Green, p3, p4);
} protected override void OnPaint(PaintEventArgs e)
{
DrawGraphic(e.Graphics);
} protected override void OnMouseMove(MouseEventArgs e)
{
p1 = new Point(0, e.Y);
p2 = new Point(Width, e.Y);
p3 = new Point(e.X, 0);
p4 = new Point(e.X, Height);
Graphics g = CreateGraphics();
DrawGraphic(g);
g.Dispose();
}
}
比如office提供了一些控件图就画的很漂亮,3D的都很漂亮
还有www.codeproject.com提供的ZedGraph.dll画图就很漂亮,而且提供的功能也多,形式也很多样
以前我做过一个小项目就是ZedGraph.dll,画出来的图大家都觉得很满意:)