用GDI画点,然后用Timer计算出下一个时刻的位置,使得它移动;
g.FillEllipse (redBrush,(int)TemLoaX,(int)TemLoaY,PoiWid,PoiHig);
可是,移动后,之前的点不会消失;加上语句:this.Invalidate ();屏幕上就闪不出来东西,是不是刷新太快,还是其他原因?
g.FillEllipse (redBrush,(int)TemLoaX,(int)TemLoaY,PoiWid,PoiHig);
可是,移动后,之前的点不会消失;加上语句:this.Invalidate ();屏幕上就闪不出来东西,是不是刷新太快,还是其他原因?
timer1.Enabled = true ;
timer1.Interval = 1500;
是的在OnPain()里面应该是可以,可是画图应该是触发某个按钮才动作的阿,不能以进来就运行巴
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor,true);
this.Refresh();
这样吗?
在OnPain()函数里面,可是还是一闪一闪的,而且停止的时候都看不到小球!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace Timer
{
public partial class Form1 : Form
{
public int X;
public int Y;
public bool Draw;
public int wi;
public int he;
public Form1()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
InitializeComponent();
this.X = 10;
this.Y = 10;
this.wi = 20;
this.he = 20;
} private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (Draw == true)
{
g.FillEllipse(new SolidBrush(Color.Blue), X, Y, wi, he);
}
} private void timer1_Tick(object sender, EventArgs e)
{
if (this.X <= this.Width - 180) this.X += 20;
else this.X = 10;
this.Refresh();
} private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
this.Draw = true;
} private void button2_Click(object sender, EventArgs e)
{
this.timer1.Stop();
}
}
}
这句有问题,找不到Generic命名空间啊?
如果改成using System.Collections,这句:this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);会出错,不包含ControlStyles.OptimizedDoubleBuffer 的定义;