如何使用条件隐藏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, 160);
myPen.Dispose();
formGraphics.Dispose();
我希望隐藏条件formGraphics.DrawLine(myPen, 108, 272, 153, 160);
的绘制线if (x > 1) {} else if (x = 1) {}
再次显示
答案 0 :(得分:2)
通过设计器或表单构造函数订阅Form.Paint
事件。然后在paint事件处理程序中执行条件绘制。还要将x变量转换为属性(公共或私有),并在表单更改时调用Invalidate
。这将导致表单重绘,调用paint事件。
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);
}
}