2013-11-15 78 views
0

我想繪製一個在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); 
     }*/ 
    } 


} 
+4

不要使用'CreateGraphics'。完全一樣。 –

回答

3

您需要使用它傳遞給你的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); 
} 
+0

這聽起來很愚蠢,但我該如何去「使用Visual Studio Designer訂閱PictureBox Paint事件」? – Eric

+1

選擇圖片框。在屬性窗格中(默認情況下右下角),點擊「閃電箭」圖標。找到「Paint」條目並雙擊它右側的空白處。 –

+1

編輯也顯示訂閱事件的基於代碼的方式。 –