WPF learning 04: 2D drawing Shape, wpf042dshape

Source: Internet
Author: User

WPF learning 04: 2D drawing Shape, wpf042dshape
Example

A removable white box:

<Window x: Class = "Shape. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" Background = "# 019aff"
Title = "Shape" Height = "350" Width = "525" KeyUp = "Window_KeyUp">
<Canvas>
<Rectangle Stroke = "White" Width = "80" Height = "80" Name = "DisplayRectangle"/>
</Canvas>
</Window>

C # code:

private double RectangleCanvasLeft = 0;private double RectangleCanvasTop  = 0;private void Window_KeyUp(object sender, KeyEventArgs e){    switch (e.Key)    {        case Key.Up:            RectangleCanvasTop += 10;            break;        case Key.Down:            RectangleCanvasTop -= 10;            break;        case Key.Right:            RectangleCanvasLeft += 10;            break;        case Key.Left:            RectangleCanvasLeft -= 10;            break;        default:            break;        }        Canvas.SetLeft(DisplayRectangle, RectangleCanvasLeft);        Canvas.SetTop(DisplayRectangle, RectangleCanvasTop);    }
} Shape Introduction

We can use various shapes provided by WPF for 2D plotting. The Inheritance relationships of shapes are as follows:

<Rectangle Stroke = "White" Width = "80" Canvas. Top = "50" Canvas. Left = "50" Height = "80" Name = "DisplayRectangle"/>

C # code that implements the same function:

Canvas.SetLeft(DisplayRectangle, 50);Canvas.SetTop(DisplayRectangle, 50);
Line:

<Line X1 = "0" Y1 = "100" X2 = "100" Y2 = "0" Stroke = "Black"> </Line>

C # implementation:

var line = new Line(){    X1 = 0,    X2 = 100,    Y1 = 100,    Y2 = 0,    Stroke = new SolidColorBrush(Colors.Black)};MainCanvas.Children.Add(line);

 

Ellipse

 

XAML implementation:

<Ellipse Stroke="Black" Height="100" Width="100" ></Ellipse><Ellipse Stroke="Black" Canvas.Left="100" Height="100" Width="50" ></Ellipse>

C # implementation:

var circle = new Ellipse(){    Width = 100,    Height = 100,    Stroke = new SolidColorBrush(Colors.Black)};var ellipse = new Ellipse(){    Width = 50,    Height = 100,    Stroke = new SolidColorBrush(Colors.Black)};Canvas.SetLeft(ellipse, 100);MainCanvas.Children.Add(circle);MainCanvas.Children.Add(ellipse);

 

Polygon

<Polygon Points = "50,100 50, 50" Stroke = "Black"> </Polygon> <Polygon Canvas. left = "100" Points = "50,100 50, 50, 50" Fill = "White" Stroke = "Black"> </Polygon>

C # implementation:

var polygon1PointsCollection = new PointCollection();polygon1PointsCollection.Add(new Point() { X = 0, Y = 0 });polygon1PointsCollection.Add(new Point() { X = 50, Y = 50 });polygon1PointsCollection.Add(new Point() { X = 50, Y = 100 });var polygon1 = new Polygon(){    Stroke = new SolidColorBrush(Colors.Black),    Points = polygon1PointsCollection};MainCanvas.Children.Add(polygon1);var polygon2PointsCollection = new PointCollection();polygon2PointsCollection.Add(new Point() { X = 0, Y = 0 });polygon2PointsCollection.Add(new Point() { X = 50, Y = 50 });polygon2PointsCollection.Add(new Point() { X = 50, Y = 100 });polygon2PointsCollection.Add(new Point() { X = 100, Y = 50 });var polygon2 = new Polygon(){    Stroke = new SolidColorBrush(Colors.Black),    Points = polygon2PointsCollection,    Fill = new SolidColorBrush(Colors.White)};Canvas.SetLeft(polygon2, 100);MainCanvas.Children.Add(polygon2);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.