Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 即使光标位于画布之外,也会调用MouseMove事件`画布周围的边框不起作用_C#_Wpf_Mousemove - Fatal编程技术网

C# 即使光标位于画布之外,也会调用MouseMove事件`画布周围的边框不起作用

C# 即使光标位于画布之外,也会调用MouseMove事件`画布周围的边框不起作用,c#,wpf,mousemove,C#,Wpf,Mousemove,对未来情况的简短描述: 当鼠标在画布上移动时,我希望画布上显示一个椭圆。椭圆应该一直保持鼠标移动,直到用户点击鼠标左键。当用户点击鼠标左键时,程序会将椭圆放置在画布上 因此,我使用了以下xaml代码: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas

对未来情况的简短描述:

当鼠标在画布上移动时,我希望画布上显示一个椭圆。椭圆应该一直保持鼠标移动,直到用户点击鼠标左键。当用户点击鼠标左键时,程序会将椭圆放置在画布上

因此,我使用了以下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#代码:

使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Shapes;
命名空间WpfApplication1
{
公共类项目
{
私有只读椭圆形状;
公共物品(帆布)
{
形状=新椭圆{宽度=50,高度=50,填充=画笔.黑色};
canvas.Children.Add(shape);
设定位置(0.0,0.0);
}
公共无效设置位置(双x,双y)
{
Canvas.SetLeft(形状,x);
画布。机顶盒(形状,y);
}
}
公共部分类主窗口:窗口
{
私有只读IList形状;
私有项目当前移动形状;
公共主窗口()
{
初始化组件();
形状=新列表();
InitMovingShape();
}
私有void InitMovingShape()
{
currentMovingShape=新项目(画布);
}
专用无效设置移动形状位置(MouseEventArgs e)
{
var pos=e.GetPosition(画布);
当前移动形状设置位置(位置X-25,位置Y-25);
}
私有void canvass_MouseMove(对象发送者,MouseEventArgs e)
{
var pos=e.GetPosition(画布);
当前移动形状设置位置(位置X-25,位置Y-25);
}
私有void canvass_MouseDown(对象发送器,MouseButtonEventArgs e)
{
形状。添加(当前移动形状);
InitMovingShape();
}
}
}
问题是,一旦鼠标离开画布,椭圆仍然会粘在鼠标上,甚至可以将椭圆放置在画布之外

首先,我在xaml代码的画布上使用了没有
边框的代码(代码来自stijn:)。然后我读到,在画布()周围放置一个
边框可能会有所帮助,并将其实现为xaml代码,如您所见。不幸的是,这没有帮助


有人能帮我解决吗?一个代码解决方案将非常受欢迎,因为我是c#的初学者。

看起来您希望在自己的控制下实现。请务必阅读它们,因为它们将在wpf中方便地执行类似的操作

但是,要解决您的问题。您应该在代码中实现
MouseEnter
MouseLeave
事件。在
MouseEnter
上添加形状,并在
MouseLeave
MouseDown
上删除形状。因此,当鼠标重新进入画布时,MouseEnter逻辑将启动并添加形状,而
MouseMove
逻辑将使用鼠标移动形状。希望我在这里说清楚。如果您仍在代码中挣扎,请告诉我

编辑

xaml


代码

公共部分类主窗口:窗口
{
私有只读IList形状;
私有椭圆运动形状;
公共主窗口()
{
初始化组件();
形状=新列表();
}
私人无效拉票(对象发送者,MouseEventArgs e)
{
AddEllipse();
}
私有无效AddEllipse()
{
currentMovingShape=新椭圆{宽度=50,高度=50,填充=Brusks.Black};
currentMovingShape.IshittesVisible=false;
canvass.Children.Add(currentMovingShape);
SetLeft(currentMovingShape,Mouse.GetPosition(canvass.X-25);
SetTop(currentMovingShape,Mouse.GetPosition(canvass.Y-25);
}
私人void canvass_MouseLeave(对象发送者,MouseEventArgs e)
{
if(currentMovingShape!=null)
{
画布。儿童。移除(当前移动形状);
currentMovingShape=null;
}
}
私有void canvass_MouseMove(对象发送者,MouseEventArgs e)
{
SetLeft(currentMovingShape,e.GetPosition(canvass).X-25);
Canvas.SetTop(currentMovingShape,e.GetPosition(canvass.Y-25);
}
私有void canvass_MouseDown(对象发送器,MouseButtonEventArgs e)
{
if(currentMovingShape!=null)
{
currentMovingShape.IshittesVisible=true;
形状。添加(当前移动形状);
AddEllipse();
}
}
}

请看,共识是“不,他们不应该”!嘿@Krishna,我必须承认,我不能实现你的建议,因为我不完全理解我使用的代码。但我试图以不同的方式实施你的建议。我使用
MouseEnter
将标志设置为1,并使用
MouseLeave
将标志设置为0。调用
MouseMove
MouseDown
事件时,会在启动其例程之前检查标志是否已设置。不幸的是,它几乎没有改变任何程序的结果。如果你能再帮我一点,我将不胜感激。@好的,给我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();            
            }


         }
    }
<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();
            }

        }
    }