将鼠标环绕屏幕边缘
问题描述
我有一个 Delphi 组件。要设置值,您可以单击并拖动。
但是,当您到达屏幕边缘时,您将无法再继续前进。然后需要返回到组件并进一步拖动,这不太人性化。
首选解决方案
我想要的是,如果到达边缘,让鼠标光标环绕屏幕,这样您就可以继续滚动值。 3dsmax 广泛使用这种类型的 GUI 控件,我喜欢它的工作方式。
或者,如果光标移出屏幕,但继续发送超出屏幕范围的 X/Y 坐标,这对我来说也很好。
到目前为止我所拥有的
我知道我可以通过 Mouse.CursorPos 获取/设置当前鼠标位置,并且可以通过 Screen.Width 和 Screen.Height 获取屏幕尺寸。
下面的代码确实按照我想要的方式包裹鼠标光标。
procedure TFormXXXX.YYYYMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
LX, LY: Integer;
begin
LX := Mouse.CursorPos.X;
if LX < 1 then
LX := Screen.Width - 1
else
if LX>Screen.Width -2 then
LX := 0;
LY := Mouse.CursorPos.Y;
if LY < 1 then
LY := Screen.Height - 1
else
if LY>Screen.Height -2 then
LY := 0;
Mouse.CursorPos := Point(LX, LY);
end;
仍然存在一个问题,我必须“手动”跟踪环绕以获得距起点的适当偏移,但我会找到一种方法来解决这个问题。
我只是不知道这是否是执行此操作的正确方法。也许有人对此有一些经验或明智的说法...
主要问题
是否有经过尝试和测试的通用方法? windows 是否提供了做类似事情的东西?
我有一些疑问
- 当有多个显示器时,这会如何表现?
- 如果用户通过慢速(VNC?)连接进行连接,会发生什么情况。光标位置是否始终到达 0 或屏幕的另一端?
- 如果输入控件不是鼠标,而是画板或触摸屏,会发生什么情况?
- 更改鼠标位置是不好的做法吗?我可以想象用户不喜欢我的应用程序弄乱他们的鼠标光标位置。
problem description
I've got a Delphi component. To set a value, you can click and drag.
However, when you reach the edge of the screen, you can't go any further. You then need to go back to the component and drag further, which is not very user-friendly.
preferred solution
What I'd like is to have the mouse cursor wrap around the screen if you reach an edge, so you can continue scrolling a value. 3dsmax uses this type of GUI control extensively, and I like how that works.
Alternatively, it would be fine for me if the cursor goes off-screen, but continues to send X/Y coordinates that are out of the screen bounds.
what i have so far
I know that I can get/set the current mouse position via Mouse.CursorPos, and that the screen dimensions are available via Screen.Width and Screen.Height.
The code below does wrap the mousecursor around the way I want to.
procedure TFormXXXX.YYYYMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
LX, LY: Integer;
begin
LX := Mouse.CursorPos.X;
if LX < 1 then
LX := Screen.Width - 1
else
if LX>Screen.Width -2 then
LX := 0;
LY := Mouse.CursorPos.Y;
if LY < 1 then
LY := Screen.Height - 1
else
if LY>Screen.Height -2 then
LY := 0;
Mouse.CursorPos := Point(LX, LY);
end;
There's still the problem that I have to "manually" keep track of the wraps to obtain a proper offset from the starting point, but I'll find a way to solve that.
I just don't know if this is a proper approach to do this. Maybe somebody has some experience or wise words to say about this...
main question
Is there a tried and tested common approach to this?
Does windows provide stuff to do something like this maybe?
some doubts that I have
- How will this behave when there are multiple monitors?
- What happens if the user is connected via a slow (VNC?) connection.. will the cursorposition always reach 0 or the other extreme end of the screen?
- What will happen if the input control is not a mouse, but a sketchpad or a touchscreen?
- Is it bad practice to change the mouse position? I can imagine users don't like my application to mess with their mouse cursor position.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过增加与控件的距离来加速变化,而不是使用简单的线性刻度,并在超过该值后开始自动递增。基本上它的工作原理就像拖动选择文本一样,一旦鼠标到达窗口底部,窗口就开始滚动,即使鼠标到达该点后就停止移动。
Rather than having a simple linear scale, you could accelerate the change with increasing distance from the control, and have a cutoff where past that it starts incrementing automatically. Basically have it work like dragging to select text does, where the window starts scrolling once the mouse reaches the bottom of a window, even if the mouse stops moving once it reaches that point.