我做了一个测试程序 。功能是在控件里跟鼠标绘二条正交的线
程序如下:
当我鼠标移动的快一些的时候,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();
        }
    }
}

解决方案 »

  1.   

    讨论是吧..托管代码GDI+慢 只是封装的好...想想微软的战略..为什么推出WPF..
      

  2.   

    别说GDI+了...只要是.NET我都不跟人讨论性能问题...
      

  3.   

    别用Invalidate();重绘会快一些,参考如下代码:public class Scale : Control
    {
        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();
        }
    }
      

  4.   

    好象wpf作图性能还是不错的。
      

  5.   

    建议使用WPF作图吧,.net的性能让人哭,又哭不出来!
      

  6.   

    做图.net提供了GDI,还有相关,我也画过一些图.但是总感觉画出来的图很难看,形式上也比较单薄一点
    比如office提供了一些控件图就画的很漂亮,3D的都很漂亮
    还有www.codeproject.com提供的ZedGraph.dll画图就很漂亮,而且提供的功能也多,形式也很多样
    以前我做过一个小项目就是ZedGraph.dll,画出来的图大家都觉得很满意:)