Mouse click and drag Event WPF

馋奶兔 提交于 2019-12-12 09:33:21

问题


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;
 }

回答1:


I think this is the easiest and most straightforward way :

 private void Window_MouseMove(object sender, MouseEventArgs e) {
     if (e.LeftButton == MouseButtonState.Pressed) {
        this.DragMove();
     }
 }



回答2:


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
    }
 }    



回答3:


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



来源:https://stackoverflow.com/questions/17441672/mouse-click-and-drag-event-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!