简体   繁体   English

故障绘图图形

[英]Trouble Drawing Graphics

I am having trouble drawing graphics on a simple form. 我在简单的表格上绘制图形时遇到麻烦。 My code compiles and runs, but the intended red box does not show up. 我的代码可以编译并运行,但是没有出现预期的红色框。

I am using the full .NET Framework 4 in Visual Studio 2010. 我在Visual Studio 2010中使用完整的.NET Framework 4。

What is going wrong, and how can it be fixed? 出了什么问题,如何解决?

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

EDIT: Working But Slow Code 编辑:工作但缓慢的代码

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

You shouldn't do your drawing in the Load event, which is fired before your form is even displayed for the first time. 您不应该在Load事件中绘制图形,该事件甚至在首次显示表单之前就触发了。 The window is repainted when it is displayed, which clears the graphics you've drawn. 该窗口在显示时会重新绘制,以清除绘制的图形。 If you want a persistent rectangle, handle the Paint event instead: 如果要使用永久矩形,请改用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);
}

If you don't want a persistent rectangle (highly doubtful, but still possible), try handling the Shown event instead of the Load event. 如果您希望使用永久矩形(高度可疑,但仍然可行),请尝试处理Shown事件而不是Load事件。

If the graphics displayed will be updated frequently (eg in a game), make sure to set DoubleBuffered to true and override OnPaint instead of handling an event: 如果显示的图形会经常更新(例如在游戏中),请确保将DoubleBuffered设置为true并覆盖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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM