日期:2014-05-17  浏览次数:20729 次

C#按钮问题
        private void b_MouseEnter(object sender, EventArgs e)
        {

            x = this.Location.X;
            y = this.Location.Y;
            this.Location = new Point(x+2,y+2);
            this.Size = new Size(108,108);
            this.BackgroundImage = Im2;
        }
        private void b_MouseLeave(object sender, EventArgs e)
        {

            x = this.Location.X;
            y = this.Location.Y;
            this.Location = new Point(x - 2, y - 2);
            this.Size = new Size(120, 120);
            this.BackgroundImage = Im1;
        }
当鼠标在某个位置时,恰好触发mouseenter事件,此时按钮变小,因此而触发mouseleave事件,此时按钮又变大,因此触发mouseenter事件,然后就无限循环了,请问这个问题该怎么解决

------解决方案--------------------
增加一个bool变量,表示当前状态,然后进入和离开的时候设置下,确保只在切换的时候调用1次,以后忽略。
------解决方案--------------------
无限循环?会无限循环?
------解决方案--------------------
如1L所说,加个bool变量判断
或者加个datetime,判断两次时间间隔是不是大于X秒Y毫秒,条件成立再触发事件
------解决方案--------------------
是,加个状态变量,判断在某个状态下,做对应的操作
------解决方案--------------------
按钮大小改变后。如果鼠标的位置在变化范围内。记下鼠标和边框的相对位置。保持相对位置不变。就可以了。