基于WPF的简单画图

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



       
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值