【问题标题】:How to refresh graphics in C#如何在 C# 中刷新图形
【发布时间】: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() 都只是刷新整个表单,而矩形没有刷新

标签: c# graphics refresh


【解决方案1】:

您可以使用Invalidate 方法。此方法使控件的特定区域无效并导致向控件发送绘制消息。

详情在这里:http://msdn.microsoft.com/ru-ru/library/system.windows.forms.panel.invalidate%28v=vs.110%29.aspx

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • @MikeLischke:我同意你的观点,但政策似乎有所不同。见this question on Meta
  • @MikeLischke:感谢您的评论。我是 StackOverflow 的新人作为回答者。现在答案已更正。
  • @Truba,现在这更有意义了。
【解决方案2】:

重绘后需要调用控件类的Refresh方法。它

强制控件使其客户区无效并立即 重绘自身和任何子控件。

【讨论】:

    【解决方案3】:

    this.Invalidate() 放在 TimerCallback 事件中。

    private void TimerCallback(object sender, EventArgs e)
    {
        x+=10;
        y+=10;
    
        this.Invalidate();
        return;
    }
    

    删除drawsomething 函数。这里不需要。

    完整代码:

    public partial class Form1 : Form
    {
        Timer timer = new Timer();
    
        int x;
        int y;
        public Form1()
        {
            InitializeComponent();
            timer.Enabled = true;
            timer.Interval = 100;  /* 100 millisec */
            timer.Tick += new EventHandler(TimerCallback);
        }
        private void TimerCallback(object sender, EventArgs e)
        {
            x += 10;
            y += 10;
            this.Invalidate();
            return;
        }
        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);
        }
    }
    

    【讨论】:

    • 谢谢,但它只适用于简单的矩形。我将它应用到我的实际程序中,但它无法刷新。
    • 您是否启用了定时器控制?
    • 是的,矩形一直在移动。但旧位置仍然是彩色的。
    • P.S. the panel is using a transparent background. 你为什么选择这个小组?
    • 我的表单中有一张背景图片,并且我需要在此表单中有几个区域显示移动的对象。因此我添加了一个面板来控制特定区域,但不希望它覆盖原始背景图片。
    猜你喜欢
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    相关资源
    最近更新 更多