0
votes

I am drawing shapes on a canvas in wpf in visual studio. What is the (best) way to “add” two small circles on the inner right side of a Rectangle? I want them to seem to the user as “small holes” on the Rectangle. Should I get the coordinates of the right side of the Rectangle and by computing the respective demanded coordinates of the circles’ centers (I want them to be above and below of the middle of the rectangle symmetrically) to draw the circles? Is Canvas GetRight the appropriate method to get the coordinates of the right side of the Rectangle? How I apply it on the code:

shapeToRender = new Rectangle() { Fill = Brushes.Red, Height = 50, Width = 50, RadiusX = 10, RadiusY = 10 };

Canvas.SetLeft(shapeToRender, e.GetPosition(canvasDrawingArea).X - rectWidth / 2);
Canvas.SetTop(shapeToRender, e.GetPosition(canvasDrawingArea).Y - rectHeight / 2);

canvasDrawingArea.Children.Add(shapeToRender);

The shapes are created by a MouseEnter event.

1
The best way? The way that takes least calculations or is the easiest to understand?AlexSavAlexandrov

1 Answers

3
votes

Is this the type of thing you are going for?

enter image description here

This is done using:

<Grid x:Class="yourNs.RectangleWithCircles"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Height="50" Width="50">
    <Rectangle Fill="Red"/>
    <UniformGrid Rows="2">
        <Ellipse Grid.Row="0" Fill="White" Height="10" Width="10" HorizontalAlignment="Right" VerticalAlignment="Center" />
        <Ellipse Grid.Row="1" Fill="White" Height="10" Width="10" HorizontalAlignment="Right" VerticalAlignment="Center" />
    </UniformGrid>
</Grid>

Where yourNs is the namespace you provide, you would just add <yourNs:RectangleWithCircles /> to the canvas.

Because you have created the RectangleWithCircles class, you can easily customize it to fit your needs by adding a public method to show or hide the circles, for example.