我在尝试在面板上绘制矩形时遇到问题。我创建了一个具有执行此功能的类,但是当我调用它时没有任何反应。当我使用函数中的代码而不是实际函数时,它可以工作。有任何想法吗?这是代码
class Snake : Form1
{
public static int x = 20;
public static int y = 20;
public static int r = 20;
public void Draw()
{
SolidBrush brush = new SolidBrush(Color.Green);
Graphics G = panel1.CreateGraphics();
G.FillRectangle(brush, x, y, r, r);
}
}
//...
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
Snake s = new Snake();
s.Draw();
}
答案 0 :(得分:2)
Snake对象在其自己的图形上下文中绘制,这与您单击的面板的上下文不同。要解决此问题,面板必须为<table class="tableau">
<thead>
<tr><th>xxx</th><th><?=implode("</th><th>",array_column($lesFraisForfait, "libelle"))?></th></tr>
</thead>
<tbody>
<tr><td>id frais</td><td><?=implode("</td><td>",array_column($lesFraisForfait, "idfrais"))?></td></tr>
<tr><td>quantité</td><td><?=implode("</td><td>",array_column($lesFraisForfait, "quantite"))?></td></tr>
</tbody>
</table>
函数提供图形:
Draw
在相关说明中,请务必始终处置class Snake
{
public static int x = 20;
public static int y = 20;
public static int r = 20;
public void Draw(Graphics G)
{
SolidBrush brush = new SolidBrush(Color.Green);
G.FillRectangle(brush, x, y, r, r);
}
}
//...
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
using (Graphics G = panel1.CreateGraphics())
{
Snake s = new Snake();
s.Draw(G);
}
}
对象。
答案 1 :(得分:1)
实施Panel Paint事件:
class Snake : Form1
{
public static int x = 20;
public static int y = 20;
public static int r = 20;
public void Draw()
{
panel1.Refresh() ;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
SolidBrush brush = new SolidBrush(Color.Green);
Graphics G = panel1.CreateGraphics();
G.FillRectangle(brush, x, y, r, r);
}
}