gpt4 book ai didi

c# - 即使光标在 Canvas 之外,也会调用 MouseMove 事件。 `Border` Canvas 周围没有工作

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:35 25 4
gpt4 key购买 nike

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 中轻松执行此类操作。

但是,要解决您的问题。您应该在代码中实现 MouseEnterMouseLeave 事件。在 MouseEnter 上添加形状,在 MouseLeaveMouseDown 上移除形状。因此,当鼠标重新进入 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com