简体   繁体   English

PictureBox双缓冲按键滞后?

[英]PictureBox double buffer keypress lag?

Sometimes, If I hold down for example the left arrow key, the pictureBox will stop drawing the coordinates of my character. 有时,如果我按住左箭头键,则pictureBox将停止绘制我的角色的坐标。 A prime example is the yellow dot encircled in red. 一个典型的例子是用红色环绕的黄点。 By holding down the left arrow key, the pictureBox did not constantly draw my characters location, until I hit another key/let go of the arrow key, then the picturebox started updating again (as you can see encircled in purple). 通过按住左箭头键,pictureBox没有经常绘制我的角色位置,直到我按下另一个键/放开箭头键,然后图片框再次开始更新(你可以看到被环绕的紫色)。

Also, another error I am experiencing is the fact that it should only be drawing one yellow dot and one 'blue whirlpool' per location; 此外,我遇到的另一个错误是它应该只为每个位置绘制一个黄点和一个'蓝色漩涡'; while it is drawing multiple (as you can see). 虽然它正在绘制多个(如你所见)。

The code below is what I am using to replicate the picture results. 下面的代码是我用来复制图片结果的代码。

private Thread grabValues;

private Bitmap bm;

private Image image1;
private Image image2;

private Graphics gM;

void RGraphics()
{
    image1 = Properties.Resources.image1;
    image2 = Properties.Resources.image2;
}

void RInvokeMe()
{
    while (true)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new UpdateControlsDelegate(MUpdate));
            this.Invoke(new UpdateControlsDelegate(CUpdate));
        }
        else
        {
            MUpdate();
            CUpdate();
        }
    }
}

void MUpdate()
{
  pictureBox1.ImageLocation = "http://urlexample/r/we.png";
}

void CUpdate()
{
        bm = new Bitmap(pictureBox1.Image);

        gM = Graphics.FromImage(bm);

        gM.CompositingMode = CompositingMode.SourceOver;

        MDraw.DrawBlueDot(gM, image2, X2, Y2);
        MDraw.DrawYellowDot(gM, image1, X1, Y1);

        pictureBox1.Image = bm;
        pictureBox1.Refresh();
}

public Form2()
{
    InitializeComponent();

    RGraphics();

    grabValues = new Thread(new ThreadStart(RInvokeMe));
    grabValues.IsBackground = true;
    grabValues.Start();
}

Basically you start a thread ( grabValues , implemented by function RInvokeMe ) which endlessly invoke on your form methods MUpdate and CUpdate . 基本上你启动一个线程( grabValues ,由函数RInvokeMe实现),它在你的表单方法MUpdateCUpdate上无休止地调用。 When you invoke a method then you post a message on the windows message queue in order to let the window execute the given function. 当您调用方法时,您在Windows消息队列上发布消息,以便让窗口执行给定的函数。 But when you press a key even the keypress message is posted to the windows queue... but maybe it is not served quickly because of the tons of invoke requests you are already executing on the form. 但是,当您按下某个键时,即使按键消息也会发布到Windows队列中......但由于您已在表单上执行了大量的调用请求,因此可能无法快速提供。

You could try to introduce a small delay ( Thread.Sleep(500); ) in your RInvokeMe loop just to see if the problem is there... 您可以尝试在RInvokeMe循环中引入一个小延迟( Thread.Sleep(500); ),以查看问题是否存在...

Then if so, I would consider to completely re-design your project in order to avoid such a scenario (if your characters are only moved only when pressing keys, why you simply do not redraw the scene only upon keypress but you do it continuously?) 然后,如果是这样,我会考虑完全重新设计你的项目,以避免这种情况(如果你的角色只在按键时移动,为什么你只是在按键时不重绘场景但是你连续不断地重绘? )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM