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



       
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF(Windows Presentation Foundation)是一种用于创建交互式用户界面的技术,可以实现各种各样的应用程序。在WPF中,我们可以使用绘图工具来创建一个简单画图组态。 首先,我们需要在WPF应用程序中创建一个画布(Canvas)元素作为画图区域。通过设置画布的宽度、高度和背景颜色等属性,可以调整画图区域的大小和外观。 接下来,我们可以使用WPF提供的各种绘图工具来绘制不同的图形。例如,可以使用直线(Line)、矩形(Rectangle)、椭圆(Ellipse)等工具来绘制基本的形状。通过设置这些图形的颜色、线条宽度和位置等属性,可以调整它们的外观和位置。 除了基本的形状,WPF还提供了更高级的绘图工具以及一些效果和转换等功能,可以实现更复杂的图形效果。例如,我们可以使用BezierSegment工具来绘制曲线,使用Path工具来创建复杂的路径,还可以使用动画和渐变等特效来增强图形的可视效果。 在画图组态中,我们还可以添加交互功能,例如鼠标点击或拖拽图形等操作。通过WPF的事件处理机制,可以捕捉用户的输入并根据输入来更新图形的状态或位置。 总结来说,通过WPF绘图工具和各种功能,我们可以很方便地创建一个简单画图组态。我们可以绘制各种基本的形状,添加高级的绘图工具和效果,以及实现交互功能,从而满足不同应用场景的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值