鼠标单击并拖动事件WPF

鼠标单击并拖动事件WPF

本文介绍了鼠标单击并拖动事件WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我尝试使用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)
{
//我的代码:移动针
}
}

private void minuteHand_MouseLeftButtonUp(object sender,MouseEventArgs e)
{
dragAction = false;
}


解决方案

不需要处理鼠标向下/向上:

  private void minuteHand_MouseMove(object sender,MouseEventArgs e)
{
if(Mouse.LeftButton == MouseButtonState.Pressed)
{
//我的代码:移动针
}
}


I am developing an analog clock picker control.The user is able to click on the minute or hour hand and drag to turn the needle to select the specific time. I was wondering how to detect such a click and drag event.

I tried using MouseLeftButtonDown + MouseMove but I cannot get it to work as MouseMove is always trigger when the mousemove happen despite me using a flag. Is there any easier way?

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;
 }
解决方案

You can make things easier and need not handle mouse down / up :

private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
    if (Mouse.LeftButton == MouseButtonState.Pressed)
    {
        //my code: moving the needle
    }
 }

这篇关于鼠标单击并拖动事件WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 11:45