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. drawing reverse rectangle using mouse

drawing reverse rectangle using mouse

Scheduled Pinned Locked Moved C#
graphicsgame-devhelp
3 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.
  • _ Offline
    _ Offline
    _tasleem
    wrote on last edited by
    #1

    hi i am making an applicaiton that draws the rectangles using mouse,when mouse is down it takes the starting points of rectangle(x,y) and when mouse moves then ending point of rectangle are taken and rectangle is drawn,here is code of that private void frmMedtronic_MouseMove(object sender, MouseEventArgs e) { if (mIsMouseDown) { //increase width of rectangle towards Rite if (e.Location.X > mrctRectangle.X) mrctRectangle.Width = e.Location.X - mrctRectangle.X; else // * trouble here { mrctRectangle.X = e.Location.X; mrctRectangle.Width = e.Location.X - mrctRectangle.X; } if (e.Location.Y > mrctRectangle.Y) mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; //moving Rectangle towards Left * else //trouble * { mrctRectangle.Y = e.Location.Y; mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; } } Invalidate(); } and in formpaint drawrectangle function is called. The problem is in moving rectangle toward left or moving upwards,as in this case ending points are greater then starting and in drawing as widht and height of rectangle can only b positive values not negative,i am having trouble in moving towards upward and left as point of screen are from 0 and not negative as in openGL, so suggest me what to do.

    Regards. Tasleem Arif

    A L 2 Replies Last reply
    0
    • _ _tasleem

      hi i am making an applicaiton that draws the rectangles using mouse,when mouse is down it takes the starting points of rectangle(x,y) and when mouse moves then ending point of rectangle are taken and rectangle is drawn,here is code of that private void frmMedtronic_MouseMove(object sender, MouseEventArgs e) { if (mIsMouseDown) { //increase width of rectangle towards Rite if (e.Location.X > mrctRectangle.X) mrctRectangle.Width = e.Location.X - mrctRectangle.X; else // * trouble here { mrctRectangle.X = e.Location.X; mrctRectangle.Width = e.Location.X - mrctRectangle.X; } if (e.Location.Y > mrctRectangle.Y) mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; //moving Rectangle towards Left * else //trouble * { mrctRectangle.Y = e.Location.Y; mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; } } Invalidate(); } and in formpaint drawrectangle function is called. The problem is in moving rectangle toward left or moving upwards,as in this case ending points are greater then starting and in drawing as widht and height of rectangle can only b positive values not negative,i am having trouble in moving towards upward and left as point of screen are from 0 and not negative as in openGL, so suggest me what to do.

      Regards. Tasleem Arif

      A Offline
      A Offline
      Andrei Ungureanu
      wrote on last edited by
      #2

      Try this:

      int StartX = (e.Location.X > mrctRectangle.X ? e.Location.X : mrctRectangle.X);
      int StartY = (e.Location.Y > mrctRectangle.Y ? e.Location.Y : mrctRectangle.Y);
      int EndX = (e.Location.X < mrctRectangle.X ? e.Location.X : mrctRectangle.X);
      int EndY = (e.Location.Y < mrctRectangle.Y ? e.Location.Y : mrctRectangle.Y);

      You have here the start and end points of an rectangle. Hope it helps.

      I will use Google before asking dumb questions

      1 Reply Last reply
      0
      • _ _tasleem

        hi i am making an applicaiton that draws the rectangles using mouse,when mouse is down it takes the starting points of rectangle(x,y) and when mouse moves then ending point of rectangle are taken and rectangle is drawn,here is code of that private void frmMedtronic_MouseMove(object sender, MouseEventArgs e) { if (mIsMouseDown) { //increase width of rectangle towards Rite if (e.Location.X > mrctRectangle.X) mrctRectangle.Width = e.Location.X - mrctRectangle.X; else // * trouble here { mrctRectangle.X = e.Location.X; mrctRectangle.Width = e.Location.X - mrctRectangle.X; } if (e.Location.Y > mrctRectangle.Y) mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; //moving Rectangle towards Left * else //trouble * { mrctRectangle.Y = e.Location.Y; mrctRectangle.Height = e.Location.Y - mrctRectangle.Y; } } Invalidate(); } and in formpaint drawrectangle function is called. The problem is in moving rectangle toward left or moving upwards,as in this case ending points are greater then starting and in drawing as widht and height of rectangle can only b positive values not negative,i am having trouble in moving towards upward and left as point of screen are from 0 and not negative as in openGL, so suggest me what to do.

        Regards. Tasleem Arif

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

        Hi, this looks the easiest to me: store MouseDown point in pt1, current MouseMove position in pt2, and in OnPaint for both x and y do:

        xMin= x1<=x2 ? x1 : x2;
        width= x1+x2-xMin -xMin; // x1+x2-xMin is xMax!

        ADDED: then Graphics.DrawRectangle(..., xMin, yMin, width, height); BTW: You might want to add 1 to width/height to always see the rectangle... :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


        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