问题描述
我有一个图片框
为用户控件
。我说这个用户控制
在主窗体上。现在,我必须按一个按钮,创建用户控制线。在我的项目,我每次按下这个按钮时,我想送两个的PointF(x和y)的用户控制参数,并绘制了新的路线,除了存在之一。我至今的油漆
时PictureBox的加载事件。
私人无效pictureBox1_Paint(对象发件人,PaintEventArgs的E)
{
笔graphPen =新朋(Color.Red,2);
的PointF pt1D =新的PointF();
的PointF pt2D =新的PointF();
pt1D.X = 0;
pt1D.Y = 10;
pt2D.X = 10;
pt2D.Y = 10;
e.Graphics.DrawLine(graphPen,pt1D,pt2D);
}
假设你想画在点击按钮就行,这里是你的代码的修改版本:
列表<的PointF>点=新的List<&的PointF GT;();
笔graphPen =新朋(Color.Red,2);
私人无效btnDrawLines_Click(对象发件人,EventArgs五)
{
图形G = picBox.CreateGraphics();
的PointF pt1D =新的PointF();
的PointF pt2D =新的PointF();
pt1D.X = 0;
pt1D.Y = 10;
pt2D.X = 10;
pt2D.Y = 10;
g.DrawLine(graphPen,pt1D,pt2D);
points.Add(pt1D);
points.Add(pt2D);
}
私人无效picBox_Paint(对象发件人,PaintEventArgs的E)
{
的for(int i = 0; I< points.Count;我+ = 2)
e.Graphics.DrawLine(graphPen,点[I],指向第[i + 1]);
}
请注意,你可以得到一个Graphics通过对象图片框
类的的createGraphics()方法是一样的 e.Graphics
对象在油漆
事件处理程序。
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);
}
Assuming that you want to draw the line on the click of the button, here's a modified version of your code:
List<PointF> points = new List<PointF>();
Pen graphPen = new Pen(Color.Red, 2);
private void btnDrawLines_Click(object sender, EventArgs e)
{
Graphics g = picBox.CreateGraphics();
PointF pt1D = new PointF();
PointF pt2D = new PointF();
pt1D.X = 0;
pt1D.Y = 10;
pt2D.X = 10;
pt2D.Y = 10;
g.DrawLine(graphPen, pt1D, pt2D);
points.Add(pt1D);
points.Add(pt2D);
}
private void picBox_Paint(object sender, PaintEventArgs e)
{
for (int i = 0; i < points.Count; i+=2)
e.Graphics.DrawLine(graphPen, points[i], points[i + 1]);
}
Note that you can get a Graphics object through the PictureBox
class's CreateGraphics()
method which is the same as the e.Graphics
object in the Paint
event handler.
这篇关于从父借鉴PictureBox的一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!