【发布时间】:2020-03-20 20:35:57
【问题描述】:
我已经制作了一个绘图克隆,并且想要在您摇动表单时清除边框(因此通过在顶部栏上按住 mouse1 并快速左右移动它)。 我试图比较 x 轴上的两个点,这些点是根据鼠标位置设置的,并针对阈值进行测试。而且我添加了一个组合,因此您需要多次克服阈值以避免意外激活,为此还有一个计时器可以重置组合。 问题在于它将快速移动到一侧视为晃动我仍然希望用户能够在不清除画布的情况下移动表单。
int firstpos = 0;
int secondpos = 0;
bool isfirst = true;
int combo = 0;
int threshold = 50;
Point lastPoint;
private void TopBar_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//to move the form
{
this.Left += e.X - lastPoint.X;
this.Top += e.Y - lastPoint.Y;
}
if (isfirst)
{
firstpos = e.X;
isfirst = false;
}
else
{
secondpos = e.X;
int diff = secondpos - firstpos;
if (diff < 0) diff *= -1;//make positive
if (diff >= threshold)
{
combo++;
Thread t = new Thread(startdecay);
t.Start();
}
if (combo == 4)
{
canvas.Invalidate();//clear the canvas
combo = 0;
}
isfirst = true;
}
}
void startdecay()
{
Thread.Sleep(1000);
combo = 0;
}
private void TopBar_MouseDown(object sender, MouseEventArgs e)
{
lastPoint = new Point(e.X, e.Y);
}
【问题讨论】:
-
与其在
else中将isfirst设置为true,不如考虑将firstpos更改为secondpos。 -
另外,跟踪
diff符号并计算它何时发生变化。