如何在基于WPF的应用程序中限制光标移动? [英] How to limit cursor movement in WPF based app?

查看:142
本文介绍了如何在基于WPF的应用程序中限制光标移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF应用程序,更具体地说是具有可拖动元素的Canvas.

I'm working with a WPF app, more specifically a Canvas with draggable elements.

一旦拖动了一个项目,我想将光标移动范围限制在拖动项目所在的画布内.

Once an item is being dragged, I'd like to limit the scope of cursor movement to inside the canvas where the items are being dragged about.

可以开始拖动的事件如下所示

The event which can start a drag is shown below


private void WidgetCanvas_PreviewHeaderLeftMouseDown(object sender, MouseButtonEventArgs e)
{
     e.Handled = true;
     ... logic to figure out if this is a valid drag, blah blah blah ...

     this.IsDragging = true;
     // TODO: clip the available cursor movement to actual width of Canvas
}

在Preview-MouseUp上,我想简单地将光标移动解开"回到正常状态.

On the Preview-MouseUp, I'd like to simply "unclip" the cursor movement back to it's normal state.

一旦开始拖动(PreviewMouseMove),我将监视鼠标的移动,因此,在最坏的情况下,我可以手动检查鼠标的位置并将其限制在画布上,但这看起来有点难看.

I'll be monitoring the movement of the mouse once I start dragging (PreviewMouseMove), so worst case, I could manually check the position of the mouse and keep it constrained to the canvas, but that seems a little ugly.

有人有更好的方法来限制光标边界吗?

Anyone have a better way to limit the cursor boundaries?

推荐答案

.

There's no clean way to do this and the not-so-clean ways will make your mouse cursor "jitter" at the border of the clipping area.

此外,我想问这是否真的是个好主意.用户应该真正拥有鼠标,当您试图人为限制他或她拥有的东西时,他或她通常会感到沮丧.

Moreover, I'd question if this is really a good idea. The user should really own the mouse and he or she generally gets frustrated when you try to artificially limit things that he or she owns.

如果您想在鼠标离开画布时提供反馈,也许您可​​以在鼠标按钮仍处于按下状态时将被拖动的项目沿边框卡住?这将告诉用户他或她已经离开了目标区域,而没有试图限制鼠标的移动位置.

If you want to provide feedback when the mouse leaves your canvas, maybe you could leave the item being dragged stuck along the border while the mouse button is still down? This would tell the user that he or she has left the target area without trying to impose limitations on where the mouse can go.

祝你好运!

这篇关于如何在基于WPF的应用程序中限制光标移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆