I combined mouse events o move my drawn rectangle object. Everything was fine i could move it and when i released the mouse button it stopped moving and settled down. However after rotating that object and then trying to move it around I just could not perform the action again. I have used the ResetTransform method at the end but this didn't help eihter. I have an alternative way for this but i would like to stick with that(above mentioned). Any sugestions what's going on behind the graphic scene and why i m unable to move my rectangle when rotation is performed?

Recommended Answers

All 7 Replies

Code? In particular, your mouse event handlers, paint method, and anything you're using to track object position & orientation will be helpful.

It s a pretty long code...So the problem is that i can't get my mousedown event to work ONCE the rectangle is rotated.if a normal rectangle is drawn and if you then try to move it and drop it works fine but once the rotation occurs the thing get's stuck. Any help on that would be greatly apreciated.In the rotation mehtod i m not recording my rec coordinates since resettransform is here.

 public Form1()
        {
            InitializeComponent();

           rect = new Rectangle(x_cor, y_cor, rec_width, rec_height);
        }
        bool moving = false;
        Graphics paper;
        int rec_width = 10;
        int rec_height = 30;
        int x_cor = 100;
        int y_cor = 100;
        Rectangle rect;
        Point cursor;
        Bitmap btm;
        Graphics bmp;
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private bool InObjectArea(MouseEventArgs e)
        {
            if (rect.Contains(cursor))
            {
                return true;
            }
            else
                return false;
        }

        private void button1_Click(object sender, EventArgs e)// (Button Create Object)
        {
            if (checkBox1.Checked) 
            {
                paper = this.CreateGraphics();
                RotateImage(paper, 75);
             }
        }



        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (InObjectArea(e))
            {
                moving = true;
            }
            else
                moving = false; 
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (rect.Contains(cursor))
            {
                rect.X = e.X;
                rect.Y = e.Y;
            }


            moving = false; 
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            label6.Text = e.Location.ToString();
            cursor.X = e.X;
            cursor.Y = e.Y;

            if (moving)
            {
                rect.X = e.X;
                rect.Y = e.Y;
                paper.Clear(Color.Gray);


            } 
        }
        public void RotateImage(Graphics pape,int rtangle)  // method 0, main method
        {
            pape.TranslateTransform(x_cor, y_cor);
            pape.RotateTransform(rtangle);

            paper.DrawRectangle(new Pen(Brushes.Blue), -(float)rect.Width / 2, -(float)rect.Height / 2, rec_width, rec_height);
            pape.ResetTransform();
        }
        private bool InObjectArea(MouseEventArgs e)
        {
            if (rect.Contains(cursor))
            {
                return true;
            }
            else
                return false;
        }

Seems this method isn't firing. Seeing as you created a new Rectangle object here. Correct me if i'm wrong

        public void RotateImage(Graphics pape,int rtangle)  // method 0, main method
        {
            pape.TranslateTransform(x_cor, y_cor);
            pape.RotateTransform(rtangle);
            paper.DrawRectangle(new Pen(Brushes.Blue), -(float)rect.Width / 2, -(float)rect.Height / 2, rec_width, rec_height);
            pape.ResetTransform();
        }

Regards

Well, i made a mistake here but after fixing it didnt work either. I have a problem with my rotation here so if anyone can tell me why this isnt drawn, when debugging, it goes into the button event and inside the if codee but i can t see the drawing on the form i guess its drawn but at some weird position.

 private void button1_Click(object sender, EventArgs e)// (Button Create Object)
        {
            if (checkBox1.Checked) 
            {
                paper = this.CreateGraphics();

                paper.TranslateTransform((float)rect.Width / 2, (float)rect.Height / 2);

                paper.RotateTransform(75);

                paper.TranslateTransform(-(float)rect.Width / 2, -(float)rect.Height / 2);
                paper.DrawRectangle(new Pen(Brushes.Blue),x_cor, y_cor,  rec_width, rec_height);
                paper.ResetTransform();



                /*Image imagefile=Image.FromFile(@"C:\Users\eugen\Desktop\22200.png");
                Bitmap bms = new Bitmap(imagefile);
                rotateImage(bms, 45F);
                paper.DrawImage(bms, new Point(100, 100));*/
             }
        }

I think that i know what the problem is and i also think that you ll agree with me. The problem is that i don t know how to fix this. When i use the TranslateTransform method it automatically makes a new coordinate system starting from the given axes so it starts at (0,0), after that i rotate it and after rotation i draw my rectangle wich is ON THE NEW COORDINATE SYSTEM wich started a (0,0). I apply then the ResetTransform method wich puts it back to normal but that doesn t solve the problem. The conclusion is that I should avoid the translate transform by all means. Should i make a custom rotation rather the rotating it with a built in method or is there something wich would be easier to apply?

If you apply standard graphics programming concepts in things like OpenGL and DirectX you have a concept of matrices which control the location and rotation of your objects. (Sure you've heard of things like WorldSpace, CameraSpace and ModelSpace) This involves pushing and popping matrixes to apply the transforms correctly. This is essentially what the *Transform methods do.

What you're doing is mostly correct, however (if I remember my WPF correctly...), you need to apply your TranslateTransform to the RenderTransform before you reset it.

EDIT: If it still doesn't work, post a link to the project (or PM me) and I'll take a quick look. It might not be related to your graphics methods in the end, the documentation on *Transform is pretty weak...

Thanx for the response Ketsuekiame, you really put maximum effort in every of your post. I m little bit busy with some other stuff but as soon as i m done i l get to this again. Tnx for the suggestions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.