我有一个面板,可以通过click向左或向右移动(根据当前位置自动选择,距离是静态的)。此外,用户可以通过单击面板、按住按钮并移动鼠标来垂直拖动面板。问题是,当面板在垂直移动后放下时,面板也会向左/向右移动,因此用户必须在之后再次单击它才能到达正确的一侧(左/右)。以下是我正在使用的方法:将事件处理程序添加到面板(此处称为 Strip)
Strip.MouseDown += new MouseEventHandler(button_MouseDown);
Strip.MouseMove += new MouseEventHandler(button_MouseMove);
Strip.MouseUp += new MouseEventHandler(button_MouseUp);
Strip.Click += new EventHandler(strip_Click);
这里是上面提到的所有方法:
void button_MouseDown(object sender, MouseEventArgs e)
{
activeControl = sender as Control;
previousLocation = e.Location;
Cursor = Cursors.Hand;
}
void button_MouseMove(object sender, MouseEventArgs e)
{
if (activeControl == null || activeControl != sender)
return;
var location = activeControl.Location;
location.Offset(0, e.Location.Y - previousLocation.Y);
activeControl.Location = location;
}
void button_MouseUp(object sender, MouseEventArgs e)
{
activeControl = null;
Cursor = Cursors.Default;
}
void strip_Click(object sender, EventArgs e) // The one moving strip to left or right
{
activeControl = sender as Control;
if (activeControl.Left != 30)
activeControl.Left = 30;
else
activeControl.Left = 5;
}
如何使面板在垂直移动时不向左或向右移动?