Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone,
i m trying to draw a rectangle in C# just like MS paint. I m able to draw it but the problem is, as i m doing it on mouse down and mouse up event so when i m darging mouse towards upper left side of form it is woking fine but if i m moving mouse in any other direction then nothing i been drawn i think it is realated to quadrants but do not how to achive it. any help is welcome
Posted

1 solution

Well, I assume you've two points, say {x0,y0} (start-of-dragging point) and {x1,y1} end-of dragging point}. let's suppose x0 != x1 and y0 != y1.
You should find the top-left point of the rectangle and the its size (both X and Y sizes):
int xtl, ytl; // top-left point
int xbr, ybr; // bottom-right point
int xcs, ycs; // size
if (x0 < x0)
{
  xtl = x0; xbr = x1;
}
else
{
  xtl = x1; xbr = x0;
}

if (y0 < y1)
{
  ytl = y0; ybr = y1;
}
else
{
  ytl = y1; ybr = y0;
}

xcs = xbr - xtl;
ycx = ybr - ytl;


Now you may properly create a rectangle, this way
Rectangle r(xtl, ytl, xcs, ycs);


:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900