⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 squarecontrol.cs

📁 tic tac toe is simple game .I think every people had play it
💻 CS
字号:
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System;

class SquareControl : UserControl
{
    //TODO: Implement as property
    public int iContents;
    public int iCol;
    public int iRow;

    //Used for double-buffering
    Bitmap bmpBackBuffer;
    
    public SquareControl(int Col, int Row)
    {
        BackColor = Color.White;
        ForeColor = SystemColors.WindowText;
        iContents = Board.Empty;
        iCol = Col; 
        iRow = Row;
        bmpBackBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);

   }
  
    protected override void OnPaint(PaintEventArgs pea)
    {
        base.OnPaint(pea);
        
        //Use a back-buffer to reduce flicker
        Graphics buffer = Graphics.FromImage(bmpBackBuffer);
        buffer.SmoothingMode = SmoothingMode.AntiAlias;


        Pen pen;
        if (this.iContents == Board.X)
        {
             pen = new Pen(new SolidBrush(Color.Red), 3f);
        }
        else if (this.iContents == Board.O)
        {
            pen = new Pen(new SolidBrush(Color.Blue), 3);
        }
        else
        {
            pen = new Pen(ForeColor);
        }
             
        switch (iContents)
        {
           case 0:
              //empty square
              buffer.Clear(BackColor);
              break;
           case -1:
              //draw an X
              buffer.Clear(BackColor);
              buffer.DrawLine(pen, 0, 0, ClientSize.Width, ClientSize.Height);
              buffer.DrawLine(pen, 0, ClientSize.Height, ClientSize.Width, 0);
              break;
           case 1:
              //draw an O
              Rectangle rect = new Rectangle(2, 2, (ClientRectangle.Width - 5), (ClientRectangle.Height - 5));
              buffer.Clear(BackColor);
              buffer.DrawEllipse(pen, rect);
              break;
        }

        //Copy the back-buffer to the screen
        Graphics viewable = pea.Graphics;
        viewable.DrawImageUnscaled(bmpBackBuffer, 0, 0);
        
        buffer.Dispose();
        viewable.Dispose();
    }
    protected override void OnResize(EventArgs ea)
    {
        //base.OnResize(ea);
        bmpBackBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
    }
    protected override void OnPaintBackground(PaintEventArgs pea)
    {
        //do nothing.  Overridden so that OnPaint will handle all the work. 
        //This helps to avoid flicker.
    }
   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -