- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
future 情况的简短描述:
只要鼠标移到我的 Canvas 上,我就想在 Canvas 上显示一个椭圆。在用户点击鼠标左键之前,椭圆应该会一直跟随鼠标移动。当用户点击鼠标左键时,程序应将椭圆放置在 Canvas 上。
因此我使用了以下 xaml 代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="700" Width="1200">
<Grid>
<Border ClipToBounds="true">
<Canvas x:Name="canvasss" Background="AntiqueWhite" Width="524" Height="368" MouseMove="Canvasss_MouseMove" MouseDown="Canvasss_MouseDown">
</Canvas>
</Border>
</Grid>
C# 代码:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace WpfApplication1
{
public class Item
{
private readonly Ellipse shape;
public Item(Canvas canvas)
{
shape = new Ellipse { Width = 50, Height = 50, Fill = Brushes.Black };
canvas.Children.Add(shape);
SetPosition(0.0, 0.0);
}
public void SetPosition(double x, double y)
{
Canvas.SetLeft(shape, x);
Canvas.SetTop(shape, y);
}
}
public partial class MainWindow : Window
{
private readonly IList<Item> shapes;
private Item currentMovingShape;
public MainWindow()
{
InitializeComponent();
shapes = new List<Item>();
InitMovingShape();
}
private void InitMovingShape()
{
currentMovingShape = new Item(canvasss);
}
private void SetMovingShapePosition(MouseEventArgs e)
{
var pos = e.GetPosition(canvasss);
currentMovingShape.SetPosition(pos.X - 25, pos.Y - 25);
}
private void Canvasss_MouseMove(object sender, MouseEventArgs e)
{
var pos = e.GetPosition(canvasss);
currentMovingShape.SetPosition(pos.X - 25, pos.Y - 25);
}
private void Canvasss_MouseDown(object sender, MouseButtonEventArgs e)
{
shapes.Add(currentMovingShape);
InitMovingShape();
}
}
}
问题是,一旦鼠标离开 Canvas ,椭圆仍然粘在鼠标上,甚至可以将椭圆放在 Canvas 之外。
首先,我使用代码(代码来自 stijn:C# - WPF - Mousemove event on canvas will overload the mouse events so click event is not fired),在 xaml 代码中 Canvas 周围没有 Border
。然后我读到它可能有助于在 Canvas 周围放置一个 Border
( MouseMove event is called even when cursor is outside of canvas ) 并将其实现到 xaml 代码中,如您所见。不幸的是,它没有帮助。
有人可以帮我解决吗?代码解决方案将不胜感激,因为我是 c# 的初学者。
最佳答案
看起来你想实现 Adorners在你的控制之下。请务必阅读它们,因为它们会派上用场,以便在 wpf 中轻松执行此类操作。
但是,要解决您的问题。您应该在代码中实现 MouseEnter
和 MouseLeave
事件。在 MouseEnter
上添加形状,在 MouseLeave
或 MouseDown
上移除形状。因此,当鼠标重新进入 Canvas 时,MouseEnter 逻辑将启动并添加形状,而您的 MouseMove
逻辑将使用鼠标移动形状。希望我在这里说清楚。如果您仍在为代码苦苦挣扎,请告诉我。
编辑
xaml
<Grid Background="Transparent">
<Canvas x:Name="canvasss" Background="AntiqueWhite" Width="300" Height="300" MouseEnter="canvasss_MouseEnter" MouseLeave="canvasss_MouseLeave" MouseMove="Canvasss_MouseMove" MouseDown="Canvasss_MouseDown" Margin="50" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
代码
public partial class MainWindow : Window
{
private readonly IList<Ellipse> shapes;
private Ellipse currentMovingShape;
public MainWindow()
{
InitializeComponent();
shapes = new List<Ellipse>();
}
private void canvasss_MouseEnter(object sender, MouseEventArgs e)
{
AddEllipse();
}
private void AddEllipse()
{
currentMovingShape = new Ellipse { Width = 50, Height = 50, Fill = Brushes.Black };
currentMovingShape.IsHitTestVisible = false;
canvasss.Children.Add(currentMovingShape);
Canvas.SetLeft(currentMovingShape, Mouse.GetPosition(canvasss).X - 25);
Canvas.SetTop(currentMovingShape, Mouse.GetPosition(canvasss).Y - 25);
}
private void canvasss_MouseLeave(object sender, MouseEventArgs e)
{
if (currentMovingShape != null)
{
canvasss.Children.Remove(currentMovingShape);
currentMovingShape = null;
}
}
private void Canvasss_MouseMove(object sender, MouseEventArgs e)
{
Canvas.SetLeft(currentMovingShape, e.GetPosition(canvasss).X - 25);
Canvas.SetTop(currentMovingShape, e.GetPosition(canvasss).Y - 25);
}
private void Canvasss_MouseDown(object sender, MouseButtonEventArgs e)
{
if (currentMovingShape != null)
{
currentMovingShape.IsHitTestVisible = true;
shapes.Add(currentMovingShape);
AddEllipse();
}
}
}
关于c# - 即使光标在 Canvas 之外,也会调用 MouseMove 事件。 `Border` Canvas 周围没有工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30705236/
如果我有一个密集的 mousemove 处理程序 window.moveEventCounter = 0; window.addEventHandler("mousemove", function(e
我创建了一个 jquery 小部件,它允许我将其附加到 Canvas 并记录用户创建的绘图。 我在使用 Firefox 时遇到问题,jQuery 触发的事件不起作用;但 native JavaScri
$("#father").mousemove(function(){ alert("out"); }) 如何只在父级上绑定(bind) mousemove 事件,而不继承子
我遇到了一些奇怪的事情,但我不确定它是否是故意的或者是什么原因造成的。 我正在试验一些 JavaScript,当用户在屏幕上移动鼠标时循环一组图像。我正在使用 jQuery mousemove为了这。
我想是新手问题。 以下代码是我在文档就绪时调用的函数的一部分。它旨在在鼠标移动时永久返回当前鼠标位置的值。 正在发生的奇怪事情:在文档就绪时移动鼠标不会将任何内容记录到控制台。我知道 mouse_mo
我创建了一个 WPF 应用程序,其中关闭了标题栏和镶边。我在整个应用程序周围有一个边框,认为它在某些方面会像 chrome 一样。我尝试做的第一件事是让 mousemove 事件在单击鼠标时捕获鼠标的
想知道是否有适用于移动设备的 mousemove 的等效项,例如 touchmove 或 touchdrag 等。 我在容器中有一个图像,可以使用鼠标移动在悬停时平移该图像。有没有办法也可以针对移动设
如何在 JQuery 中创建鼠标按下并移动时触发的事件?并且每次 mousedown + mousemove 只触发一次? 最佳答案 更新: 因此,看起来如果您的鼠标不再位于绑定(bind) onmo
我已经实现了一个自定义 NSView(标记为 )并且能够获取 mouseUp:、mouseDown: 和 mouseMoved: 事件。对此,我已 AcceptFirstResponder 返回 YE
我通过以下方式使用事件监听器/处理程序: var _builder_canvas = document.getElementById("Builder"); mouseMove = f
我有一个子类NSView这是子类 NSDocument 的 .xib 文件的一部分,它通过 NSDocumentController 的默认行为变得活跃。的openDocument:方法。在此子类中
正如标题所说,我注意到在我的 Canvas 上,当按下/释放鼠标按钮时,即使我实际上没有移动鼠标,也会触发 mousemove 。问题是,在释放按钮的情况下,它会在 mouseup 之后被触发! 这是
u如何记录鼠标坐标的值? div = document.querySelector('.div'); div.addEventListener('mousemove', function yo(u)
Private Sub framePDF_MouseMove(ByVal... ) framePDF.BackColor = &H80000012& 所以,框架的颜色正在改变。 当光标远离框架时,我
我正在尝试制作一个特定的 仅当我的鼠标位于 范围内时,元素才保持可见元素并在 内移动元素。如果鼠标在 之外元素或在 内静止元素,那么它应该隐藏 元素。 我正在使用 mousemove事件监听器
我试图获取一个 mousemove 函数来显示当鼠标移动到特定 div 内时我创建的自定义光标元素。自定义光标是我希望它出现在的 div 中的绝对定位 div。我看到的奇怪的事情是我可以从开发人员工具
尝试用头顶摄像头制作一个简单的角色扮演游戏。当我去实现一个 MouseMotionListener 时(这不是我第一次使用 MouseMotionListener),似乎 mousedMoved Mo
我一直在尝试使用这段代码,就像当您将鼠标悬停在开始按钮上时,它应该将其颜色更改为灰色,但每当我将鼠标悬停在它上面时。什么也没发生,有人能告诉我为什么吗?我没有收到任何错误,编译器似乎无法识别我的鼠标移
我正在尝试从外部小程序获取鼠标移动事件,如下所示: Applet loader = (Applet) new URLClassLoader( new URL[] { new F
我正在用java创建一个游戏。在其中,您控制一个跟随鼠标的方 block 。我想对正方形实现碰撞检测,以便它在 JFrame 内稍微停止,而不是在边缘处。使用箭头键执行此操作非常容易,但我无法使用 m
我是一名优秀的程序员,十分优秀!