how to add shapes to canvas

Hi can anybody know how to add any shape like rectangle , circle or line to canvas through programmatically at mouse cursor possition.

thanx - Nagu



Answer this question

how to add shapes to canvas

  • geniusguy

    In my previous post, I used that to fill the Rectangle:

    r.Fill = Brushes.Black;

    So it could be easy to modify a little the code to use a different color or to change some values (width, height, etc...)


    Bye


  • TeeBoy

    Nagu wrote:

    could you give me a simple code. I dont know any thing about ContorlTemplate i'm knew to wpf.

    Thanx - Nagu



    Take a look at the examples of the SDK: they are a lot of them that will help you.
    Check also the hands of labs


    Bye


  • Par Hedberg

    Ya for that its working but problem is i'm creating 10 shapes randomly in those 10 shapes i'm selecting a particular shape for that its not working (For example if i have 10 shapes i want to apply properties for 5th shape then how can i reffer that particular shape )

    thanx


  • pentash

    could you give me a simple code. I dont know any thing about ContorlTemplate i'm knew to wpf.

    Thanx - Nagu


  • Geraldo Thomaz JR.

    Try this:

    XAML:
    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:m="clr-namespace:TestWPF"
    x:Class="Window1"
    xmlns:IcoToImageConverter="clr-namespace:TestWPF"
    Name="MainWindow"
    MouseDoubleClick="MyMouseDoubleClickEvent"
    >
    <Canvas>
    <Button Name="m_TestButton" VerticalAlignment="Center" HorizontalAlignment="Center" />

    <Canvas Name="TestCanvas" />
    </Canvas>
    </Window>

    C#:
    public void MyMouseDoubleClickEvent(object sender, System.Windows.Input.MouseEventArgs e)
    {
    // Get mouse position
    Point p = System.Windows.Input.Mouse.GetPosition(MainWindow);

    // Initialize a new Rectangle
    Rectangle r = new Rectangle();

    // Set up rectangle's size
    r.Width = 500;
    r.Height = 500;

    // Set up the Background color
    r.Fill = Brushes.Black;

    // Set up the position in the window, at mouse coordonate
    Canvas.SetTop(r, p.Y);
    Canvas.SetLeft(r, p.X);

    // Add rectangle to the Canvas
    TestCanvas.Children.Add(r);
    }


    Now, each time you will double clic on the Window, a black rectangle will appear at the mouse at the mouse cursor position :)


    HTH.


    Bye


  • Jacksdisplayname

    Ya i got it by reffering sender object

    any way thanx for ur reply

    Nagu


  • BauerK

    Is it possible to add any text to a shape
  • kailass

    Nagu wrote:
    Is it possible to add any text to a shape


    I think you will need to modify the ControlTemplate for the Shape


  • rod_r

    You can named the Shape so you might be able to reffer to a particular Shape by using its name.


    Bye


  • Shawn Rheal

    Thanx for ur reply but a small doubt regarding to zoom in and zoom out is it possible to apply zoom (or any thing like change background color or change height or width like that) properities to a particular shape(circle or rectangle)

    Thanx - Nagu


  • how to add shapes to canvas