我知道这很简单,但我不明白为什么在点击按钮后Form1上没有画出任何内容:
namespace GeometryTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, PaintEventArgs e)
{
System.Drawing.Graphics gr = this.CreateGraphics();
gr.Clear(Color.White);
Pen pen = new Pen(System.Drawing.Color.Red,3);
gr.DrawLine(pen, 20, 20, 200, 250);
}
}
}
答案 0 :(得分:4)
问题:您在PaintEventArgs
事件中使用Button Click
作为参数。
如果您将button1_Click
事件处理程序订阅到Click
的{{1}}事件,我甚至怀疑这会编译。
解决方案1:
您需要Button
按钮Subscribe/Register
事件,如下所示:
Click
和EventHandler应如下所示:
button1.Click += new System.EventHandler(button1_Click);
完整代码:
private void button1_Click(object sender, EventArgs e)
{
//event handling code here
}
解决方案2:如果您想在Form_Paint事件上绘制一行,您可以订阅namespace GeometryTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += new System.EventHandler(button1_Click);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
System.Drawing.Graphics gr = this.CreateGraphics();
gr.Clear(Color.White);
Pen pen = new Pen(System.Drawing.Color.Red,3);
gr.DrawLine(pen, 20, 20, 200, 250);
}
}
}
事件,如下所示:
您需要Form_Paint
来形成Paint事件,如下所示:
Subscribe/Register
和EventHandler应如下所示:
this.Paint += new System.Windows.Forms.PaintEventHandler(Form1_Paint);
完整代码:
private void Form1_Paint(object sender, PaintEventArgs e)
{
//event handling code here
}