鼠标点击并拖动事件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;
}


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

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

does the trick

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

07-31 14:19