C#中的矩形 [英] Rectangle in C#

查看:200
本文介绍了C#中的矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我试图像MS画图一样在C#中绘制一个矩形.我能够绘制它,但是问题是,当我在鼠标按下和鼠标向上事件中执行此操作时,当我将鼠标向窗体的左上方放大时,它可以正常工作,但是如果我将鼠标向任何其他方向移动,则我什么也没做绘制,我认为它已实现到象限,但不知道如何实现.欢迎任何帮助.

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

推荐答案

好吧,我假设您有两个观点,例如{x0,y0}(拖动起点)和{x1,y1}结束-拖动点}.让我们假设x0 != x1 y0 != y1.
您应该找到矩形的左上角点及其大小(X和Y大小):
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);



:)



:)


这篇关于C#中的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆