gpt4 book ai didi

c# - 无法在 C# 中的表单加载上绘制椭圆

转载 作者:太空宇宙 更新时间:2023-11-03 20:11:05 25 4
gpt4 key购买 nike

我正在尝试在一个图片框中绘制一些椭圆,该图片框在 C# 中的表单加载上包含一个 PNG。当我执行下面的代码时,我看到我的省略号半秒钟,然后我就再也看不到它们了。

当我点击我的图片框时,我可以画一个椭圆,但是当我最小化表格时,它们就不再出现了。

我读到过您不应该将绘图代码放在 formload 中,而应该放在 OnPaint 方法中,我就是这样做的。我不知道该尝试什么了。谢谢。 (请注意,我已经对一些代码进行了注释以显示我的尝试)。

public partial class FormParterre : Form
{
Graphics g;
float circleSize = 15;
//Brushes rouge = new Brushes (Brushes.Red);

ReservationBilletSiegeDAO reservationBilletSiegeDAO = new ReservationBilletSiegeDAO();
SiegeDAO siegeDAO = new SiegeDAO();

List <Siege> sieges;
List<ReservationBilletSiege> rbs;
ReservationBillet reservationBillet = new ReservationBillet();
ReservationBilletSiege reservationBilletSiege;

SolidBrush semiTransBrush;


public FormParterre()
{
InitializeComponent();
pictureBox1.Image = new Bitmap("./parterre.png");
g = pictureBox1.CreateGraphics();


}

public FormParterre(ReservationBillet rb)
{
reservationBillet = rb;
pictureBox1.Image = new Bitmap("./parterre.png");
g = pictureBox1.CreateGraphics();



InitializeComponent();
}

protected override void OnPaintBackground(PaintEventArgs e)
{
// do nothing! prevents flicker
}

protected override void OnPaint(PaintEventArgs e)
{

sieges = siegeDAO.readAll();

rbs = reservationBilletSiegeDAO.readAll();

foreach (ReservationBilletSiege reservationBilletSiegeTMP in rbs)
{
Console.WriteLine(reservationBilletSiegeTMP.toString());
int x = siegeDAO.read(reservationBilletSiegeTMP.idSiege).xValeur;
int y = siegeDAO.read(reservationBilletSiegeTMP.idSiege).yValeur;
float xx = (float)x;
float yy = (float)y;
Console.WriteLine("le x: " + xx);
Console.WriteLine("le y: " + yy);


/*e.Graphics.FillRectangle(new SolidBrush(BackColor), e.ClipRectangle);*/
g.FillEllipse(new SolidBrush(Color.FromArgb(128, 0, 0, 255)), xx - circleSize / 2, yy - circleSize / 2, circleSize, circleSize);
}

}

private void pictureBox1_Click(object sender, EventArgs e)
{
MouseEventArgs me = (MouseEventArgs)e;
txtX.Text = me.X.ToString();
txtY.Text = me.Y.ToString();

Console.WriteLine("click");

g.FillEllipse(new SolidBrush(Color.FromArgb(128, 0, 0, 255)), float.Parse(txtX.Text) - circleSize / 2, float.Parse(txtY.Text) - circleSize / 2, circleSize, circleSize);





}

private void FormParterre_Shown(object sender, EventArgs e)
{

}

private void FormParterre_Load(object sender, EventArgs e)
{

/*sieges = siegeDAO.readAll();
//semiTransBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 255));



rbs = reservationBilletSiegeDAO.readAll();

foreach (ReservationBilletSiege reservationBilletSiegeTMP in rbs)
{
Console.WriteLine(reservationBilletSiegeTMP.toString());
int x = siegeDAO.read(reservationBilletSiegeTMP.idSiege).xValeur;
int y = siegeDAO.read(reservationBilletSiegeTMP.idSiege).yValeur;
float xx = (float)x;
float yy = (float)y;
Console.WriteLine("le x: " + xx);
Console.WriteLine("le y: " + yy);

g.FillEllipse(new SolidBrush(Color.FromArgb(128, 0, 0, 255)), xx - circleSize / 2, yy - circleSize / 2, circleSize, circleSize);
}*/
}


}

最佳答案

您需要使用传递给您的 PaintEventArgs.Graphics 属性在 OnPaint 方法中进行绘制。如果您想在 PictureBox 上绘画,您可以尝试订阅它的 Paint 事件并在那里绘画。

这对我有用:

public void Form1()
{
InitializeComponent();

pictureBox1.Paint += pictureBox1_Paint;
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillEllipse(Brushes.Red, pictureBox1.ClientRectangle);
}

关于c# - 无法在 C# 中的表单加载上绘制椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20011079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com