当光标在画布之外的MouseMove事件甚至被称为

当光标在画布之外的MouseMove事件甚至被称为

本文介绍了当光标在画布之外的MouseMove事件甚至被称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不`吨知道什么是错我的代码或WPF,但这里的问题是:我要创建一个小程序,在那里你可以在画布上光标借鉴。
我有一个简单的WPF窗口:

I don`t know if something is wrong with my code or WPF, but here is the problem: I want to create a small program, where you can draw with your cursor in the canvas.I have a simple WPF window:

<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="475" Width="544">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100px"></RowDefinition>
        <RowDefinition Height ="*"></RowDefinition>
        <RowDefinition Height="100px"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100px"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
        <ColumnDefinition Width="100px"></ColumnDefinition>
    </Grid.ColumnDefinitions>
        <Canvas Grid.Row="1" Grid.Column="1"  Name="imageCanvas" MouseEnter="StackPanel_MouseEnter" MouseLeave="StackPanel_MouseLeave" Background="LightGray"
                      MouseDown="StackPan_MouseDown" MouseUp="StackPan_MouseUp" MouseMove="StackPan_MouseMove">

        </Canvas>
</Grid>



下面是后台代码:

   private void StackPan_MouseDown(object sender, MouseButtonEventArgs e)
    {
        draw = true;
    }

    private void StackPan_MouseUp(object sender, MouseButtonEventArgs e)
    {
        draw = false;
    }

    private void StackPan_MouseMove(object sender, MouseEventArgs e)
    {
        if (draw)
        {
            var pos = Mouse.GetPosition(imageCanvas);
            Rectangle rec = new Rectangle()
                    {
                        Width = 10,
                        Height = 10,
                        Fill = Brushes.Red,
                    };
            Canvas.SetTop(rec, pos.Y);
            Canvas.SetLeft(rec, pos.X);
            imageCanvas.Children.Add(rec);
        }

    }



我可以做什么以下内容:

为什么当光标位于画布之外StackPan_MouseMove叫什么名字?

Why does StackPan_MouseMove called when the cursor is outside of canvas?

下面是可执行文件,在这里你可以看到这个behavore:

Here is the executable file, where you can notice this behavore:https://www.dropbox.com/s/ye1w2j9chld2oif/WpfApplication1.exe

推荐答案

尝试把画布边境控制和设置内在边框控件的的属性真正

Try placing the Canvas inside a Border control and setting the Border control's ClipToBounds property to true.

<Border ClipToBounds="true" Grid.Row="1" Grid.Column="1" >
    <Canvas Name="imageCanvas" MouseEnter="StackPanel_MouseEnter" MouseLeave="StackPanel_MouseLeave" Background="LightGray"
            MouseDown="StackPan_MouseDown" MouseUp="StackPan_MouseUp" MouseMove="StackPan_MouseMove">
    </Canvas>
</Border>

在一个稍微不同的说明,似乎一个的 /的可能是你正在尝试做的,除非你避免它们的具体原因是什么更合适的控制。

On a slightly different note, it seems that an InkPresenter/InkCanvas may be a more suitable control for what you're trying to do, unless you're avoiding them for specific reasons.

这篇关于当光标在画布之外的MouseMove事件甚至被称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:19