none
你知道这是什么原因吗? RRS feed

  • 问题

  • public partial class Form1 : Form { Point prePoint, curPoint; int PointNumber = 0; MOUSE_STATE mouse_state; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { pictureBox1.Image = Image.FromFile("D:\\签名系统\\图片resouce\\底图.jpg"); this.WindowState = FormWindowState.Maximized; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { textBox1.Text = PointNumber.ToString(); if (mouse_state == MOUSE_STATE.MOUSE_STATE_DOWN) { curPoint = Control.MousePosition; if (prePoint == curPoint) { MessageBox.Show("相等"); } PointNumber++; if (2 == PointNumber) { prePoint = curPoint; } else if (3 == PointNumber) { prePoint = curPoint; } else if (4 == PointNumber) { prePoint = curPoint; PointNumber = 2; } } } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { PointNumber = 1; prePoint = Control.MousePosition; mouse_state = MOUSE_STATE.MOUSE_STATE_DOWN; } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { PointNumber = 0; mouse_state = MOUSE_STATE.MOUSE_STATE_UP; } } public enum MOUSE_STATE { MOUSE_STATE_UP, MOUSE_STATE_DOWN }

    按照逻辑应该不会打印出“相等吧”,在什么条件下才促使if(prePoint==curPoint)成立的呢?

    在移动鼠标的过程中打印出“相等”的情况很少?哪位帮忙看看呀


    2014年3月20日 2:55

全部回复

  • 没有人知道么?
    2014年3月21日 3:06
  • 因为这代码在呼叫pictureBox1_MouseDown 之后必须要在移动后才会再次呼叫pictureBox1_MouseMove,如此一来位置一定不对,可以使用以下代码观察:

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        textBox1.Text = PointNumber.ToString();
        if (mouse_state == MOUSE_STATE.MOUSE_STATE_DOWN)
        {
            curPoint = Control.MousePosition;
            textBox3.Text = "X: " + curPoint.X + "; Y: " + curPoint.Y;
            textBox2.Text = "X: " + prePoint.X + "; Y: " + prePoint.Y; 
            if (prePoint == curPoint)
            {
                MessageBox.Show("相等");
            }
            PointNumber++;
            if (2 == PointNumber)
            {
                prePoint = curPoint;
            }
            else if (3 == PointNumber)
            {
                prePoint = curPoint;
            }
            else if (4 == PointNumber)
            {
                prePoint = curPoint;
                PointNumber = 2;
            }
        }
    }

    这样你就会知道为什么会不对了。

    如果你需要pictureBox1_MouseDown 后位置相同,我建议直接将按钮事件带入pictureBox1_MouseMove。

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        PointNumber = 1;
        prePoint = Control.MousePosition;
        mouse_state = MOUSE_STATE.MOUSE_STATE_DOWN;
        pictureBox1_MouseMove(sender, e);
    }

    2014年3月21日 4:07
  • 我还是不懂为什么会相等,我就是想只在鼠标移动的时候才进行一些运算啊,如果我鼠标不移动,就不会进行MouseMove里面的代码呀,一旦鼠标移动curPoint就被赋予了另一个Control.MousePosition值,就不可能和之前的MousePosition相等了呀
    2014年3月24日 1:49