当我试图在PictureBox中绘制一个带有负坐标(-x和-y)的矩形时,矩形会消失,但是当它具有正坐标时,一切都没问题.这是代码:
这里我得到矩形的起始坐标
private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
start_point.X = e.X;
start_point.Y = e.Y;
}
Run Code Online (Sandbox Code Playgroud)
在这里我得到矩形的结束坐标:
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
end_point.X = e.X;
end_point.Y = e.Y;
PictureBox1.Refresh();
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我计算矩形的宽度和高度:
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(sb, start_point.X, start_point.Y, end_point.X - start_point.X, end_point.Y - start_point.Y);
}
Run Code Online (Sandbox Code Playgroud)
如果起点坐标小于终点坐标,则一切正常,但当结束坐标小于起点坐标时,宽度或高度或两个值均为负值...如何解决此问题?