15

我正在开发一个模拟时钟选择器控件。用户可以单击分针或时针并拖动以转动指针以选择特定时间。我想知道如何检测这样的点击和拖动事件。

我尝试使用 MouseLeftButtonDown + MouseMove 但我无法让它工作,因为尽管我使用了标志,但当 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;
 }
4

3 回答 3

8

我认为这是最简单和最直接的方法:

 private void Window_MouseMove(object sender, MouseEventArgs e) {
     if (e.LeftButton == MouseButtonState.Pressed) {
        this.DragMove();
     }
 }
于 2015-06-10T04:36:02.570 回答
5
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;
 }

成功了

于 2013-12-05T13:58:39.250 回答
4

您可以使事情变得更容易,并且不需要处理鼠标向下/向上:

private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
    if (Mouse.LeftButton == MouseButtonState.Pressed)
    {
        //my code: moving the needle
    }
 }    
于 2013-07-03T08:15:47.860 回答