2012-05-28 94 views
0

我无法在简单的窗体上绘制图形。我的代码编译并运行,但预期的红色框不显示。故障绘图图形

我使用的是完整的.NET Framework 4的Visual Studio 2010中

到底哪里出问题了,怎么可以把它固定?

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); 
} 

如果想要一个持久的矩形(非常令人怀疑,但还是有可能),尝试处理Shown事件而不是Load事件。

如果显示会经常更新的图形(例如游戏),请务必将DoubleBufferedtrue和覆盖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