奇怪的位置,同时移动的WinForms picbox [英] Weird location while moving WinForms picbox

查看:101
本文介绍了奇怪的位置,同时移动的WinForms picbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想不出弄清楚问题出调试完毕后,我终于决定写如下:

为什么位置跳来跳去? 147 86 294 212然后再逢回调?

  pic.MouseMove + = my_MouseMove;

my_MouseMove(对象发件人,发送MouseEventArgs E)

Console.WriteLine({0} {1} {2} {3},前,安永,e.Location.X,e.Location.Y);

147 86 147 86
294 212 294 212
147 86 147 86
294 212 294 212
147 86 147 86
294 212 294 212
 


  //这code期望的eX,Y的X,Y相对回报于母公司控制。
    //这似乎是相对的mousedown打破这一点。
    点heroClick =新的点();
    私人无效hero_MouseDown(对象发件人,发送MouseEventArgs E)
    {
        heroClick.X = e.X;
        heroClick.Y = e.Y;
    }
    私人无效hero_MouseMove(对象发件人,发送MouseEventArgs E)
    {
        如果(e.Button!= System.Windows.Forms.MouseButtons.Left)
            返回;
        VAR xDif = e.X  -  heroClick.X;
        VAR yDif = e.Y  -  heroClick.Y;
        heroClick.X + = xDif;
        heroClick.Y + = yDif;
        lvl.playerX + = xDif;
        lvl.playerY + = yDif;
        图片框PIC =((图片框)发送者);
        pic.Left + = xDif;
        pic.Top + = yDif;
        //Console.WriteLine("{0} {1} {2} {3},前,安永,e.Location.X,e.Location.Y,发送者);
        textBox2.Text =的String.Format({0} {1} {2} {3},前,安永,e.Location.X,e.Location.Y,发送者);
        textBox2.Update();
    }
 

解决方案

在code作为贴是相当不足的猜测可能是怎么回事。我会采取从线程主题,同时移动picBox的提示。如果您使用MouseMove事件以移动控制,那么你改变控件相对于鼠标的位置。下一个报告的位置将是不同的,即使你没有移动鼠标。自鼠标位置被报告相对于对照的客户区的左上角。

的效果通常很有意思,于是控制系统的POGO,来回跳动迅速。您需要弥补这一点,通常通过的没有的更新存储的previous鼠标的位置。没有看到任何相关的code,我不能给比这更好的建议。

I couldnt figure the problem out so after debugging i finally decide to write the below.

Why is the location jumping around?! 147 86 to 294 212 then back every callback?

pic.MouseMove += my_MouseMove;

my_MouseMove(object sender, MouseEventArgs e)

Console.WriteLine("{0} {1} {2} {3}", e.X, e.Y, e.Location.X, e.Location.Y);

147 86 147 86
294 212 294 212
147 86 147 86
294 212 294 212
147 86 147 86
294 212 294 212


    //This code expects e.X,Y to return the X,Y Relative to the parent control. 
    //It seems to be relative to mousedown which breaks this.
    Point heroClick = new Point();
    private void hero_MouseDown(object sender, MouseEventArgs e)
    {
        heroClick.X = e.X;
        heroClick.Y = e.Y;
    }
    private void hero_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != System.Windows.Forms.MouseButtons.Left)
            return;
        var xDif = e.X - heroClick.X;
        var yDif = e.Y - heroClick.Y;
        heroClick.X += xDif;
        heroClick.Y += yDif;
        lvl.playerX += xDif;
        lvl.playerY += yDif;
        PictureBox pic = ((PictureBox)sender);
        pic.Left += xDif;
        pic.Top+= yDif;
        //Console.WriteLine("{0} {1} {2} {3}", e.X, e.Y, e.Location.X, e.Location.Y, sender);
        textBox2.Text = string.Format("{0} {1} {2} {3}", e.X, e.Y, e.Location.X, e.Location.Y, sender);
        textBox2.Update();
    }

解决方案

The code as posted is quite insufficient to guess what might be going on. I'll take a hint from the thread subject "while moving picBox". If you use the MouseMove event to move a control then you change the position of the control relative to the mouse. The next reported position will be different, even if you didn't move the mouse. Since the mouse position is reported relative to the upper left corner of the client area of the control.

The effect is usually very interesting, the control does the pogo, jumping back and forth rapidly. You need to compensate for this, usually by not updating the stored previous mouse position. I can't give better advice than this without seeing any relevant code.

这篇关于奇怪的位置,同时移动的WinForms picbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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