Groupbox重绘问题

时间:2012-11-16 01:40:29

标签: c# paint redraw

我觉得我已经在互联网上搜索答案,但我仍然没有解决这个问题。 我正在用C#构建一个刽子手游戏(我还是新手)并且我在我的表单上有一个groupbox,我正在绘制不同的“身体部位”。问题是每次调用draw事件时都会重绘它。所以我最终会得到一个头,然后只是一条胳膊,一条腿等等。

我已经尝试过覆盖OnPaintBackground这样的事情,虽然这解决了重绘的问题,但它会搞砸整个表单,当我这样做时它看起来很糟糕。 我也尝试绘制一个面板而不是一个组合框,但由于某种原因我甚至无法得到它。

有人可以提出任何建议吗? 顺便说一句,这不是个人作业,只是个人项目和学习经历。

一些代码:这就是我用来触发绘制功能的东西(它在我拥有的其他条件下触发)

this.groupBoxHang.Paint += new System.Windows.Forms.PaintEventHandler(this.groupBoxHangMan_Paint);

然后它来到这里,它调用实际的函数,它在一个单独的类

    private void groupBoxHangMan_Paint(object sender, PaintEventArgs e)
    {          
       draw.DrawHangMan(e.Graphics, GetRemainingGuess());      
    }

以下是其他课程的一部分:

    public void DrawHangMan(Graphics dM, int guessesRemaining)
    {        
        Pen blackPen = new Pen(Brushes.Black);

        Pen deathPen = new Pen(Brushes.Red);
        deathPen.Width = 2.0F;

        if (guessesRemaining == 7)
        {
            dM.DrawEllipse(blackPen, 94, 75, 60, 60);//head
        }
        else if (guessesRemaining == 6)
        {
            dM.DrawLine(blackPen, 124, 210, 124, 135);//body
        }
        else if (guessesRemaining == 5)
        {
            dM.DrawLine(blackPen, 123, 170, 65, 145);//left arm
        }
        else if (guessesRemaining == 4)
        {
            dM.DrawLine(blackPen, 123, 170, 181, 145); //right arm
        }
        else if (guessesRemaining == 3)
        {
            dM.DrawLine(blackPen, 123, 210, 65, 255);//left leg
        }
        else if (guessesRemaining == 2)
        {
            dM.DrawLine(blackPen, 123, 210, 181, 255);//right leg
        }
        else if (guessesRemaining == 1)
        {
            dM.DrawEllipse(blackPen, 105, 93, 10, 10); //left open eye
            dM.DrawEllipse(blackPen, 133, 93, 10, 10); //right open eye
        }
        else if (guessesRemaining == 0)
        {
            dM.DrawLine(deathPen, 102, 104, 118, 92);//left eye dead
            dM.DrawLine(deathPen, 118, 104, 102, 92);

            dM.DrawLine(deathPen, 130, 104, 146, 92);//right dead eye
            dM.DrawLine(deathPen, 146, 104, 130, 92);
        }         

    }

1 个答案:

答案 0 :(得分:0)

你的If语句有两件事。它们被设置为仅在等于计数时激活,以便在计数小于阈值时不再重绘,第二个是使用else语句只运行其中一个级别。在Paint事件中执行此操作是正确的。尝试这样的事情:

public void DrawHangMan(Graphics dM, int guessesRemaining)
{
    Pen blackPen = new Pen(Brushes.Black);
    Pen deathPen = new Pen(Brushes.Red);
    deathPen.Width = 2.0F;

    if (guessesRemaining < 8)
    {
        dM.DrawEllipse(blackPen, 94, 75, 60, 60);//head
    }
    if (guessesRemaining < 7)
    {
        dM.DrawLine(blackPen, 124, 210, 124, 135);//body
    }
    if (guessesRemaining < 6)
    {
        dM.DrawLine(blackPen, 123, 170, 65, 145);//left arm
    }
    if (guessesRemaining < 5)
    {
        dM.DrawLine(blackPen, 123, 170, 181, 145); //right arm
    }
    if (guessesRemaining < 4)
    {
        dM.DrawLine(blackPen, 123, 210, 65, 255);//left leg
    }
    if (guessesRemaining < 3)
    {
        dM.DrawLine(blackPen, 123, 210, 181, 255);//right leg
    }
    if (guessesRemaining < 2)
    {
        dM.DrawEllipse(blackPen, 105, 93, 10, 10); //left open eye
        dM.DrawEllipse(blackPen, 133, 93, 10, 10); //right open eye
    }
    if (guessesRemaining < 1)
    {
        dM.DrawLine(deathPen, 102, 104, 118, 92);//left eye dead
        dM.DrawLine(deathPen, 118, 104, 102, 92);
        dM.DrawLine(deathPen, 130, 104, 146, 92);//right dead eye
        dM.DrawLine(deathPen, 146, 104, 130, 92);
    }

}