저는 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);
}
}
, CreateGraphics 해당를 사용하지 않는 대신 Paint 이벤트를 사용하지 마십시오. 지금은 윈도우가 보이기 전에 생성자에서 픽셀을 튀기는 중입니다. Sine1.Show() 호출도 없으므로 결코 보이지 않습니다. Windows Forms에 대한 소개 책인 잘못된 설명이 많이 있습니다. 한 번없이 버그를 놓고 싸우는 것이 좋습니다. –