我正在尝试在 c# 中的表单加载上包含 PNG 的图片框中绘制一些椭圆。当我执行下面的代码时,我看到我的省略号有半秒钟,然后我就看不到它们了。
当我单击我的图片框时,我可以绘制一个椭圆但是,当我最小化表单时,它们不再出现。
我读过你不应该把你的绘图代码放在表单加载中,而是放在 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);
}*/
}
}