【发布时间】:2014-05-03 04:29:21
【问题描述】:
我在面板中有一个计时器,当计时器计时,它会更改矩形的坐标。
我尝试了两种方法: 1.在onPaint方法里面, 2.定时器调用函数创建图形并绘制移动矩形
第一个不起作用,但是当我切换窗口时,它移动了一次。
第二个有问题。它正在移动,但之前的位置被填充了颜色,这意味着图形没有刷新。
我只是使用g.FillRectangles() 来做到这一点。
谁能帮帮我?
附: the panel is using a transparent background.
添加: 这是一个 System.Windows.Form.Timer
timer = new Timer();
timer.Enabled = false;
timer.Interval = 100; /* 100 millisec */
timer.Tick += new EventHandler(TimerCallback);
private void TimerCallback(object sender, EventArgs e)
{
x+=10;
y+=10;
//drawsomething();
return;
}
1.
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
g.FillRectangle(Brushes.Red, x, y, 100, 100);
base.OnPaint(e);
}
2。
private void drawsomething()
{
if (graphics == null)
graphics = CreateGraphics();
graphics.FillRectangle(Brushes.Red, x, y, 100, 100);
}
【问题讨论】:
-
请发布您的定时器控制事件代码。
-
尝试使用 public virtual void Refresh(); Check here
-
试试这个 Form.ActiveForm.Refresh();或 this.Refresh();
-
嗨,himanshu,Refresh() 都只是刷新整个表单,而矩形没有刷新