2016-12-21 7 views
-1

EDIT:これは、「C#でOnPaintイベントを使用する方法」の複製ではありません。私は答えを知らないときに質問をしています。それは、ディップ・スティックが何であるか分からないときに、「私の車のオイルをチェックするにはどうすればいいか」と尋ねるようなものです。重複した記号が重複していない理由を説明する方法でそれを編集する場合は、重複する記号を削除してください。コンストラクタ内のフォームのペイントでフォームに何も表示されないのはなぜですか?

ランダムテレインジェネレータを作成しようとしています。現時点では、Windows Forms Graphicsを使用してすべてを描画しています。私はエンジンが動作するように見えることはできません。次のコードを実行すると、0,0から50,50までの黒い四角形を描画する必要がありますが、フォームには何も表示されません。誰でも解決できますか?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Random_Terrain_Generator 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      Engine e = new Engine(100, 100); 
      e.Color3(Color.Blue); 
      e.rect(new Rectangle(0,0,50,50)); 
      Graphics fg = this.CreateGraphics(); 
      fg.DrawImage(e.getBuffer(), 1, 100, 100, 100); 
     } 
    } 

    public class Engine 
    { 
     Color fillColor; 
     Bitmap buffer; 
     Graphics bufferGraphics; 

     public Engine(int bufferWidth, int bufferHeight) 
     { 
      buffer = new Bitmap(bufferWidth, bufferHeight); 
      bufferGraphics = Graphics.FromImage(buffer); 
     } 

     public void fRect(Rectangle r) 
     { 
      bufferGraphics.FillRectangle(new SolidBrush(fillColor), r); 
     } 

     public void rect(Rectangle r) 
     { 
      bufferGraphics.DrawRectangle(new Pen(fillColor), r); 
     } 

     public void Color3(Color c) 
     { 
      fillColor = c; 
     } 

     public Bitmap getBuffer() 
     { 
      return buffer; 
     } 
    } 
} 
+2

すべてをフォームコンストラクタで行いました。フォームがペイントされると、あなたの箱は消えてしまいます。フォームは、視覚的に更新する必要があると感じると、常に再描画されます。つまり、毎回ペイントする必要があります。 http://stackoverflow.com/questions/10076116/how-to-use-the-onpaint-event-in-c – TyCobb

+0

私はそれをフォームロードイベントに入れると助けになるのでしょうか?私はinitializeConponentの後にそれをやっているので、フォームの後にペイントすると仮定します。 –

+1

いいえ、OnPaintメソッドをオーバーライドしなければならないので、ペイントが終了したらペイントできます。私がリンクしている例を見てください。 – TyCobb

答えて

1

これは機能しました。迅速な回答のためにTyCobbに感謝します。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Random_Terrain_Generator 
{ 
    public partial class Form1 : Form 
    { 
     Engine e; 

     public Form1() 
     { 
      InitializeComponent(); 

      e = new Engine(100, 100); 
      e.Color3(Color.Blue); 
      e.fRect(new Rectangle(0,0,50,50)); 
     } 

     protected override void OnPaint(PaintEventArgs pe) 
     { 
      // Call the OnPaint method of the base class. 
      base.OnPaint(pe); 
      pe.Graphics.DrawImage(e.getBuffer(), 1, 1, 100, 100); 

     } 
    } 

    public class Engine 
    { 
     Color fillColor; 
     Bitmap buffer; 
     Graphics bufferGraphics; 

     public Engine(int bufferWidth, int bufferHeight) 
     { 
      buffer = new Bitmap(bufferWidth, bufferHeight); 
      bufferGraphics = Graphics.FromImage(buffer); 
     } 

     public void fRect(Rectangle r) 
     { 
      bufferGraphics.FillRectangle(new SolidBrush(fillColor), r); 
     } 

     public void rect(Rectangle r) 
     { 
      bufferGraphics.DrawRectangle(new Pen(fillColor), r); 
     } 

     public void Color3(Color c) 
     { 
      fillColor = c; 
     } 

     public Bitmap getBuffer() 
     { 
      return buffer; 
     } 
    } 
} 

フォームのOnPaintイベントをオーバーライドして、フレームごとに矩形を再描画します。問題は、フォームが長方形にペイントしていたことでした。 How to use the OnPaint event in C#?

+2

'fg'は必要ありません。 'PainteEventArgs'にはあなたのための' Graphics'があります。うれしいことはあなたのために働いた。 – TyCobb

+1

ええ、一般的なルールとして、CreateGraphics()を使ってコントロールを描画することはありません。有効期間は無期限にとどまることはありません。代わりに 'PaintEventArgs.Graphics'プロパティを必ず使用してください。 – Blorgbeard

+0

よろしくお願いします。ありがとう! –

関連する問題