Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dragable rectangles

Dragable rectangles

Scheduled Pinned Locked Moved C#
questioncomgraphicstools
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Casper Hansen
    wrote on last edited by
    #1

    Hello. I have a question.. I have this script:

    int mouseDownX;
    int mouseDownY;

        int width;
        int height;
    
        private void Form1\_MouseUp(object sender, MouseEventArgs mouseEv)
        {
            width = mouseEv.X - mouseDownX;
            height = mouseEv.Y - mouseDownY;
    
            textBox2.Text = Convert.ToString(mouseEv.Location);
    
            Graphics graphics = this.CreateGraphics();
    
            Rectangle rectangle = new Rectangle(
                    mouseDownX, mouseDownY, width, height);
    
            graphics.DrawRectangle(Pens.Red, rectangle);
        }
    
        private void Form1\_MouseDown(object sender, MouseEventArgs mouseEv)
        {
            textBox1.Text = Convert.ToString(mouseEv.Location);
    
            mouseDownX = mouseEv.X;
            mouseDownY = mouseEv.Y;
        }
    

    Which works fine if I make a rectangle from the left top corner to the right bottom corner But is there any easier way of just making rectangles? Here is a video of what I want: http://www.youtube.com/watch?v=V5YO4U1UnO0 And here is a download link to my project: http://peecee.dk/upload/download/123329 So I want to be able to draw rectangles from any angle if you understand?

    C C 2 Replies Last reply
    0
    • C Casper Hansen

      Hello. I have a question.. I have this script:

      int mouseDownX;
      int mouseDownY;

          int width;
          int height;
      
          private void Form1\_MouseUp(object sender, MouseEventArgs mouseEv)
          {
              width = mouseEv.X - mouseDownX;
              height = mouseEv.Y - mouseDownY;
      
              textBox2.Text = Convert.ToString(mouseEv.Location);
      
              Graphics graphics = this.CreateGraphics();
      
              Rectangle rectangle = new Rectangle(
                      mouseDownX, mouseDownY, width, height);
      
              graphics.DrawRectangle(Pens.Red, rectangle);
          }
      
          private void Form1\_MouseDown(object sender, MouseEventArgs mouseEv)
          {
              textBox1.Text = Convert.ToString(mouseEv.Location);
      
              mouseDownX = mouseEv.X;
              mouseDownY = mouseEv.Y;
          }
      

      Which works fine if I make a rectangle from the left top corner to the right bottom corner But is there any easier way of just making rectangles? Here is a video of what I want: http://www.youtube.com/watch?v=V5YO4U1UnO0 And here is a download link to my project: http://peecee.dk/upload/download/123329 So I want to be able to draw rectangles from any angle if you understand?

      C Offline
      C Offline
      C1AllenS
      wrote on last edited by
      #2

      Hello, There is no other way apart from this to draw rectangles. However, the only problem was the calculation part which was working only when you draw from lefttop to right bottom. However, while doing vice versa, the height and width was getting negative values. Try this piece of code in MouseUp event()

          private void Form1\_MouseUp(object sender, MouseEventArgs mouseEv)
          {
      
              textBox2.Text = Convert.ToString(mouseEv.Location);
      
              Graphics graphics = this.CreateGraphics();
              Rectangle rectangle;            
      
              if (mouseEv.Y > mouseDownY)
              {
                  height = mouseEv.Y - mouseDownY;
              }
              else
                  height =  mouseDownY - mouseEv.Y;
              
              if (mouseEv.X < mouseDownX)
              {
                  width = mouseDownX - mouseEv.X;
                  
                  rectangle = new Rectangle(mouseEv.X, mouseEv.Y, width, height);
              }
              else
              {
                  width = mouseEv.X - mouseDownX;
                  rectangle = new Rectangle(mouseDownX, mouseDownY, width, height);
              }
      
              graphics.DrawRectangle(Pens.Red, rectangle);
                          
          }
      

      I believe this should resolve your issue. Regards, Allen

      Allen Smith ComponentOne LLC www.componentone.com

      L 1 Reply Last reply
      0
      • C C1AllenS

        Hello, There is no other way apart from this to draw rectangles. However, the only problem was the calculation part which was working only when you draw from lefttop to right bottom. However, while doing vice versa, the height and width was getting negative values. Try this piece of code in MouseUp event()

            private void Form1\_MouseUp(object sender, MouseEventArgs mouseEv)
            {
        
                textBox2.Text = Convert.ToString(mouseEv.Location);
        
                Graphics graphics = this.CreateGraphics();
                Rectangle rectangle;            
        
                if (mouseEv.Y > mouseDownY)
                {
                    height = mouseEv.Y - mouseDownY;
                }
                else
                    height =  mouseDownY - mouseEv.Y;
                
                if (mouseEv.X < mouseDownX)
                {
                    width = mouseDownX - mouseEv.X;
                    
                    rectangle = new Rectangle(mouseEv.X, mouseEv.Y, width, height);
                }
                else
                {
                    width = mouseEv.X - mouseDownX;
                    rectangle = new Rectangle(mouseDownX, mouseDownY, width, height);
                }
        
                graphics.DrawRectangle(Pens.Red, rectangle);
                            
            }
        

        I believe this should resolve your issue. Regards, Allen

        Allen Smith ComponentOne LLC www.componentone.com

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Almost there. You must draw a positive distance starting from the smallest value, both for X and width, and for Y and height. It may be different though in both dimensions, your code assumes it is the same for both. You really should introduce new variables for Xmin and Ymin. I typically like to add a method DrawRectangle that hides these limitations, so it accepts all value combinations for X1,Y1 and X2,Y2 :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Voting for dummies? No thanks. X|


        1 Reply Last reply
        0
        • C Casper Hansen

          Hello. I have a question.. I have this script:

          int mouseDownX;
          int mouseDownY;

              int width;
              int height;
          
              private void Form1\_MouseUp(object sender, MouseEventArgs mouseEv)
              {
                  width = mouseEv.X - mouseDownX;
                  height = mouseEv.Y - mouseDownY;
          
                  textBox2.Text = Convert.ToString(mouseEv.Location);
          
                  Graphics graphics = this.CreateGraphics();
          
                  Rectangle rectangle = new Rectangle(
                          mouseDownX, mouseDownY, width, height);
          
                  graphics.DrawRectangle(Pens.Red, rectangle);
              }
          
              private void Form1\_MouseDown(object sender, MouseEventArgs mouseEv)
              {
                  textBox1.Text = Convert.ToString(mouseEv.Location);
          
                  mouseDownX = mouseEv.X;
                  mouseDownY = mouseEv.Y;
              }
          

          Which works fine if I make a rectangle from the left top corner to the right bottom corner But is there any easier way of just making rectangles? Here is a video of what I want: http://www.youtube.com/watch?v=V5YO4U1UnO0 And here is a download link to my project: http://peecee.dk/upload/download/123329 So I want to be able to draw rectangles from any angle if you understand?

          C Offline
          C Offline
          Casper Hansen
          wrote on last edited by
          #4

          Hey I have this project now: http://peecee.dk/upload/download/123403 It allmost works, the preview thing just doesnt work properly Could you guys take a look at it? Because I cant seem to find a soloution

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups