我想繪製一個在c#中的formload上包含PNG的圖片框中的一些橢圓。當我在下面執行代碼時,我會看到我的橢圓半秒鐘,然後我再也看不到它們了。不能在窗體加載繪製橢圓c#
當我點擊我的圖片框時,我可以繪製一個橢圓,但是當我最小化表單時,它們不會再出現。
我讀過你不應該把你的繪圖代碼放在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);
}*/
}
}
不要使用'CreateGraphics'。完全一樣。 –