在 PictureBox 上从父级画一条线
我有一个 PictureBox
作为 UserControl
。我在主窗体上添加了这个用户控件
。现在我必须按下一个按钮并在用户控件上创建一条线。在我的项目中,每次按下此按钮时,除了现有的一条线之外,我还想向用户发送两个 PointF(x 和 y)的控制参数并绘制一条新线。到目前为止,当加载图片框时,我有 Paint
事件。
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Pen graphPen = new Pen(Color.Red, 2);
PointF pt1D = new PointF();
PointF pt2D = new PointF();
pt1D.X = 0;
pt1D.Y = 10;
pt2D.X = 10;
pt2D.Y = 10;
e.Graphics.DrawLine(graphPen, pt1D, pt2D);
}
I have a PictureBox
as UserControl
. I added this User Control
on the main form. Now I have to press a button and create a line on the user control. On my project, every time I press this button, I want to send to user control parameters of two PointF(x and y) and draw a new line, in addition to the existent one. I have so far the Paint
event when picturebox is loaded.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Pen graphPen = new Pen(Color.Red, 2);
PointF pt1D = new PointF();
PointF pt2D = new PointF();
pt1D.X = 0;
pt1D.Y = 10;
pt2D.X = 10;
pt2D.Y = 10;
e.Graphics.DrawLine(graphPen, pt1D, pt2D);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您想在单击按钮时绘制线条,这是代码的修改版本:
请注意,您可以通过
PictureBox
类的CreateGraphics() 方法,与
Paint
事件处理程序中的e.Graphics
对象相同。Assuming that you want to draw the line on the click of the button, here's a modified version of your code:
Note that you can get a Graphics object through the
PictureBox
class'sCreateGraphics()
method which is the same as thee.Graphics
object in thePaint
event handler.如果您要添加要绘制的线条,您可能需要一个
Line
类:然后您可以将这些“线条”添加到列表中:
然后添加到它们并告诉控件更新它的绘图:
然后在您的绘图中:
If you are adding lines to be drawn, the you probably want a little
Line
class:And then you can just add these "lines" to a list:
and add to them and tell the control to update it's drawing:
then in your drawing: