从Windows窗体单击一个按钮不起作用。
因为,我必须使用参数“ Graphics g ...”作为方法。我无法更改方法,因此,当用户单击按钮时,我必须更改某些内容。
下面是代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//protected override void OnPaint(PaintEventArgs e)
//{
// Graphics g = e.Graphics;
// TekenCirkel(g, 50, 50, 100, 100);
// TekenRechthoek(g, 0, 0, 100, 100);
//}
public void TekenCirkel(Graphics g, int x, int y, int w, int h) {
Pen cirkel = new Pen(Color.Blue, 2);
g.DrawEllipse(cirkel, x,y, w, h);
}
public void TekenRechthoek(Graphics g, int x, int y, int w, int h){
Pen rechthoek = new Pen(Color.Black, 2);
g.DrawRectangle(rechthoek, x, y, w, h);
}
private void Cirkel_Click(object sender, EventArgs e)
{
TekenCirkel(g, 50, 50, 100, 100);
}
}
如您所见,我使用onPaint方法测试了它,并定义了g的作用。使用此方法时,代码有效。但这在尝试单击按钮时是不可能的。因为EventArgs和PaintEventArgs是不同的东西。
E:您单击一个按钮,并且Cirkel / square被画在表单上。我想知道如何调用我创建的绘制Cirkel /正方形的方法。
最佳答案
那不是绘画的原理。
您必须仅使用OnPaint()
绘画所有内容,并使用类中的数据结构跟踪要绘画的内容。
然后,如果要更改它,请调用Invalidate()
使其重新绘制。