tags:

views:

518

answers:

3

Hello ! Could you please point me to a good C# tutorial for drawing 2d graphics like Ellipse and Rectangle (that inherit from Shape) on a Canvas using WPF ? I'm also interested later to click on shapes and identify which shape was clicked, and also to drag and drop shapes on the canvas. Thank you !

+1  A: 

MSDN has a very good overview of Shapes and Basic Drawing in WPF.

For info on handling click on shapes, see Hit Testing in the Visual Layer.

Reed Copsey
+1  A: 

Thank you for the links. Could you please tell me what kind of project should I start in Visual Studio in order to compile and run this program (taken from Shapes and Basic Drawing in WPF):

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SDKSample
{
    public partial class SetBackgroundColorOfShapeExample : Page
    {
        public SetBackgroundColorOfShapeExample()
        {
            // Create a StackPanel to contain the shape.
            StackPanel myStackPanel = new StackPanel();

            // Create a red Ellipse.
            Ellipse myEllipse = new Ellipse();

            // Create a SolidColorBrush with a red color to fill the 
            // Ellipse with.
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();

            // Describes the brush's color using RGB values. 
            // Each value has a range of 0-255.
            mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
            myEllipse.Fill = mySolidColorBrush;
            myEllipse.StrokeThickness = 2;
            myEllipse.Stroke = Brushes.Black;

            // Set the width and height of the Ellipse.
            myEllipse.Width = 200;
            myEllipse.Height = 100;

            // Add the Ellipse to the StackPanel.
            myStackPanel.Children.Add(myEllipse);

            this.Content = myStackPanel;
        }

    }
}