本文介绍了在按钮上画一条线单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户画一条线,就像我们在绘画应用程序中画线一样,这是我的代码


MainWindow.xaml的xaml代码

I want to let user draw a line like we draw in a paint application following is my code


xaml code for MainWindow.xaml

<Window x:Class="LineWPF.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">
    <DockPanel LastChildFill="True">
        <Button Name="buttonToolLine" DockPanel.Dock="Top" Height="41" MouseDown="buttonToolLine_MouseDown" MouseMove="buttonToolLine_MouseMove">Line</Button>
            <Canvas Name="DrawingCanvas"></Canvas>
    </DockPanel>

</Window>





以下是我背后的代码MainWindow.xaml.cs





Following is my behind the code MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace LineWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        Point StartPoint;
        Point EndPoint;
        Line CurrentLine;
        public MainWindow()
        {
            InitializeComponent();
        }

         void buttonToolLine_MouseDown(object sender, MouseButtonEventArgs e)
        {
            StartPoint = e.GetPosition(DrawingCanvas);
        }

         void buttonToolLine_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                EndPoint = e.GetPosition(DrawingCanvas);
                DrawLine(StartPoint, EndPoint, DrawingCanvas);
            }
        }
         void DrawLine(Point From, Point To, Canvas TargetCanvas)
        {

            CurrentLine = new Line();
            CurrentLine.StrokeEndLineCap = PenLineCap.Round;
            CurrentLine.StrokeStartLineCap = PenLineCap.Round;
            CurrentLine.Stroke = Brushes.Red;
            CurrentLine.StrokeThickness = 2.0;
            CurrentLine.X1 = From.X;
            CurrentLine.Y1 = From.Y;
            CurrentLine.X2 = To.X;
            CurrentLine.Y2 = To.Y;
            Canvas.SetLeft(TargetCanvas, From.X);
            Canvas.SetTop(TargetCanvas, From.Y);
            TargetCanvas.Children.Add(CurrentLine);
        }

    }
}



我在这里错过了什么,当我调试时也没有击中mousedown事件,仍然是编码中的新功能,尤其是WPF和图形中的新功能,请帮助



What did I missed here, The mousedown event is also not hit when I debug, Still new in coding and specially in WPF and graphics please help

推荐答案


<DockPanel LastChildFill="True">
        <Button Name="buttonToolLine" DockPanel.Dock="Top" Height="41" >Line</Button>
            <Canvas Name="DrawingCanvas" MouseDown="DrawingCanvas_MouseDown" MouseLeave="DrawingCanvas_MouseLeave"></Canvas>
    </DockPanel>


并且即使没有命中,代码背后的代码也发生了变化


and the code behind code also changed still the even is not hit


这篇关于在按钮上画一条线单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:32
查看更多