鼠标单击并拖动事件WPF

编程入门 行业动态 更新时间:2024-10-27 16:35:30
本文介绍了鼠标单击并拖动事件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

本文发布于:2023-10-08 04:20:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   拖动   单击   事件   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!