none
Rectangle rotate once at MouseRightButtonDown RRS feed

  • Question

  • Hi all, 

    I need your help with this issue i have been struggling for the past days. I  want to rotate a rectangle at an angle of 30° everytime i press the rightClickButton.

    It works fine when i click once on the rightClickButton.But when i want to click again it doesnt rotate anymore.

    It's the same behavior with MouseButtonEventHandler or without.

    Can you guys help me ?? 

    Thanks in advance.

      private void Rectangle_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
            {

                MouseButtonEventHandlerRightClick(sender, e);
             
            
            }

            private void MouseButtonEventHandlerRightClick(object sender, MouseButtonEventArgs e)
            {
                element = (Rectangle)sender;
               
                    if (Mouse.RightButton == MouseButtonState.Pressed)
                    {


                        Console.WriteLine("has cliqued right");

                        anc_width = element.Width;
                        anc_height = element.Height;
                        i += 1;


                        Console.WriteLine("before transform of guantanamera");
                        double angle = 30;
                        RotateTransform rotateTransform2 = new RotateTransform(angle);
                        rotateTransform2.CenterX = element.Width / 2;
                        rotateTransform2.CenterY = element.Height / 2;

                        // Console.WriteLine(element.Width / 2 + " toto "+ element.Height/2);


                        element.RenderTransform = rotateTransform2;



                        /* Canvas.SetLeft(element, Canvas.GetLeft(element)+40);
                         Canvas.SetTop(element, Canvas.GetTop(element));*/

                    }
                    else
                    {
                        Console.WriteLine("Releasing Element");
                        element = null;
                    }
                

              

            }

    Thursday, December 5, 2019 11:19 AM

All replies

  • Your problem is that the RotateTransform is not an incremental object.  The angle is the angle for the starting position.  You will need to keep the current Transform (WPF does that for you) and when you click just increment the angle by 30.

    Lloyd Sheen

    Thursday, December 5, 2019 5:49 PM
  • Hi,

    Do you resolve the issue?You need to modify the code:

      private void MouseButtonEventHandlerRightClick(object sender, MouseButtonEventArgs e)
            {
               element = (Rectangle)sender;
    
                if (Mouse.RightButton == MouseButtonState.Pressed)
                {
    
    
                    Console.WriteLine("has cliqued right");
    
                   anc_width = element.Width;
                  anc_height = element.Height;
                    i += 1;
    
    
                    Console.WriteLine("before transform of guantanamera");
                    double angle = 30*i;
                    RotateTransform rotateTransform2 = new RotateTransform(angle);
                    rotateTransform2.CenterX = element.Width / 2;
                    rotateTransform2.CenterY = element.Height / 2;
    
                    // Console.WriteLine(element.Width / 2 + " toto "+ element.Height/2);
    
    
                    element.RenderTransform = rotateTransform2;
    
    
    
                    /* Canvas.SetLeft(element, Canvas.GetLeft(element)+40);
                     Canvas.SetTop(element, Canvas.GetTop(element));*/
    
                }
                else
                {
                    Console.WriteLine("Releasing Element");
                    element = null;
                }
    
    
    
    
            }

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 9, 2019 5:32 AM
    Moderator