2012-05-28 3 views
0

간단한 양식으로 그래픽을 그리는 데 문제가 있습니다. 내 코드가 컴파일되고 실행되지만 의도 한 빨간색 상자가 표시되지 않습니다.트러블 드로잉 그래픽

나는 2010 년

가 무엇이 잘못 가고 Visual Studio에서 전체 .NET 프레임 워크 4를 사용하고, 그것을 어떻게 해결 될 수 있는가?

private void Form1_Load(object sender, EventArgs e) 
{ 
    System.Drawing.Graphics graphicsObj; 
    graphicsObj = this.CreateGraphics(); 
    Pen myPen = new Pen(System.Drawing.Color.Red, 5); 
    Rectangle myRectangle = new Rectangle(20, 20, 250, 200); 
    graphicsObj.DrawRectangle(myPen, myRectangle); 
} 

편집 : 작동하지만 느린 코드

void BatteryPaint(object sender, EventArgs e) 
    { 
     TabPage page = (TabPage)sender; 
     if (!controlsSetup) 
     { 
      populateBatteryTab(page); 
      controlsSetup = true; 
     } 
     //Create the items 
     Rectangle busBar = new Rectangle(); 
     Rectangle batPack = new Rectangle(); 
     Rectangle pack1Outline = new Rectangle(); 
     Rectangle pack2Outline = new Rectangle(); 
     Rectangle pack3Outline = new Rectangle(); 
     Rectangle pack4Outline = new Rectangle(); 

     Color GreenZone = Color.FromArgb(150, 0, 255, 0); 
     Color YellowZone = Color.FromArgb(150, 255, 255, 0); 
     Color RedZone = Color.FromArgb(150, 255, 0, 0); 
     Color greyZone = Color.FromArgb(200, 200, 200, 200); 

     Graphics graphicControl = page.CreateGraphics(); 

     SolidBrush busBarBrush = new SolidBrush(Color.Peru); 
     SolidBrush GreenBrush = new SolidBrush(GreenZone); 
     SolidBrush GreyBrush = new SolidBrush(greyZone); 

     Pen packPen = new Pen(Color.LightGray, (float)8); 


     Point busBarTop = new Point(page.Width/64, page.Height/32); 
     Point busBarBottom = new Point(busBarTop.X, busBarTop.Y + page.Height/6); 

     //change the properties 
     //Bus Bar Top 
     busBar.Width = page.Width*153/640; 
     busBar.Height = page.Height/64; 
     busBar.Location = busBarTop; 
     graphicControl.FillRectangle(busBarBrush, busBar); 

     //Bus Bar Bottom 
     busBar.Location = busBarBottom; 
     graphicControl.FillRectangle(busBarBrush, busBar); 



     //Pack 1 
     batPack.Width = page.Width/20; 
     batPack.Height = (busBarBottom.Y + busBar.Height) - busBarTop.Y; 
     batPack.Location = new Point(busBarTop.X + page.Width/packSpacingMultiplier, busBarTop.Y); 
     pack1Outline.Width = batOutlineWidth; 
     graphicControl.FillRectangle(GreenBrush, batPack); 
     pack1Outline.Height = (3 * (Battery.Width + page.Width/batSpacingMultiplier) + page.Width/batSpacingMultiplier); 
     pack1Outline.Location = new Point(BatPack1.X - (page.Width/batSpacingMultiplier), BatPack1.Y - (page.Width/batSpacingMultiplier)); 
     for(int numBats = 0; numBats < 30; numBats++) 
     { 
      Battery.Location = new Point(BatPack1.X + ((numBats % 10) * (Battery.Width + page.Width/batSpacingMultiplier)), BatPack1.Y + ((numBats/10) * (Battery.Width + page.Width/batSpacingMultiplier))); 
      graphicControl.FillEllipse(new SolidBrush(BatteryZone(0.00)), Battery); 
     } 

     //Pack 2 
     batPack.Location = new Point(batPack.Location.X + batPack.Width + page.Width/packSpacingMultiplier, batPack.Location.Y); 
     graphicControl.FillRectangle(GreenBrush, batPack); 
     pack2Outline.Width = batOutlineWidth; 
     pack2Outline.Height = (3 * (Battery.Width + page.Width/batSpacingMultiplier) + page.Width/batSpacingMultiplier); 
     pack2Outline.Location = new Point(BatPack2.X - (page.Width/batSpacingMultiplier), BatPack2.Y - (page.Width/batSpacingMultiplier)); 
     for(int numBats = 0; numBats < 30; numBats++) 
     { 
      Battery.Location = new Point(BatPack2.X + ((numBats % 10) * (Battery.Width + page.Width/batSpacingMultiplier)), BatPack2.Y + ((numBats/10) * (Battery.Width + page.Width/batSpacingMultiplier))); 
      graphicControl.FillEllipse(new SolidBrush(BatteryZone(0.00)), Battery); 
     } 

     //Pack 3 
     batPack.Location = new Point(batPack.Location.X + batPack.Width + page.Width/packSpacingMultiplier, batPack.Location.Y); 
     graphicControl.FillRectangle(GreenBrush, batPack); 
     pack3Outline.Width = batOutlineWidth; 
     pack3Outline.Height = (3 * (Battery.Width + page.Width/batSpacingMultiplier) + page.Width/batSpacingMultiplier); 
     pack3Outline.Location = new Point(BatPack3.X - (page.Width/batSpacingMultiplier), BatPack3.Y - (page.Width/batSpacingMultiplier)); 
     for(int numBats = 0; numBats < 30; numBats++) 
     { 
      Battery.Location = new Point(BatPack3.X + ((numBats % 10) * (Battery.Width + page.Width/batSpacingMultiplier)), BatPack3.Y + ((numBats/10) * (Battery.Width + page.Width/batSpacingMultiplier))); 
      graphicControl.FillEllipse(new SolidBrush(BatteryZone(0.00)), Battery); 
     } 

     //Pack 4 
     batPack.Location = new Point(batPack.Location.X + batPack.Width + page.Width/packSpacingMultiplier, batPack.Location.Y); 
     graphicControl.FillRectangle(GreyBrush, batPack); 
     pack4Outline.Width = batOutlineWidth; 
     pack4Outline.Height = (3 * (Battery.Width + page.Width/batSpacingMultiplier) + page.Width/batSpacingMultiplier); 
     pack4Outline.Location = new Point(BatPack4.X - (page.Width/batSpacingMultiplier), BatPack4.Y - (page.Width/batSpacingMultiplier)); 
     for(int numBats = 0; numBats < 30; numBats++) 
     { 
      Battery.Location = new Point(BatPack4.X + ((numBats % 10) * (Battery.Width + page.Width/batSpacingMultiplier)), BatPack4.Y + ((numBats/10) * (Battery.Width + page.Width/batSpacingMultiplier))); 
      graphicControl.FillEllipse(new SolidBrush(BatteryZone(0.00)), Battery); 
     } 


     //add the controls 
     graphicControl.DrawRectangle(packPen, pack1Outline); 
     graphicControl.DrawRectangle(packPen, pack2Outline); 
     graphicControl.DrawRectangle(packPen, pack3Outline); 
     graphicControl.DrawRectangle(packPen, pack4Outline); 
    } 
+0

C#에서 문제 그리기 그래픽으로 이름을 변경하세요 전자가 분명히 가능하기 때문입니다. – Jay

+0

@Jay : 제목에 태그를 지정하면 안됩니다. – Ryan

+1

* 20 초 이상 창을 업데이트하는 게임 루프를 작성하지 않는 한 CreateGraphics()를 사용하지 마십시오. Windows에서 묻는대로 그림을 그리려면 항상 Paint 이벤트를 구현하십시오. –

답변

5

당신은 당신의 양식도 처음으로 표시되기 전에 해고 된 Load 이벤트에 드로잉을해서는 안된다. 윈도우가 표시되면 다시 그려 지므로 그린 그래픽이 지워집니다. 당신이 영구적 인 사각형을 원하는 경우, 대신 Paint 이벤트를 처리 :

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    System.Drawing.Graphics graphicsObj = e.Graphics; 
    Pen myPen = new Pen(System.Drawing.Color.Red, 5); 
    Rectangle myRectangle = new Rectangle(20, 20, 250, 200); 
    graphicsObj.DrawRectangle(myPen, myRectangle); 
} 

당신이하지을 경우 (가능한 여전히 매우 의심하지만) 지속적인 사각형을 원하는 대신 Load 이벤트의 Shown 이벤트를 처리 시도 .

자주 업데이트됩니다 표시되는 그래픽 (예 : 게임), trueDoubleBuffered을 설정하고 이벤트 처리 대신 OnPaint을 무시해야합니다 경우

protected override void OnPaint(PaintEventArgs e) 
{ 
    Graphics g = e.Graphics; 
    Pen myPen = new Pen(Color.Red, 5); 
    g.DrawRectangle(myPen, 20, 20, 250, 200); 
} 
+0

고맙습니다. 잘 작동하지만 100 개 개체의 경우 약 .2 초가 걸립니다. – Reid

+0

@Reid : 어떻게 그 물체를 그리는거야? – Ryan

+0

'drawElipse'를 사용하여 하나의 직사각형 객체를 사용 100 번 – Reid