Click here to Skip to main content
15,790,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to rotate shape derived classes around their centre with the mouse. My code doesn't work. The shapes need to be created dynamically and not in XAML.

What I have tried:

C++
<pre>private void Shape_MouseMove(object sender, MouseEventArgs e)
        {
            Shape? obj = sender as Shape;

            if (obj != null)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    if (obj.IsMouseCaptured)
                    {
                        if (!(obj is Path))
                        {
                            if (Keyboard.Modifiers == ModifierKeys.Shift)
                            {
                                Point ptCenter = new Point(obj.ActualWidth / 2, obj.ActualHeight / 2);
                                Point pt = Mouse.GetPosition(obj);

                                double radians = Math.Atan2(pt.Y - ptCenter.Y, pt.X - ptCenter.X);
                                double angle = 180 * radians / Math.PI;
                                // Apply a 180 degree shift when X is negative so that we can rotate
                                // all of the way around
                                if (pt.X - ptCenter.X < 0)
                                {
                                    angle += 180;
                                }
                                RotateTransform? rt = obj.RenderTransform as RotateTransform;
                                //obj.RenderTransformOrigin = new Point(0.5, 0.5);
                                if (rt != null)
                                {
                                    rt.CenterX = ptCenter.X;
                                    rt.CenterY = ptCenter.Y;
                                    rt.Angle = angle;
                                }
                                else
                                {
                                    obj.RenderTransform = new RotateTransform()
                                    {
                                        CenterX = ptCenter.X,
                                        CenterY = ptCenter.Y,
                                        Angle = angle
                                    };
                                }
                                Title = angle.ToString();
                            }
Posted
Updated 14-Jul-22 13:41pm
v2
Comments
Gerry Schmitz 9-Jul-22 18:18pm    
Use RenderTransformOrigin for the center point.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.uielement.rendertransformorigin?view=windowsdesktop-6.0
FatalError0x4c 10-Jul-22 18:04pm    
Doesn't solve my problem. But thanks,

1 solution

// thanks to this at stackoverflow
C#
<Canvas Background="Transparent"
        MouseLeftButtonDown="CanvasMouseLeftButtonDown"
        MouseLeftButtonUp="CanvasMouseLeftButtonUp"
        MouseMove="CanvasMouseMove">

    <!-- shapes are added to this Canvas -->
</Canvas>

private Shape shape;

private void CanvasMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    shape = e.OriginalSource as Shape;
}

private void CanvasMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    shape = null;
}

private void CanvasMouseMove(object sender, MouseEventArgs e)
{
    if (shape != null)
    {
        var canvas = (Canvas)sender;
        var pos = e.GetPosition(canvas);
        var center = shape.TranslatePoint(
            new Point(shape.ActualWidth / 2, shape.ActualHeight / 2),
            canvas);

        if (shape.RenderTransform is not RotateTransform transform)
        {
            transform = new RotateTransform();
            shape.RenderTransform = transform;
            shape.RenderTransformOrigin = new Point(0.5, 0.5);
        }

        transform.Angle = -Vector.AngleBetween(pos - center, new Vector(1, 0));
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900