Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to let user draw a line like we draw in a paint application following is my code


xaml code for MainWindow.xaml

XML
<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>




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


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
Posted

Why do you register to MouseDown and MouseMove events of the Button? It looks like you have to register to the events of the Canvas...

 
Share this answer
 
ok agreed so now changed to

XML
<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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900