<Window x:Class="MyXamlPad.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Fun with Panels!"
Height="338" Width="400"
WindowStartupLocation="CenterScreen"
>
<DockPanel LastChildFill="True">
<ToolBar DockPanel.Dock="Top" Name="mainToolBar" Height="50">
<RadioButton Name="circleOption" GroupName="shapeSelection" Click="circleOption_Click">
<Ellipse Fill="Green" Height="35" Width="35" />
</RadioButton>
<RadioButton Name="rectOption" GroupName="shapeSelection" Click="rectOption_Click">
<Rectangle Fill="Red" Height="35" Width="35" RadiusX="10" RadiusY="10"/>
</RadioButton>
<RadioButton Name="lineOption" GroupName="shapeSelection" Click="lineOption_Click">
<Line Fill="Red" Height="35" Width="35" Stroke="Blue" StrokeThickness="10"
X1="10" Y1="10" Y2="25" X2="25" StrokeStartLineCap="Triangle" StrokeEndLineCap="Round"/>
</RadioButton>
</ToolBar>
<Canvas Background="LightBlue" Name="canvasDrawingArea"
MouseRightButtonDown="canvasDrawingArea_MouseRightButtonDown" MouseLeftButtonDown="canvasDrawingArea_MouseLeftButtonDown"/>
</DockPanel>
</Window>
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 Microsoft.Win32;
namespace MyXamlPad
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private enum SelectedShape
{
Circle,
Rectangle,
Line
}
private SelectedShape currentShape;
private void circleOption_Click(object sender, RoutedEventArgs e)
{
currentShape = SelectedShape.Circle;
}
private void rectOption_Click(object sender, RoutedEventArgs e)
{
currentShape = SelectedShape.Rectangle;
}
private void lineOption_Click(object sender, RoutedEventArgs e)
{
currentShape = SelectedShape.Line;
}
private void canvasDrawingArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Shape shapeToRender = null;
switch (currentShape)
{
case SelectedShape.Circle:
shapeToRender = new Ellipse() { Fill=Brushes.Green,Height=35,Width=35};
break;
case SelectedShape.Rectangle:
shapeToRender = new Rectangle() { Fill=Brushes.Red,Height=35,Width=35,RadiusX=10,RadiusY=10};
break;
case SelectedShape.Line:
shapeToRender = new Line() { Stroke=Brushes.Blue,StrokeThickness=10,X1=0,X2=50,Y1=0,Y2=50,
StrokeStartLineCap=PenLineCap.Triangle,StrokeEndLineCap=PenLineCap.Round};
break;
default:
return;
}
Canvas.SetLeft(shapeToRender,e.GetPosition(canvasDrawingArea).X);
Canvas.SetTop(shapeToRender,e.GetPosition(canvasDrawingArea).Y);
canvasDrawingArea.Children.Add(shapeToRender);
}
private void canvasDrawingArea_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
Point pt = e.GetPosition((Canvas)sender);
HitTestResult result = VisualTreeHelper.HitTest(canvasDrawingArea, pt);
if (result!=null)
{
canvasDrawingArea.Children.Remove(result.VisualHit as Shape);
}
}
}
}