我正在开发模拟时钟选择器控件。 用户可以单击分针或时针并拖动以转动针以选择特定时间。我想知道如何检测这样的点击和拖动事件。
我尝试使用MouseLeftButtonDown + MouseMove,但我无法让它工作,因为尽管我使用了标志,但鼠标移动时始终会触发MouseMove。有没有更简单的方法?
public bool dragAction = false;
private void minuteHand_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
dragAction = true;
minuteHand_MouseMove(this.minuteHand, e);
}
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (dragAction == true)
{
//my code: moving the needle
}
}
private void minuteHand_MouseLeftButtonUp(object sender, MouseEventArgs e)
{
dragAction = false;
}
答案 0 :(得分:6)
我认为这是最简单,最直接的方式:
private void Window_MouseMove(object sender, MouseEventArgs e) {
if (e.LeftButton == MouseButtonState.Pressed) {
this.DragMove();
}
}
答案 1 :(得分:4)
您可以使事情变得更轻松,无需处理鼠标按下/向上:
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
//my code: moving the needle
}
}
答案 2 :(得分:4)
public bool dragAction = false;
private void minuteHand_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
dragAction = true;
minuteHand_MouseMove(this.minuteHand, e);
}
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (dragAction == true)
{
this.DragMove();
}
}
private void minuteHand_MouseLeftButtonUp(object sender, MouseEventArgs e)
{
dragAction = false;
}
诀窍