读书人

图形闪烁如何解决?

发布时间: 2012-07-29 15:26:14 作者: rapoo

图形闪烁,怎么解决???
这是我的代码,window窗体中只有3个button控件和两个timer控件。程序运行时,所画的圆在移动过程中总是会闪烁。怎么办啊???
public partial class Form1 : Form
{
private int l = 100;
//private int m = 3;
private int x1 = 30;
private int x = 0;
private int y = 30;

public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);//防止窗口跳动
SetStyle(ControlStyles.DoubleBuffer, true);
}

private void paint_circle(int x)
{
Graphics g = this.CreateGraphics();
g.SmoothingMode = SmoothingMode.HighSpeed; ;
Image i = new Bitmap(this.Size.Width, this.Size.Height);
Graphics k = Graphics.FromImage(i);
g.FillEllipse(new SolidBrush(Color.Blue), x, y, l, l);
g.DrawEllipse(new Pen(Color.Red, 3), x, y, l, l);
g.DrawImage(i, 0, 0);
g.Dispose();

}

private void button1_Click(object sender, EventArgs e)
{
this.Refresh();
this.paint_circle(x1);
//this.Invalidate();
}
//右移
private void button3_Click(object sender, EventArgs e)
{
x = x1;
timer2.Stop();
timer1.Start();
}
//左移
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
timer2.Start();
}
//右移
private void timer1_Tick(object sender, EventArgs e)
{
this.Invalidate(false);
timer1.Interval = 10;
if ( x + l < this.Location.X + this.Size.Width )
{
x++;
this.Refresh();
this.paint_circle(x);
}
}
//左移
private void timer2_Tick(object sender, EventArgs e)
{
this.Invalidate(false);
timer1.Interval = 10;
if ( x > x1 )
{
x--;
this.Refresh();
this.paint_circle(x);

}
}
}

[解决办法]
最近老夫特别喜欢帮别人解决问题,
在你代码上修改了一下,只实现了向右移,你可以举一返三;
直接上代码 ,不解释

C# code
    public partial class Form1 : Form    {        private int l = 100;        private int x1 = 30;        private int x = 0;        private int y = 30;        public Form1()        {            InitializeComponent();            SetStyle(ControlStyles.UserPaint, true);            SetStyle(ControlStyles.AllPaintingInWmPaint, true);//防止窗口跳动            SetStyle(ControlStyles.DoubleBuffer, true);            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);        }        Pen pen = new Pen(Brushes.Red, 3);        Brush brush = Brushes.Blue;        private void paint_circle(Graphics g, int x)        {            g.FillEllipse(brush, x, y, l, l);            g.DrawEllipse(pen, x, y, l, l);        }        //右移        private void button3_Click(object sender, EventArgs e)        {            x = x1;            timer1.Start();        }        //右移        private void timer1_Tick(object sender, EventArgs e)        {            timer1.Interval = 10;            if (x++ < this.Bounds.Right)                this.Refresh();        }        private void Form1_Paint(object sender, PaintEventArgs e)        {            if (this.timer1.Enabled)            {                this.paint_circle(e.Graphics, x);            }        }    } 

读书人网 >C#

热点推荐