2017-12-26 14 views
-1

私はC#を初めて使用しており、OOPとクラスを使用しようとしています。私は単純な正弦波と軸線(X軸)を描こうとしています。私は "main - Form1"に似たコードを持っていますが、クラス内からフォームに描画することはできません。それは何も描かない!コードがコンパイルされます。クラス内からラインを描画できない - なぜ描画されませんか?

私には何が欠けていますか?私はもっ​​とうまくいくことができますか?

私は、ボタンのクリックからクラスを呼び出す - ここ

Drawclick(object sender, EventArgs e) 
    { DrawSine Sine1 = new DrawSine(950); 
    } 

は、このフォームに何かを描画することができるが、あなたがあるため、すぐにLoadイベントの後に見ることができないクラス

class DrawSine:Form1 
{ 
     private float sinex=0;//set up variables for sine 
     private float countx = 0; 
     private float siney = 0; 
     private float sinex1=0; 
     private float siney1=0; 
     public float offset; 
     public float scalex; 
     public float scaley; 

     public DrawSine(int widthG) 
     { 
      Graphics Graphsine = this.CreateGraphics();//declare sine 
      Graphics Graphline = this.CreateGraphics();//declare axis 
      Pen Graphpen = new Pen(Brushes.Black, 1.0F); 
      Graphline.DrawLine(Graphpen, 0, 200, widthG, 200);//draw line 
               0,200 to end of form,200 

      int WidthG = widthG; 
      do //draw sine wave left to right 
      { 
       sinex += scalex * 6.28f/widthG; 
       siney = (float)Math.Sin(sinex); 
       Graphsine.DrawLine(Graphpen, sinex1, siney1 + offset, 
            countx, siney * 100 + offset); 
       sinex1 = sinex; 
       sinex1 = siney * 100; 
       countx += 1; 
      } 
      while (countx <= widthG); 

     } 

} 
+1

はcreateGraphicsのは絶対に使用しないでください、代わりにPaintイベントを使用します。現在、ウィンドウが表示される前に、コンストラクタ内のピクセルが飛び散っています。 Sine1.Show()も呼び出されないので、決して表示されません。 Windowsフォームの入門書である間違った点がたくさんありますが、間違いなくバグと戦うことになります。 –

答えて

0

ですフォームがレンダリングされるときにPaintBackgroundイベントがあり、PaintEventイベントが発生します。

これらのイベントは、フォーム上のすべてを効果的に消去します。

2救済:

のOnPaintイベントをオーバーライドして、そこにあなたのコードを記述し、

ワン(のはDrawSine()を言ってみましょう)の方法では上記のドローコードをカプセル化します。あなたの絵はフォームのサイズ変更やフォームの再描画に影響を与えません。

protected override void OnPaint(PaintEventArgs e) 
{ 
    base.OnPaint(e); 
    DrawSine(e, 950); 
} 

、利用形態Shownイベント

private void Form1_Shown(object sender, EventArgs e) 
{ 
    DrawSine(null, 950); 
} 

あなたのオリジナルの方法:

public DrawSine(Graphics g, int widthG) 
{ 
    Graphics Graphsine = g??this.CreateGraphics();//declare sine 
    Graphics Graphline = g??this.CreateGraphics();//declare axis 
    Pen Graphpen = new Pen(Brushes.Black, 1.0F); 
    Graphline.DrawLine(Graphpen, 0, 200, widthG, 200);//draw line 
              //0,200 to end of form,200 

    int WidthG = widthG; 
    do //draw sine wave left to right 
    { 
     sinex += scalex * 6.28f/widthG; 
     siney = (float)Math.Sin(sinex); 
     Graphsine.DrawLine(Graphpen, sinex1, siney1 + offset, 
           countx, siney * 100 + offset); 
     sinex1 = sinex; 
     sinex1 = siney * 100; 
     countx += 1; 
    } 
    while (countx <= widthG); 
} 
+0

助けてくれてありがとう!私はWindowsフォームで本を必要とし、ペイントとCreateGraphicsを使用するように見える - どのような提案?ダウンロード可能なバージョン – EngRick

+0

https://stackoverflow.com/questions/15057406/c-sharp-draw-line-onpaint-vs-creategraphics - 同様の質問にリンクします。簡潔な答えは大きな本より優れている;) – Sunil

関連する問題