How Draw rectangle in WPF?

后端 未结 6 558
天涯浪人
天涯浪人 2020-12-30 02:19

I need draw rectangle in canvas. I know how to draw. But I did not get to do so would draw on a 360-degree

Example. blue, lilac, green they are one and the same rect

6条回答
  •  一向
    一向 (楼主)
    2020-12-30 03:01

    Steps:

    1. On MouseLeftButtonDown: if you are not rotating: add a rectangle with its top left corner at the coordinates of the mouse and its height and width calculated by the difference between the top corner and the mouse coordinates. Set a boolean to true to indicate that you are drawing. if you are rotating: stop rotating by setting the rotating boolean to false.

    2. On MouseMove: check if the left mouse button is still down and you are drawing (boolean from previous step). recalculate the width and height of the rectangle. If you are rotating adjust the rotation of the rectangle by calculating the angle between the point where you released the button, the RenderTransformOrigin and the current location of the mouse. (Use Vector.AngleBetween()

    3. On MouseLeftButtonUp: if drawing is true set the drawing boolean to false and set a rotating boolean to true.

    This flow will allow you to click (set a corner of the rectangle), drag and release to set the opposite corner, move the mouse to rotate the rectangle and click to fix the rectangle.

    Place and rotate the rectangle by using RenderTransform: that will make things much easier than setting margins or Canvas.Left on the rectangle.

    Let me know if you need help.

提交回复
热议问题