1

我正在用 winform (tic-tac-toe) 制作一个简单的游戏,但在绘制块控制时遇到了一些问题。

这是我制作的类,代表游戏中的一个块(没有游戏逻辑,只有 UI)。

public class UI_Block : Control
{
    private Rectangle block;
    private SIGNS sign;
    public  SIGNS Sign 
    {
        get {return sign;}
        set
        {
            if (sign == SIGNS.EMPTY)
                sign = value;
        } 
    }

    public UI_Block( ) {
        sign = SIGNS.EMPTY;
    }

    public void SetBlockOnBoard(int x, int y)
    {
        this.Location = new Point( x , y );
        this.Size = new Size(Parent.Width /3, Parent.Height / 3);

        block = new Rectangle(this.Location, this.Size);
    }

    public void DrawSign(Graphics g)
    {
        Pen myPen = new Pen(Color.Red);

        if (sign == SIGNS.O)
        {
            drawO(g,new Pen(Brushes.Black));
        }

        if (sign == SIGNS.X)
        {
            drawX(g, new Pen(Brushes.Red));
        }

    }

    protected override void OnPaint(PaintEventArgs e)
    {
        DrawSign(e.Graphics);
        base.OnPaint(e);
    }

    //Draw X
    private void drawX(Graphics g, Pen myPen)
    {
        //draw first daignol
        Point daignolStart = new Point {  X = this.Location.X  ,  Y = this.Location.Y   };
        Point daignolEnd = new Point   {  X = this.Size.Width  ,  Y = this.Size.Height  };

        g.DrawLine(myPen, daignolStart, daignolEnd);

        //draw second daignol
        daignolStart = new Point {  X = Size.Width ,  Y = this.Location.Y  };
        daignolEnd = new Point   {  X = Location.X,   Y = Size.Height      };

        g.DrawLine(myPen, daignolEnd, daignolStart);
    }


    //Draw O
    private void drawO(Graphics g, Pen myPen)
    {
        g.DrawEllipse(myPen, block);
    }

}

我将它们都添加到 winForm 类中,并查看绘制它们时的样子:

public partial class Form1 : Form
{
    UI.UI_Block block;
    UI.UI_Block blockX;

    public Form1()
    {
        InitializeComponent();

        block = new UI.UI_Block();
        blockX = new UI.UI_Block();
        Controls.Add(block);
        Controls.Add(blockX);

    }

    protected override void OnLoad(EventArgs e)
    {
        block. SetBlockOnBoard(0, 0);
        blockX.SetBlockOnBoard(0, block.Height);

        block.Sign = SIGNS.X;
        blockX.Sign = SIGNS.O;

        base.OnLoad(e);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        //block.DrawSign(e.Graphics);
        //block.DrawSign(e.Graphics);
        base.OnPaint(e);
    }

}

我尝试了一些事情,比如不使用该onPaint事件,但我仍然得到相同的结果。

这是我运行它时看到的内容:

enter image description here 知道为什么我不能画他们两个吗?

4

1 回答 1

2

您没有在其可见区域中绘制控件的内容,因此它绘制得很好,但您看不到它。

Every control has it's own coordinate space (client coords), which starts at 0,0 regardless of where it is positioned within the parent control. You are placing the control in it's parent correctly by setting its Location, but then you are also using the Location to offset the graphics, so they are essentially offset twice.

(If you make your control bigger you'll be able to see the X being drawn further down the screen)

To fix this, do all your drawing in the client coordinate space of your control, i.e. draw in the area (0, 0, width, height)

(P.S. You could just draw all 9 tiles in the parent control, which is a more efficient approach than creating 9 child controls. But what you are doing will work fine)

于 2013-03-30T15:01:21.927 回答