在循环中画多条线 [英] Draw multiple line in a loop
问题描述
我想将所有线条绘制在一起.这就是为什么我在此循环中调用循环的原因,我在其中检查一些条件,然后画线,但一次只画一条线.但是在功能上我做得不错,我通过断点和消息框进行了检查,但只显示了一行.请帮忙.
私有 无效 drawAll_line()
{
for ( int i = 1 ; i < = Anum; i ++)
{
如果(!vAnd [i] .isPin1Open)
{
如果(vAnd [i] .VpIn1)
{
System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
System.Drawing.Pen myPen;
myPen = 新 System.Drawing.Pen(Color.Green);
formGraphics.DrawLines(myPen,vAnd [i] .pin1PointPath);
myPen.Dispose();
formGraphics.Dispose();
}
其他
{
System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
System.Drawing.Pen myPen;
myPen = 新 System.Drawing.Pen(Color.Red);
formGraphics.DrawLines(myPen,vAnd [i] .pin1PointPath);
myPen.Dispose();
formGraphics.Dispose();
}
}
如果(!vAnd [i] .isPin2Open)
{
如果(vAnd [i] .VpIn2)
{
System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
System.Drawing.Pen myPen;
myPen = 新 System.Drawing.Pen(Color.Green);
formGraphics.DrawLines(myPen,vAnd [i] .pin2PointPath);
myPen.Dispose();
formGraphics.Dispose();
}
其他
{
System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
System.Drawing.Pen myPen;
myPen = 新 System.Drawing.Pen(Color.Red);
formGraphics.DrawLines(myPen,vAnd [i] .pin2PointPath);
myPen.Dispose();
formGraphics.Dispose();
}
}
}
}
调试器可能是弄清楚为什么未按预期绘制线条的最佳方法.话虽如此,我看到了一些奇怪的事情:
首先,for (int i = 1; i <= Anum; i++)数组基于0,所以不是
for (int i = 0; i < Anum; i++)还是更好的
for (int i = 0; i < vAnd.Length /* or vAnd.Count for a List */; i++)?
然后,panel1.CreateGraphics()
让我想知道您是否在正确的位置进行绘制.在控件上绘图时,您需要在Control
的OnPaint
事件处理程序中执行此操作,该处理程序为您提供要在其上进行绘制的Graphics
对象.
每次调用该方法时创建和处置新的Pen
都是很浪费的,您可以通过使用静态的Pens.Red
和Pens.Green
对象来节省一些资源.
如果这些帮助都没有找到问题,请尝试在每次调用DrawLines
的地方都设置一个断点,以确保它被调用了预期的次数,并且传递给它的参数正确.
I want to draw all line together. Thats why I calling for loop in this loop there I''m checking some conditions then draw line but it draws only one line at a time. But functionally it doing right I checked by breakpoint and messagebox but displaying only one line. Please help.
private void drawAll_line()
{
for (int i = 1; i <= Anum; i++)
{
if (!vAnd[i].isPin1Open)
{
if (vAnd[i].VpIn1)
{
System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(Color.Green);
formGraphics.DrawLines(myPen, vAnd[i].pin1PointPath);
myPen.Dispose();
formGraphics.Dispose();
}
else
{
System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(Color.Red);
formGraphics.DrawLines(myPen, vAnd[i].pin1PointPath);
myPen.Dispose();
formGraphics.Dispose();
}
}
if (!vAnd[i].isPin2Open)
{
if (vAnd[i].VpIn2)
{
System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(Color.Green);
formGraphics.DrawLines(myPen, vAnd[i].pin2PointPath);
myPen.Dispose();
formGraphics.Dispose();
}
else
{
System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(Color.Red);
formGraphics.DrawLines(myPen, vAnd[i].pin2PointPath);
myPen.Dispose();
formGraphics.Dispose();
}
}
}
}
The debugger is probably the best way to figure out why the lines aren''t getting drawn as expected. That being said I see some strange things:
First,for (int i = 1; i <= Anum; i++)arrays are 0 based, so shouldn''t that be
for (int i = 0; i < Anum; i++)or better yet,
for (int i = 0; i < vAnd.Length /* or vAnd.Count for a List */; i++)?
Then,panel1.CreateGraphics()
makes me wonder if you''re doing the drawing in the correct place. When drawing on a Control you need to do it in theControl
''sOnPaint
event handler which gives you theGraphics
object to paint on.
Creating and disposing newPen
s every time the method is called is wasteful, you could save some resources by using the staticPens.Red
andPens.Green
objects.
If none of those help find the problem try putting a break point on each call toDrawLines
to make sure that it''s being called the expected number of times and the parameters that are being passed to it are correct.
这篇关于在循环中画多条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!