在用户控件中绘制图像

本文关键字:绘制 图像 控件 用户 | 更新日期: 2023-09-27 18:24:30

我试图制作一个Connect Four游戏,但我遇到了问题。我想让洗衣店的秋天充满活力。这是我的代码:

public class BoardA : UserControl
{
    private static int xSize = 7, ySize = 6;
    private int slotWidth, slotHeight;
    private SlotState[,] states;
    private int currentColumn;
    private bool paintBoard;
    public BoardA()
    {
        states = new SlotState[xSize, ySize];
        slotWidth = 70;
        slotHeight = 70;
        currentColumn = -1;
        MouseMove += new MouseEventHandler(BoardA_MouseMove);
        MouseLeave += new EventHandler(BoardA_MouseLeave);
        Click += new EventHandler(BoardA_Click);
        paintBoard = true;
    }
    void BoardA_Click(object sender, EventArgs e)
    {
        if (ColumnClick != null)
            ColumnClick(this, new ColumnClickedEventArgs(currentColumn));
    }
    void BoardA_MouseLeave(object sender, EventArgs e)
    {
        this.CreateGraphics().FillRectangle(new SolidBrush(Color.Transparent), currentColumn * slotWidth, 0, slotWidth, slotHeight);
        currentColumn = -1;
    }
    void BoardA_MouseMove(object sender, MouseEventArgs e)
    {
        int newColumn = e.X / slotWidth;
        if (newColumn != currentColumn)
        {
            Graphics g = this.CreateGraphics();
            g.FillRectangle(new SolidBrush(Color.Transparent), currentColumn * slotWidth, 0, slotWidth, slotHeight);
            currentColumn = newColumn;
            g.DrawImage(Resources.Down_arrow, currentColumn * slotWidth, 0, slotWidth, slotHeight);
        }
    }

    #region functions paint
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if (paintBoard)
        {
            for (int i = 0; i < xSize; i++)
            {
                for (int j = 0; j < ySize; j++)
                {
                    DrawSlot(states[i, j], i, j);
                }
            }
        }
        e.Graphics.DrawImage(Resources.Down_arrow, currentColumn * slotWidth, 0, slotWidth, slotHeight);
    }
    public void UpdateGraphic()
    {
        RecreateHandle();
    }
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        base.OnPaintBackground(e);
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;
            return cp;
        }
    }
    #endregion
    public SlotState this[int columnIndex, int rowIndex]
    {
        get
        {
            return states[columnIndex, rowIndex];
        }
        set
        {
            states[columnIndex, rowIndex] = value;
            DrawSlot(value, columnIndex, rowIndex);
        }
    }
    public Size BoardSize
    {
        get { return new Size(slotWidth, slotHeight); }
    }
    public event ColumnClickedEventHandler ColumnClick;
    public bool IsFull
    {
        get
        {
            foreach (var item in states)
            {
                if (item == SlotState.Empty)
                    return false;
            }
            return true;
        }
    }
    public void Clear()
    {
        for (int i = 0; i < xSize; i++)
        {
            for (int j = 0; j < ySize; j++)
            {
                this[i, j] = SlotState.Empty;
            }
        }
    }
    public bool EnterToColumn(int Column, SlotState state)
    {
        if (Column < 0 || Column >= xSize)
            throw new ArgumentOutOfRangeException("Column");
        if (state == SlotState.Empty)
            return false;
        Graphics g = this.CreateGraphics();
        for (int i = ySize - 1; i >= 0; i--)
        {
            if (this[Column, i] == SlotState.Empty)
            {
                for (int j = 0; j < ySize - 1; j++)
                {
                    for (int k = 0; k < slotHeight; k++)
                    {
                        g.DrawImage(Resources.RealRed_Slot, Column * slotWidth, (j + 1) * slotHeight + k, slotWidth, slotHeight); // Not working !!!!!
                        g.DrawImage(Resources.RealEmpty_Slot, Column * slotWidth, (j + 1) * slotHeight, slotWidth, slotHeight); // Not working !!!!!
                        g.DrawImage(Resources.RealEmpty_Slot, Column * slotWidth, (j + 2) * slotHeight, slotWidth, slotHeight); // Not working !!!!!
                        System.Threading.Thread.Sleep(5);
                    }
                }
                this[Column, i] = state;
                return true;
            }
        }
        return false;
    }
    public bool PopTopOfColumn(int Column)
    {
        if (Column < 0 || Column >= xSize)
            throw new ArgumentOutOfRangeException("Column");
        for (int i = ySize - 1; i >= 0; i--)
        {
            if (this[Column, i] != SlotState.Empty)
            {
                this[Column, i] = SlotState.Empty;
                return true;
            }
        }
        return false;
    }

    private void DrawSlot(SlotState state, int columnIndex, int rowIndex)
    {
        Graphics g = this.CreateGraphics();
        switch (state)
        {
            case SlotState.Empty:
                g.DrawImage(Resources.RealEmpty_Slot, columnIndex * slotWidth, (rowIndex + 1) * slotHeight, slotWidth, slotHeight);
                break;
            case SlotState.Red:
                g.DrawImage(Resources.Red_Slot, columnIndex * slotWidth, (rowIndex + 1) * slotHeight, slotWidth, slotHeight);
                break;
            case SlotState.Orange:
                g.DrawImage(Resources.Orange_Slot, columnIndex * slotWidth, (rowIndex + 1) * slotHeight, slotWidth, slotHeight);
                break;
            default:
                break;
        }
    }
}

而且。。。不起作用。我该怎么办?如果我在绘画中这样做并刷新它,它会闪烁。

在用户控件中绘制图像

您需要在Paint处理程序中绘制所有;否则,它将在下次重新绘制时被擦除。

若要防止闪烁,请将DoubledBuffered设置为true。