Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何隐藏系统。使用条件绘制线_C#_Winforms_System.drawing - Fatal编程技术网

C# 如何隐藏系统。使用条件绘制线

C# 如何隐藏系统。使用条件绘制线,c#,winforms,system.drawing,C#,Winforms,System.drawing,如何隐藏系统。绘制带条件的线条: System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.White); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.DrawLine(myPen, 108, 272, 153, 1

如何隐藏系统。绘制带条件的线条:

    System.Drawing.Pen myPen;            
    myPen = new System.Drawing.Pen(System.Drawing.Color.White);           
    System.Drawing.Graphics formGraphics = this.CreateGraphics();

    formGraphics.DrawLine(myPen, 108, 272, 153, 160);  

    myPen.Dispose();
    formGraphics.Dispose();

我想隐藏绘制的线条
formGraphics.DrawLine(myPen,108272153160)如果(x>1){}否则如果(x=1){}
再次显示它

通过设计器或表单构造函数订阅
表单.Paint
事件。然后在绘制事件处理程序中执行条件绘制。还可以将x变量设置为属性(公共或私有),并在其更改时在窗体上调用
Invalidate
。这将导致窗体重新绘制,并调用绘制事件

private int x = 0;

public int X
{
    get
    {
        return x;
    }

    set
    {
        x = value;
        // Cause the form to be redrawn.
        this.Invalidate();
    }
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.Clear(Color.Black);

    // Only draw the line if x == 1.
    if (x == 1)
    {
        e.Graphics.DrawLine(Pens.White, 108, 272, 153, 160);
    }
}

通过设计器或表单构造函数订阅
Form.Paint
事件。然后在绘制事件处理程序中执行条件绘制。还可以将x变量设置为属性(公共或私有),并在其更改时在窗体上调用
Invalidate
。这将导致窗体重新绘制,并调用绘制事件

private int x = 0;

public int X
{
    get
    {
        return x;
    }

    set
    {
        x = value;
        // Cause the form to be redrawn.
        this.Invalidate();
    }
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.Clear(Color.Black);

    // Only draw the line if x == 1.
    if (x == 1)
    {
        e.Graphics.DrawLine(Pens.White, 108, 272, 153, 160);
    }
}

不要使用CreateGraphics,因为这是临时的-使用容器的paint事件中的图形。要删除一行,请重新绘制除该行以外的所有内容。如果您在winforms中,则应在
窗体中重复绘制这些内容。绘制
事件,因此只需刷新,在事件中,这次不要绘制该行。不要使用CreateGraphics,因为这是临时的-使用容器绘制事件中的图形。若要删除一条线,请重新绘制该线以外的所有内容。如果您在winforms中,则应在
窗体中重复绘制这些内容。绘制
事件,因此只需刷新,在事件中,这次不绘制该线。您好,但是如果我希望每次都隐藏并淹没同一条线,如果x=1淹没,如果x=0隐藏,然后,如果x=1,则再次溺水等。对于顺序x切换为1和0的同一行,请参见编辑:在x发生变化时在表单上调用Invalidate。非常好的解决方案,正是我需要的,谢谢!似乎我只需要public int X IvalidateHello,但是如果我想每次隐藏并淹没同一行,如果X=1淹没,如果X=0隐藏,然后再次如果X=1淹没等。对于顺序X切换为1和0的同一行,请参见编辑:在X更改时在表单上调用Invalidate。非常好的解决方案,正是我需要的,谢谢!似乎我只需要公共int X Ivalidate