在循环中画多条线 [英] Draw multiple line in a loop

查看:74
本文介绍了在循环中画多条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将所有线条绘制在一起.这就是为什么我在此循环中调用循环的原因,我在其中检查一些条件,然后画线,但一次只画一条线.但是在功能上我做得不错,我通过断点和消息框进行了检查,但只显示了一行.请帮忙.

 私有 无效 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()让我想知道您是否在正确的位置进行绘制.在控件上绘图时,您需要在ControlOnPaint事件处理程序中执行此操作,该处理程序为您提供要在其上进行绘制的Graphics对象.

每次调用该方法时创建和处置新的Pen都是很浪费的,您可以通过使用静态的Pens.RedPens.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 the Control''s OnPaint event handler which gives you the Graphics object to paint on.

Creating and disposing new Pens every time the method is called is wasteful, you could save some resources by using the static Pens.Red and Pens.Green objects.

If none of those help find the problem try putting a break point on each call to DrawLines 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆