c# - WPF - 使用鼠标事件在 Canvas 上绘图

标签 c# wpf wpf-controls

我在处理 Canvas 上的鼠标事件时遇到问题。我想用鼠标在上面绘图,我想出了这些事件处理程序,但当我开始绘图时它们什么也没做。

    private void paintSurface_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ButtonState == MouseButtonState.Pressed)
            currentPoint = e.GetPosition(this);
    }

    private void paintSurface_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Line line = new Line();

            line.Stroke = SystemColors.WindowFrameBrush;
            line.X1 = currentPoint.X;
            line.Y1 = currentPoint.Y;
            line.X2 = e.GetPosition(this).X;
            line.Y2 = e.GetPosition(this).Y;

            currentPoint = e.GetPosition(this);

            paintSurface.Children.Add(line);
        }
    }

你能告诉我缺少什么或如何重写它以便它开始工作吗?

最佳答案

我敢打赌你的 Canvas 没有收到鼠标事件,因为它的背景属性设置为透明

这对我来说很好。

enter image description here

<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="350" Width="525">
    <Canvas  Name="paintSurface" MouseDown="Canvas_MouseDown_1" MouseMove="Canvas_MouseMove_1" >
        <Canvas.Background>
            <SolidColorBrush Color="White" Opacity="0"/>
        </Canvas.Background>
    </Canvas>
</Window>


using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {

        Point currentPoint = new Point();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Canvas_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
                currentPoint = e.GetPosition(this);
        }

        private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Line line = new Line();

                line.Stroke = SystemColors.WindowFrameBrush;
                line.X1 = currentPoint.X;
                line.Y1 = currentPoint.Y;
                line.X2 = e.GetPosition(this).X;
                line.Y2 = e.GetPosition(this).Y;

                currentPoint = e.GetPosition(this);

                paintSurface.Children.Add(line);
            }
        }

    }
}

关于c# - WPF - 使用鼠标事件在 Canvas 上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16037753/

相关文章:

c# - 如何使用 Windsor 将依赖项注入(inject)自定义 RoleProvider?

c# - 如何从 window.external.Notify ("someText")的事件处理程序(即 WebBrowser.ScriptNotify 事件)获取结果返回给 JS?

c# - 访问在 MainWindow() 构造函数期间实例化的类

c# - 如何使用 WPF 用户控件关闭父窗口

c# - Blazor 请求 json 文件

c# - 向 Outlook 2010 功能区添加选项卡?

c# - 标题中带有下拉菜单的自定义 WPF DataGrid

c# - WPF UserControl 无法在窗体外部绑定(bind)

wpf - 如何通过MVVM在WPF中的数据网格中一次选择2行

.net - 哪个 WPF 控制套件最适合 MVVM?