You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.6 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace WPFCanvas
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private enum DrawMode
{
Line,
Circle,
Square,
Eraser,
};
private bool _mouseClicked;
private Point _prePosition;
private Brush _myBrush;
private double _thickness;
private Rectangle _tmpRect;
private DrawMode MODE;
public MainWindow()
{
InitializeComponent();
}
private void btnLine_Click(object sender, RoutedEventArgs e)
{
MODE = DrawMode.Line;
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_mouseClicked = true;
_prePosition = e.GetPosition(canvas);
}
private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_mouseClicked = false;
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
Point nowPosition = e.GetPosition(canvas);
if (!_mouseClicked)
return;
switch (MODE)
{
case DrawMode.Line:
Line line = new Line()
{
X1 = _prePosition.X,
Y1 = _prePosition.Y,
X2 = nowPosition.X,
Y2 = nowPosition.Y,
Stroke = _myBrush == null ? Brushes.Black : _myBrush,
StrokeThickness = _thickness <= 0 ? 1 : _thickness
};
canvas.Children.Add(line);
_prePosition = nowPosition;
break;
case DrawMode.Square:
if (e.MouseDevice.LeftButton != MouseButtonState.Pressed)
_tmpRect = null;
else
{
Rectangle rect = new Rectangle();
rect.Stroke = Brushes.Gray;
rect.Fill = _myBrush == null ? Brushes.Black : _myBrush;
rect.Opacity = 0.8;
double left = _prePosition.X;
double top = _prePosition.Y;
double width = nowPosition.X - _prePosition.X;
double height = nowPosition.Y - _prePosition.Y;
if (nowPosition.X < _prePosition.X)
{
left = nowPosition.X;
width *= -1;
}
if (nowPosition.Y < _prePosition.Y)
{
top = nowPosition.Y;
height *= -1;
}
rect.Margin = new Thickness(left, top, 0, 0);
rect.Width = width;
rect.Height = height;
canvas.Children.Remove(_tmpRect);
_tmpRect = rect;
canvas.Children.Add(rect);
}
break;
}
}
private void btnBlack_Click(object sender, RoutedEventArgs e)
{
_myBrush = Brushes.Black;
}
private void btnRed_Click(object sender, RoutedEventArgs e)
{
_myBrush = Brushes.Red;
}
private void btnBlue_Click(object sender, RoutedEventArgs e)
{
_myBrush = Brushes.Blue;
}
private void btnGreen_Click(object sender, RoutedEventArgs e)
{
_myBrush = Brushes.Green;
}
private void btnYellow_Click(object sender, RoutedEventArgs e)
{
_myBrush = Brushes.Yellow;
}
private void cboThickness_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_thickness = double.Parse(cboThickness.SelectedValue.ToString());
}
private void btnSquare_Click(object sender, RoutedEventArgs e)
{
MODE = DrawMode.Square;
}
}
}