运动目标与Paint事件 [英] Moving Object with Paint Event

查看:218
本文介绍了运动目标与Paint事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试,明天,我们必须使用Paint事件重绘我们的对象,我们可以不使用定时器。



正如MSDN说:的。当控制重绘Paint事件引发的,但是,出现了我的名气,只有当窗体最小化,或者得到了无形的背面看到

我的代码:




 公共部分类Form1中:形态
{
公共图形drawArea;
公众诠释XPOS,yPos;

公共Form1中()
{
的InitializeComponent();
}
私人无效Form1_Paint(对象发件人,PaintEventArgs的E)
{
drawArea = e.Graphics;
DrawUser();
}

私人无效Form1_KeyDown(对象发件人,发送KeyEventArgs E)
{
开关(e.KeyCode)
{
钥匙的情况下。下:
yPos ++;
中断;
情况下Keys.Up:
yPos--;
中断;
情况下Keys.Left:
xPos--;
中断;
情况下Keys.Right:
XPOS ++;
中断;
}
}

私人无效DrawUser()
{
drawArea.FillRectangle(新SolidBrush(Color.Red),XPOS,yPos,50, 50);
}
}




所以,当我按箭头键多次,该对象仅移动后,我重新大小我的形式。我希望它瞬间移动,只能使用Paint事件。



感谢


解决方案

我发现它



通过添加 this.Invalidate(); <!按下该键后/ code>。这将告诉Paint事件重绘。


I have a test tomorrow, and we must use the paint event to redraw our objects, we may not use a timer.

As the MSDN says: "The Paint event is raised when the control is redrawn.", but that,occurs for my known, only when the form is minimized, or got invisible and back visible.

My code:

public partial class Form1 : Form
{
    public Graphics drawArea;
    public int xPos, yPos;

    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        drawArea = e.Graphics;
        DrawUser();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        { 
            case Keys.Down:
                yPos++;
                break;
            case Keys.Up:
                yPos--;
                break;
            case Keys.Left:
                xPos--;
                break;
            case Keys.Right:
                xPos++;
                break;
        }
    }

    private void DrawUser()
    {
        drawArea.FillRectangle(new SolidBrush(Color.Red), xPos, yPos, 50, 50);
    }
}

So, When I press the key arrows multiple times, the object only moves after I re-size my form. I want it to move instantly, only using the paint event.

Thanks

解决方案

I found it!

By adding this.Invalidate(); after the key is pressed. This will tell the paint event to redraw.

这篇关于运动目标与Paint事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆