2017-12-26 33 views
-1

저는 C#에 익숙하지 않으며 OOP와 클래스를 사용하려고합니다. 간단한 사인파와 축선 (X 축)을 그리려합니다. 나는 "main - Form1"에서 유사한 코드를 얻었지만 클래스 내에서 폼을 그리는 것은 불가능합니다. 그것은 아무것도 가져 오지 않는다! 코드가 컴파일됩니다.클래스 내에서 선을 그릴 수 없음 - 왜 그리지 않습니까?

무엇이 누락 되었습니까? 내가 더 잘할 수 있을까?

내가 버튼에서 클래스를 클릭 전화 -

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

클래스

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 Forms에 대한 소개 책인 잘못된 설명이 많이 있습니다. 한 번없이 버그를 놓고 싸우는 것이 좋습니다. –

답변

0

이 양식에 무언가를 그리기 할 수 있습니다하지만 당신은 볼 수 없습니다 곧 Load 이벤트 때문에, 양식이 렌더링 될 때 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 Forms에 대한 책이 필요하고 그림판 대 CreateGraphics를 사용하는 것처럼 보입니다 - 어떤 제안이 있습니까? 다운로드 할 수있는 버전은 무엇입니까? – EngRick

+0

https://stackoverflow.com/questions/15057406/c-sharp-draw-line-onpaint-vs-creategraphics - 비슷한 질문으로 연결하십시오. 간결한 답변은 큰 책보다 낫다;) – Sunil