为什么这不画任何东西?
Why does this not draw anything?
我的代码中有以下 if 语句:
//global variables
int x1;
int y1;
int x2;
int y2;
int counter = 0;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (radioButtonDrawLine.Checked)
{
if (counter == 0)
{
x1 = Cursor.Position.X;
y1 = Cursor.Position.Y;
counter++;
}
else
{
x2 = Cursor.Position.X;
y2 = Cursor.Position.Y;
if (counter == 1)
{
Graphics g = CreateGraphics();
g.DrawLine(Pens.Black, x2, y2, x1, y1);
}
counter = 0;
}
}
}
我应该在我的图片框上点击两次,每次点击它都会保存 x 和 y。第二次单击时,应在两个坐标之间绘制一条线。
虽然这不起作用,但我不明白为什么。谁能告诉我怎么了?
您应该在 Paint 事件中进行绘图。像下面这样的东西应该可以工作:
//global variables
private Point? p1;
private Point? p2;
private int counter = 0;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (radioButtonDrawLine.Checked)
{
if (counter == 0)
{
p1 = pictureBox1.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
counter++;
}
else
{
p2 = pictureBox1.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
pictureBox1.Refresh();
counter = 0;
}
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (p1.HasValue && p2.HasValue)
{
e.Graphics.DrawLine(Pens.Black, p1.Value.X, p1.Value.Y, p2.Value.X, p2.Value.Y);
}
}
pictureBox1.Refresh()
调用是在创建第二个点后强制重绘。请注意,我还使用了 Point 结构而不是 int 来存储坐标,因为这样可以将坐标保持在逻辑组中并使其更清楚。
您的代码存在两个主要问题。
首先,您在表单上调用 CreateGraphics
,而不是在图片框上 - 因此,如果您确实绘制到正确的位置,则绘图会被图片框隐藏。
其次,您的坐标已关闭,因为 Cursor.Position
returns 屏幕坐标,而不是相对于您正在绘制的控件的坐标。但这已经没有必要了——你不应该首先使用 Click
事件,而应该使用 MouseUp
。 Click
是一个不同的动作,它根本不需要涉及定点设备(例如按按钮上的 space)。你想处理鼠标点击,所以使用鼠标事件。作为奖励,您将在事件处理程序的参数中获得点击的本地坐标 :)
最后,如果你想让图像持久化,我建议不要直接绘制到图片框图形对象,而是创建一个内存位图来保存绘图,让图片框来做重新粉刷它认为合适的。否则,任何导致图片框重新绘制的内容也会清除您目前绘制的内容。
我的代码中有以下 if 语句:
//global variables
int x1;
int y1;
int x2;
int y2;
int counter = 0;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (radioButtonDrawLine.Checked)
{
if (counter == 0)
{
x1 = Cursor.Position.X;
y1 = Cursor.Position.Y;
counter++;
}
else
{
x2 = Cursor.Position.X;
y2 = Cursor.Position.Y;
if (counter == 1)
{
Graphics g = CreateGraphics();
g.DrawLine(Pens.Black, x2, y2, x1, y1);
}
counter = 0;
}
}
}
我应该在我的图片框上点击两次,每次点击它都会保存 x 和 y。第二次单击时,应在两个坐标之间绘制一条线。 虽然这不起作用,但我不明白为什么。谁能告诉我怎么了?
您应该在 Paint 事件中进行绘图。像下面这样的东西应该可以工作:
//global variables
private Point? p1;
private Point? p2;
private int counter = 0;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (radioButtonDrawLine.Checked)
{
if (counter == 0)
{
p1 = pictureBox1.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
counter++;
}
else
{
p2 = pictureBox1.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
pictureBox1.Refresh();
counter = 0;
}
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (p1.HasValue && p2.HasValue)
{
e.Graphics.DrawLine(Pens.Black, p1.Value.X, p1.Value.Y, p2.Value.X, p2.Value.Y);
}
}
pictureBox1.Refresh()
调用是在创建第二个点后强制重绘。请注意,我还使用了 Point 结构而不是 int 来存储坐标,因为这样可以将坐标保持在逻辑组中并使其更清楚。
您的代码存在两个主要问题。
首先,您在表单上调用 CreateGraphics
,而不是在图片框上 - 因此,如果您确实绘制到正确的位置,则绘图会被图片框隐藏。
其次,您的坐标已关闭,因为 Cursor.Position
returns 屏幕坐标,而不是相对于您正在绘制的控件的坐标。但这已经没有必要了——你不应该首先使用 Click
事件,而应该使用 MouseUp
。 Click
是一个不同的动作,它根本不需要涉及定点设备(例如按按钮上的 space)。你想处理鼠标点击,所以使用鼠标事件。作为奖励,您将在事件处理程序的参数中获得点击的本地坐标 :)
最后,如果你想让图像持久化,我建议不要直接绘制到图片框图形对象,而是创建一个内存位图来保存绘图,让图片框来做重新粉刷它认为合适的。否则,任何导致图片框重新绘制的内容也会清除您目前绘制的内容。