views:

132

answers:

4

Hi,

Consider this code:

Ellipse myCircle = new Ellipse();
myCircle.Width = 400;
myCircle.Height = 400;
myCircle.Stroke = Brushes.PeachPuff;
myCircle.StrokeThickness = 25;
myCircle.ToolTip = "Bold Circle";
Canvas.SetTop(myCircle, 0);
Canvas.SetLeft(myCircle, 0);
canvas.Children.Add(myCircle);

Ellipse myCircle2 = new Ellipse();
myCircle2.Width = 400;
myCircle2.Height = 400;
myCircle2.Stroke = Brushes.Black;
myCircle2.StrokeThickness = 1;
Canvas.SetTop(myCircle2, 0);
Canvas.SetLeft(myCircle2, 0);
canvas.Children.Add(myCircle2);

I see that myCircle2 lying at the edge of myCircle. How can I define the stroke style of myCircle such that myCircle2 will lie in the middle of myCircle ?

Thanks !

A: 

There're several ways: one is this (in XAML)

<Canvas>  
<Ellipse Width="400" Height="400"  Stroke="PeachPuff" StrokeThickness="25" />
<Ellipse Width="375" Height="375"  Stroke="Black" StrokeThickness="1" Margin="12.5 12.5 0 0" />

ema
A: 

Actually, I was looking for changing some parameter of the Bold circle (myCircle), and not myCircle2. Though, I tried your suggestion in C#, and got an unsatisfactory result. The black circle is not in the middle of the Bold one... Any other ideas ?

A: 

Try this as a general case:

        int diameter = 400;
        int thickness = 25;

        Ellipse myCircle = new Ellipse();
        myCircle.StrokeThickness = thickness;
        myCircle.Width = diameter + thickness;
        myCircle.Height = diameter + thickness;
        myCircle.Stroke = Brushes.PeachPuff;
        myCircle.ToolTip = "Bold Circle";
        Canvas.SetTop(myCircle, -thickness/2);
        Canvas.SetLeft(myCircle, -thickness / 2);
        canvas.Children.Add(myCircle);

        Ellipse myCircle2 = new Ellipse();
        myCircle2.Width = diameter;
        myCircle2.Height = diameter;
        myCircle2.Stroke = Brushes.Black;
        myCircle2.StrokeThickness = 1;
        Canvas.SetTop(myCircle2, 0);
        Canvas.SetLeft(myCircle2, 0);
        canvas.Children.Add(myCircle2);

The adjustment here is to accommodate the thickness of your bold circle by adjusting the width, height in the positive direction and adjusting the top and left in the negative direction by half to recenter.

Godeke
A: 

Thank you ! Looks perfect !

One more question:

Is this:

Canvas.SetTop(myCircle, ...);
Canvas.SetLeft(myCircle, ...);
canvas.Children.Add(myCircle);

the only way to specify where myCircle will appear on the canvas ? I'm looking for a shorter method doing that. Thanks !