none
Draw rectangle under an angle with mouse

    Question

  • Hi,

    i would like to do that what i said in topic. I would like to rotate an rectangle around my first point witch i create with mouse down event.I would also like to langhten that rectangle at the sam time. Height is standard.


    Thanks
    Tuesday, November 13, 2007 8:49 AM

Answers

  • Hi, Jaby,

     

    Based on my understanding, you want to draw a width-fixed rectangle and rotate it with your mouse, don't you?

     

    Well, we should calculate the width and angle to achieve your goal.

    For example

    Code Block

            Point clickPoint;

            bool mouseDown = false;

            int recHeight = 30; //suppose the height is 30

            Rectangle drawRec;

            float angle;

            public Form1()

            {

                InitializeComponent();

                drawRec = new Rectangle();

                drawRec.Height = recHeight;

            }

     

            private void Form1_MouseDown(object sender, MouseEventArgs e)

            {

                mouseDown = true;

                clickPoint = e.Location;//Get the location when mouse is down.

            }

     

            private void Form1_MouseMove(object sender, MouseEventArgs e)

            {

                if (mouseDown)

                {

                    angle = (float)(Math.Atan((double)(e.Y - clickPoint.Y) / (double)(e.X - clickPoint.X)) * 180 / Math.PI); //Calculate the angle when mouse is moving

                    if (e.X - clickPoint.X < 0)

                    {

                        angle += 180;

                    }

                   

                    label1.Text = angle.ToString();

                    drawRec.Width = (int) Math.Sqrt(Math.Pow((double)(e.X - clickPoint.X), 2) + Math.Pow((double)(e.Y - clickPoint.Y), 2)); //Calculate the width of your rectangle

                    this.Invalidate();

                }

            }

     

            private void Form1_MouseUp(object sender, MouseEventArgs e)

            {

                mouseDown = false;

            }

     

            private void Form1_Paint(object sender, PaintEventArgs e)

            {

                if (mouseDown)

                {

                    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //Draw the rectangle and rotate it with your mouse

                    e.Graphics.TranslateTransform((float)clickPoint.X, (float)clickPoint.Y);

                    e.Graphics.RotateTransform(angle);

                    e.Graphics.DrawRectangle(Pens.Black, drawRec);

                }

            }

     

     

    Hope this helps,

    Regards

    Monday, November 19, 2007 2:51 AM

All replies

  • Hi, Jaby,

     

    Based on my understanding, you want to draw a width-fixed rectangle and rotate it with your mouse, don't you?

     

    Well, we should calculate the width and angle to achieve your goal.

    For example

    Code Block

            Point clickPoint;

            bool mouseDown = false;

            int recHeight = 30; //suppose the height is 30

            Rectangle drawRec;

            float angle;

            public Form1()

            {

                InitializeComponent();

                drawRec = new Rectangle();

                drawRec.Height = recHeight;

            }

     

            private void Form1_MouseDown(object sender, MouseEventArgs e)

            {

                mouseDown = true;

                clickPoint = e.Location;//Get the location when mouse is down.

            }

     

            private void Form1_MouseMove(object sender, MouseEventArgs e)

            {

                if (mouseDown)

                {

                    angle = (float)(Math.Atan((double)(e.Y - clickPoint.Y) / (double)(e.X - clickPoint.X)) * 180 / Math.PI); //Calculate the angle when mouse is moving

                    if (e.X - clickPoint.X < 0)

                    {

                        angle += 180;

                    }

                   

                    label1.Text = angle.ToString();

                    drawRec.Width = (int) Math.Sqrt(Math.Pow((double)(e.X - clickPoint.X), 2) + Math.Pow((double)(e.Y - clickPoint.Y), 2)); //Calculate the width of your rectangle

                    this.Invalidate();

                }

            }

     

            private void Form1_MouseUp(object sender, MouseEventArgs e)

            {

                mouseDown = false;

            }

     

            private void Form1_Paint(object sender, PaintEventArgs e)

            {

                if (mouseDown)

                {

                    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //Draw the rectangle and rotate it with your mouse

                    e.Graphics.TranslateTransform((float)clickPoint.X, (float)clickPoint.Y);

                    e.Graphics.RotateTransform(angle);

                    e.Graphics.DrawRectangle(Pens.Black, drawRec);

                }

            }

     

     

    Hope this helps,

    Regards

    Monday, November 19, 2007 2:51 AM
  • Thanks, that was exectly what i needed.
    Monday, November 19, 2007 8:28 AM
  • Hi, its me again.
    I was wondering if u could tell me how to change the code so that it would
    my mouse pointer be at the lower line of the rectangle when I'm drawing.

    I hope you undarstud me.

    Ohh and how to use this in tabcontrol(tabpage).

    tnx

    Regards
    Tuesday, November 27, 2007 4:10 PM
  • Hi,
    i manage to fix the issue why it didnt work with tabcontrol, i forgot to use tabpage1.refresh(); when mouse moves. But i still dont know how to fix my first problem, how to draw the rectangle, that it would be above the pointer.


    Sunday, December 02, 2007 11:45 AM