locked
Design surface in C#

Answers

  • Hi Paras,

     

    If your wanting to create a drawing surface I'd highly recommend you think about Windows Presentation Foundation. Here are a few links that might be of interested.

     

    http://www.codeproject.com/KB/WPF/WPF_DrawTools.aspx

    http://www.codeproject.com/KB/WPF/SpiroGraphWPF.aspx

    http://www.actiprosoftware.com/Support/ResourceGuides/WPF/Default.aspx

     

    Here is also a small, almost useless, example that I 'adapted' from the legend that is Mr Charles Petzold. It lets you put a rectange and circle on to the form and then lets you drag them about. Not exactly like the example you posted but near enough.

     

    Create a WPF application and paste this as the Window XAML.

     

    <Window x:Class="DesignSurfaceExample.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window1" Height="300" Width="300">

        <Grid>

            <Grid.RowDefinitions>

                <RowDefinition Height="66*" />

                <RowDefinition Height="196*" />

            </Grid.RowDefinitions>

            <Button Margin="13,9,139,0" Grid.Row="0"

                    Name="cmdAddRectange" Height="23"

                    VerticalAlignment="Top"

                    Click="cmdAddRectange_Click">Add Rectangle</Button>

            <Button Margin="13,33,139,10" Grid.Row="0"

                    Name="cmdAddCircle" Click="cmdAddEllipse_Click">Add Circle</Button>

     

            <Canvas Grid.Row="1" Name="drawingSurface">

     

            </Canvas>

     

        </Grid>

    </Window>

     

    Then paste this in the code behind....

     

    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;

     

    namespace DesignSurfaceExample

    {

        /// <summary>

        /// Interaction logic for Window1.xaml

        /// </summary>

        public partial class Window1 : Window

        {

            public Window1()

            {

                InitializeComponent();

            }

     

            //adds a rectangle to the canvas

            private void cmdAddRectange_Click(object sender, RoutedEventArgs e)

            {

                //create new rectangle and add to canvas

                Rectangle rect = new Rectangle();

                rect.Fill = Brushes.Black;

                rect.Width = 100;

                rect.Height = 150;

     

                //needs to be given a left and top position

                Canvas.SetLeft(rect, 0);

                Canvas.SetTop(rect, 0);

     

                //add to the canvas

                drawingSurface.Children.Add(rect);

            }

     

            //adds a circle to the canvas

            private void cmdAddEllipse_Click(object sender, RoutedEventArgs e)

            {

                Ellipse circle = new Ellipse();

                circle.Fill = Brushes.Black;

                circle.Width = 100;

                circle.Height = 100;

     

                //needs to be given a left and top position

                Canvas.SetLeft(circle, 0);

                Canvas.SetTop(circle, 0);

     

                //add to the canvas

                drawingSurface.Children.Add(circle);

            }

     

     

     

            #region Dragging Methods

     

            UIElement elDragging; //element being dragged

            Point ptMouseStart, ptElementStart; //where the mouse and elememnt started

     

            //on left mouse button down

            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs args)

            {

                base.OnMouseLeftButtonDown(args);

     

                // Get the element that was clicked and prepare for future events.

                ptMouseStart = args.GetPosition(drawingSurface);

                elDragging = drawingSurface.InputHitTest(ptMouseStart) as UIElement;

     

                //if an element was found

                if (elDragging != null)

                {

                    //store the elements starting position

                    ptElementStart = new Point(Canvas.GetLeft(elDragging), Canvas.GetTop(elDragging));

                }

            }

     

            //on mouse move

            protected override void OnMouseMove(MouseEventArgs args)

            {

                base.OnMouseMove(args);

     

                //if there is an element to drag

                if (elDragging != null)

                {

                    //get the position of the mouse as it's dragged

                    Point ptMouse = args.GetPosition(drawingSurface);

     

                    //move the element along with the mouse

                    Canvas.SetLeft(elDragging, ptElementStart.X + ptMouse.X - ptMouseStart.X);

                    Canvas.SetTop(elDragging, ptElementStart.Y + ptMouse.Y - ptMouseStart.Y);

                }

            }

     

            //on mouse up

            protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)

            {

                base.OnMouseLeftButtonUp(e);

                //no element to drag

                elDragging = null;

            }

     

            #endregion

        }

    }

     

    I really do think WPF will make the drawing surface easier to work with.

     

    *My appologies to everyone else for posting 'orrible C# !

     

     

    Tuesday, July 01, 2008 12:13 PM
  • Open the solution in SharpDevelop and convert the projects.

    Tuesday, July 01, 2008 11:32 AM

All replies

  •  

    Why do you want it in C#?
    Tuesday, July 01, 2008 11:29 AM
  • Open the solution in SharpDevelop and convert the projects.

    Tuesday, July 01, 2008 11:32 AM
  • Hi Paras,

     

    If your wanting to create a drawing surface I'd highly recommend you think about Windows Presentation Foundation. Here are a few links that might be of interested.

     

    http://www.codeproject.com/KB/WPF/WPF_DrawTools.aspx

    http://www.codeproject.com/KB/WPF/SpiroGraphWPF.aspx

    http://www.actiprosoftware.com/Support/ResourceGuides/WPF/Default.aspx

     

    Here is also a small, almost useless, example that I 'adapted' from the legend that is Mr Charles Petzold. It lets you put a rectange and circle on to the form and then lets you drag them about. Not exactly like the example you posted but near enough.

     

    Create a WPF application and paste this as the Window XAML.

     

    <Window x:Class="DesignSurfaceExample.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window1" Height="300" Width="300">

        <Grid>

            <Grid.RowDefinitions>

                <RowDefinition Height="66*" />

                <RowDefinition Height="196*" />

            </Grid.RowDefinitions>

            <Button Margin="13,9,139,0" Grid.Row="0"

                    Name="cmdAddRectange" Height="23"

                    VerticalAlignment="Top"

                    Click="cmdAddRectange_Click">Add Rectangle</Button>

            <Button Margin="13,33,139,10" Grid.Row="0"

                    Name="cmdAddCircle" Click="cmdAddEllipse_Click">Add Circle</Button>

     

            <Canvas Grid.Row="1" Name="drawingSurface">

     

            </Canvas>

     

        </Grid>

    </Window>

     

    Then paste this in the code behind....

     

    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;

     

    namespace DesignSurfaceExample

    {

        /// <summary>

        /// Interaction logic for Window1.xaml

        /// </summary>

        public partial class Window1 : Window

        {

            public Window1()

            {

                InitializeComponent();

            }

     

            //adds a rectangle to the canvas

            private void cmdAddRectange_Click(object sender, RoutedEventArgs e)

            {

                //create new rectangle and add to canvas

                Rectangle rect = new Rectangle();

                rect.Fill = Brushes.Black;

                rect.Width = 100;

                rect.Height = 150;

     

                //needs to be given a left and top position

                Canvas.SetLeft(rect, 0);

                Canvas.SetTop(rect, 0);

     

                //add to the canvas

                drawingSurface.Children.Add(rect);

            }

     

            //adds a circle to the canvas

            private void cmdAddEllipse_Click(object sender, RoutedEventArgs e)

            {

                Ellipse circle = new Ellipse();

                circle.Fill = Brushes.Black;

                circle.Width = 100;

                circle.Height = 100;

     

                //needs to be given a left and top position

                Canvas.SetLeft(circle, 0);

                Canvas.SetTop(circle, 0);

     

                //add to the canvas

                drawingSurface.Children.Add(circle);

            }

     

     

     

            #region Dragging Methods

     

            UIElement elDragging; //element being dragged

            Point ptMouseStart, ptElementStart; //where the mouse and elememnt started

     

            //on left mouse button down

            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs args)

            {

                base.OnMouseLeftButtonDown(args);

     

                // Get the element that was clicked and prepare for future events.

                ptMouseStart = args.GetPosition(drawingSurface);

                elDragging = drawingSurface.InputHitTest(ptMouseStart) as UIElement;

     

                //if an element was found

                if (elDragging != null)

                {

                    //store the elements starting position

                    ptElementStart = new Point(Canvas.GetLeft(elDragging), Canvas.GetTop(elDragging));

                }

            }

     

            //on mouse move

            protected override void OnMouseMove(MouseEventArgs args)

            {

                base.OnMouseMove(args);

     

                //if there is an element to drag

                if (elDragging != null)

                {

                    //get the position of the mouse as it's dragged

                    Point ptMouse = args.GetPosition(drawingSurface);

     

                    //move the element along with the mouse

                    Canvas.SetLeft(elDragging, ptElementStart.X + ptMouse.X - ptMouseStart.X);

                    Canvas.SetTop(elDragging, ptElementStart.Y + ptMouse.Y - ptMouseStart.Y);

                }

            }

     

            //on mouse up

            protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)

            {

                base.OnMouseLeftButtonUp(e);

                //no element to drag

                elDragging = null;

            }

     

            #endregion

        }

    }

     

    I really do think WPF will make the drawing surface easier to work with.

     

    *My appologies to everyone else for posting 'orrible C# !

     

     

    Tuesday, July 01, 2008 12:13 PM
  •  

    hello,

     

    i want the Design Surface Code in equivelent C# code.

     

    to do this,

     

    I tried doing so with the help of  "VB.Net To C#.Net converter" 2.18 ver.

     

    But I'm not getting the desired converted result.

     

     

    there are plenty of errors generated after conversion,

     

    even though when I turned OPTION STRICT ON

     

     

    so is there any proper way to convert multiple projects of a solution from VB.Net to C#.Net.

     

     

    plz help for the same.

    Wednesday, July 02, 2008 12:20 PM
  • When I converted it in accordance with my previous post I got no errors.

    Wednesday, July 02, 2008 1:25 PM