다음을 통해 공유


DataGridViewImageColumn 클래스

정의

DataGridViewImageCell 개체의 컬렉션을 호스팅합니다.

public ref class DataGridViewImageColumn : System::Windows::Forms::DataGridViewColumn
[System.Drawing.ToolboxBitmap(typeof(System.Windows.Forms.DataGridViewImageColumn), "DataGridViewImageColumn.bmp")]
public class DataGridViewImageColumn : System.Windows.Forms.DataGridViewColumn
[System.Drawing.ToolboxBitmap(typeof(System.Windows.Forms.DataGridViewImageColumn), "DataGridViewImageColumn")]
public class DataGridViewImageColumn : System.Windows.Forms.DataGridViewColumn
[<System.Drawing.ToolboxBitmap(typeof(System.Windows.Forms.DataGridViewImageColumn), "DataGridViewImageColumn.bmp")>]
type DataGridViewImageColumn = class
    inherit DataGridViewColumn
[<System.Drawing.ToolboxBitmap(typeof(System.Windows.Forms.DataGridViewImageColumn), "DataGridViewImageColumn")>]
type DataGridViewImageColumn = class
    inherit DataGridViewColumn
Public Class DataGridViewImageColumn
Inherits DataGridViewColumn
상속
특성

예제

다음 코드 예제에서는 이미지를 사용하여 TicTacToe 게임을 만드는 방법을 보여 줍니다.

#using <System.Drawing.dll>
#using <System.dll>
#using <system.windows.forms.dll>

using namespace System::IO;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System;
public ref class TicTacToe: public System::Windows::Forms::Form
{
public:
   TicTacToe()
      : Form()
   {
      oBytes = gcnew array<Byte>{
        0x42, 0x4D, 0xC6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x76, 
        0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0xB, 0x0, 0x0, 0x0, 0xA,
        0x0, 0x0, 0x0, 0x1, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x50,
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10,
        0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
        0x0, 0x80, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x80, 0x80, 0x0, 
        0x80, 0x0, 0x0, 0x0, 0x80, 0x0, 0x80, 0x0, 0x80, 0x80, 0x0, 
        0x0, 0xC0, 0xC0, 0xC0, 0x0, 0x80, 0x80, 0x80, 0x0, 0x0, 0x0,
        0xFF, 0x0, 0x0, 0xFF, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0xFF, 
        0x0, 0x0, 0x0, 0xFF, 0x0, 0xFF, 0x0, 0xFF, 0xFF, 0x0, 0x0, 
        0xFF, 0xFF, 0xFF, 0x0, 0xFF, 0xFF, 0x0, 0xF, 0xFF, 0xF0, 
        0x0, 0x0, 0xFF, 0x0, 0xFF, 0xF0, 0xF, 0xF0, 0x0, 0x0, 0xF0, 
        0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xF0, 0xFF, 0xFF, 
        0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xF, 0xFF, 0xFF, 0xFF, 0xFF, 
        0x0, 0x0, 0x0, 0xF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0, 
        0xF0, 0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xF0, 0xFF, 
        0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xFF, 0x0, 0xFF, 0xF0, 
        0xF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0xF, 0xFF, 0xF0, 0x0, 
        0x0};
      xBytes = gcnew array<Byte>{
        0x42, 0x4D, 0xC6, 0x0, 0x0, 0x0, 0x0, 
        0x0, 0x0, 0x0, 0x76, 0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 
        0xB, 0x0, 0x0, 0x0, 0xA, 0x0, 0x0, 0x0, 0x1, 0x0, 0x4, 0x0, 
        0x0, 0x0, 0x0, 0x0, 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
        0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x80, 
        0x0, 0x0, 0x0, 0x80, 0x80, 0x0, 0x80, 0x0, 0x0, 0x0, 0x80, 
        0x0, 0x80, 0x0, 0x80, 0x80, 0x0, 0x0, 0xC0, 0xC0, 0xC0, 0x0,
        0x80, 0x80, 0x80, 0x0, 0x0, 0x0, 0xFF, 0x0, 0x0, 0xFF, 0x0, 
        0x0, 0x0, 0xFF, 0xFF, 0x0, 0xFF, 0x0, 0x0, 0x0, 0xFF, 0x0, 
        0xFF, 0x0, 0xFF, 0xFF, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0x0, 
        0xF0, 0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xFF, 0xF, 
        0xFF, 0xFF, 0xF, 0xF0, 0x0, 0x0, 0xFF, 0xF0, 0xFF, 0xF0, 
        0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 0xF, 0xF, 0xFF, 0xF0, 0x0,
        0x0, 0xFF, 0xFF, 0xF, 0xF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF,
        0xF, 0xF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xF0, 0xFF, 0xF0, 
        0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xF, 0xFF, 0xFF, 0xF, 0xF0, 0x0,
        0x0, 0xF0, 0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xFF, 
        0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0};
      blankBytes = gcnew array<Byte>{
        0x42, 0x4D, 0xC6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x76, 
        0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0xB, 0x0, 0x0, 0x0, 0xA,
        0x0, 0x0, 0x0, 0x1, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x50,
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10,
        0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
        0x0, 0x80, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x80, 0x80, 0x0, 
        0x80, 0x0, 0x0, 0x0, 0x80, 0x0, 0x80, 0x0, 0x80, 0x80, 0x0, 
        0x0, 0xC0, 0xC0, 0xC0, 0x0, 0x80, 0x80, 0x80, 0x0, 0x0, 0x0,
        0xFF, 0x0, 0x0, 0xFF, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0xFF, 
        0x0, 0x0, 0x0, 0xFF, 0x0, 0xFF, 0x0, 0xFF, 0xFF, 0x0, 0x0, 
        0xFF, 0xFF, 0xFF, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 
        0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 
        0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 
        0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 
        0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 
        0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 
        0xFF, 0xF0, 0x0, 0x0};
      blank = gcnew Bitmap( gcnew MemoryStream( blankBytes ) );
      x = gcnew Bitmap( gcnew MemoryStream( xBytes ) );
      o = gcnew Bitmap( gcnew MemoryStream( oBytes ) );
      this->AutoSize = true;
      turn = gcnew Label;
      Button1 = gcnew Button;
      Button2 = gcnew Button;
      Button3 = gcnew Button;
      Button4 = gcnew Button;
      Button5 = gcnew Button;
      Panel1 = gcnew FlowLayoutPanel;
      SetupButtons();
      InitializeDataGridView( nullptr, nullptr );

      xString = L"X's turn";
      oString = L"O's turn";
      gameOverString = L"Game Over";
      bitmapPadding = 6;
   }


private:
   DataGridView^ dataGridView1;
   Button^ Button1;
   Label^ turn;
   Button^ Button2;
   Button^ Button3;
   Button^ Button4;
   Button^ Button5;
   FlowLayoutPanel^ Panel1;

#pragma region S " bitmaps " 
   array<Byte>^oBytes;
   array<Byte>^xBytes;
   array<Byte>^blankBytes;

#pragma endregion 
   Bitmap^ blank;
   Bitmap^ x;
   Bitmap^ o;
   String^ xString;
   String^ oString;
   String^ gameOverString;
   int bitmapPadding;
   void InitializeDataGridView( Object^ ignored, EventArgs^ e )
   {
      this->Panel1->SuspendLayout();
      this->SuspendLayout();
      ConfigureForm();
      SizeGrid();
      CreateColumns();
      CreateRows();
      this->Panel1->ResumeLayout( false );
      this->ResumeLayout( false );
   }

   void ConfigureForm()
   {
      AutoSize = true;
      turn->Size = System::Drawing::Size( 75, 34 );
      turn->TextAlign = ContentAlignment::MiddleLeft;
      Panel1->Location = System::Drawing::Point( 0, 8 );
      Panel1->Size = System::Drawing::Size( 120, 196 );
      Panel1->FlowDirection = FlowDirection::TopDown;
      ClientSize = System::Drawing::Size( 355, 200 );
      Controls->Add( this->Panel1 );
      Text = L"TicTacToe";
      dataGridView1 = gcnew System::Windows::Forms::DataGridView;
      dataGridView1->Location = Point(120,0);
      dataGridView1->AllowUserToAddRows = false;
      dataGridView1->CellClick += gcnew DataGridViewCellEventHandler( this, &TicTacToe::dataGridView1_CellClick );
      dataGridView1->CellMouseEnter += gcnew DataGridViewCellEventHandler( this, &TicTacToe::dataGridView1_CellMouseEnter );
      dataGridView1->CellMouseLeave += gcnew DataGridViewCellEventHandler( this, &TicTacToe::dataGridView1_CellMouseLeave );
      Controls->Add( dataGridView1 );
      turn->Text = xString;
      turn->AutoSize = true;
   }

   void SetupButtons()
   {
      Button1->AutoSize = true;
      SetupButton( Button1, L"Restart", gcnew EventHandler( this, &TicTacToe::Reset ) );
      Panel1->Controls->Add( turn );
      SetupButton( Button2, L"Increase Cell Size", gcnew EventHandler( this, &TicTacToe::MakeCellsLarger ) );
      SetupButton( Button3, L"Stretch Images", gcnew EventHandler( this, &TicTacToe::Stretch ) );
      SetupButton( Button4, L"Zoom Images", gcnew EventHandler( this, &TicTacToe::ZoomToImage ) );
      SetupButton( Button5, L"Normal Images", gcnew EventHandler( this, &TicTacToe::NormalImage ) );
   }

   void SetupButton( Button^ button, String^ buttonLabel, EventHandler^ handler )
   {
      Panel1->Controls->Add( button );
      button->Text = buttonLabel;
      button->AutoSize = true;
      button->Click += handler;
   }


   void CreateColumns()
   {
      DataGridViewImageColumn^ imageColumn;
      int columnCount = 0;
      do
      {
         Bitmap^ unMarked = blank;
         imageColumn = gcnew DataGridViewImageColumn;
         
         //Add twice the padding for the left and 
         //right sides of the cell.
         imageColumn->Width = x->Width + 2 * bitmapPadding + 1;
         imageColumn->Image = unMarked;
         dataGridView1->Columns->Add( imageColumn );
         columnCount = columnCount + 1;
      }
      while ( columnCount < 3 );
   }


   void CreateRows()
   {
      dataGridView1->Rows->Add();
      dataGridView1->Rows->Add();
      dataGridView1->Rows->Add();
   }


   void SizeGrid()
   {
      dataGridView1->ColumnHeadersVisible = false;
      dataGridView1->RowHeadersVisible = false;
      dataGridView1->AllowUserToResizeColumns = false;
      dataGridView1->AllowUserToResizeRows = false;
      dataGridView1->BorderStyle = BorderStyle::None;
      
      //Add twice the padding for the top of the cell 
      //and the bottom.
      dataGridView1->RowTemplate->Height = x->Height + 2 * bitmapPadding + 1;
      dataGridView1->AutoSize = true;
   }


   void Reset( Object^ sender, System::EventArgs^ e )
   {
      dataGridView1->~DataGridView();
      InitializeDataGridView( nullptr, nullptr );
   }


   void dataGridView1_CellClick( Object^ sender, DataGridViewCellEventArgs^ e )
   {
      if ( turn->Equals( gameOverString ) )
      {
         return;
      }

      DataGridViewImageCell^ cell = dynamic_cast<DataGridViewImageCell^>(dataGridView1->Rows[ e->RowIndex ]->Cells[ e->ColumnIndex ]);
      if ( cell->Value == blank )
      {
         if ( IsOsTurn() )
         {
            cell->Value = o;
         }
         else
         {
            cell->Value = x;
         }

         ToggleTurn();
      }

      if ( IsAWin( cell ) )
      {
         turn->Text = gameOverString;
      }
   }


   void dataGridView1_CellMouseEnter( Object^ sender, DataGridViewCellEventArgs^ e )
   {
      Bitmap^ markingUnderMouse = dynamic_cast<Bitmap^>(dataGridView1->Rows[ e->RowIndex ]->Cells[ e->ColumnIndex ]->Value);
      if ( markingUnderMouse == blank )
      {
         dataGridView1->Cursor = Cursors::Default;
      }
      else
      if ( markingUnderMouse == o || markingUnderMouse == x )
      {
         dataGridView1->Cursor = Cursors::No;
         ToolTip(e,true);
      }
   }

   void ToolTip( DataGridViewCellEventArgs^ e, bool showTip )
   {
      DataGridViewImageCell^ cell = dynamic_cast<DataGridViewImageCell^>(dataGridView1->Rows[ e->RowIndex ]->Cells[ e->ColumnIndex ]);
      DataGridViewImageColumn^ imageColumn = dynamic_cast<DataGridViewImageColumn^>(dataGridView1->Columns[ cell->ColumnIndex ]);
      if ( showTip )
            cell->ToolTipText = imageColumn->Description;
      else
      {
         cell->ToolTipText = String::Empty;
      }
   }

   void dataGridView1_CellMouseLeave( Object^ sender, DataGridViewCellEventArgs^ e )
   {
      ToolTip( e, false );
      dataGridView1->Cursor = Cursors::Default;
   }


   void Stretch( Object^ sender, EventArgs^ e )
   {
      System::Collections::IEnumerator^ myEnum = dataGridView1->Columns->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         DataGridViewImageColumn^ column = safe_cast<DataGridViewImageColumn^>(myEnum->Current);
         column->ImageLayout = DataGridViewImageCellLayout::Stretch;
         column->Description = L"Stretched";
      }
   }

   void ZoomToImage( Object^ sender, EventArgs^ e )
   {
      System::Collections::IEnumerator^ myEnum1 = dataGridView1->Columns->GetEnumerator();
      while ( myEnum1->MoveNext() )
      {
         DataGridViewImageColumn^ column = safe_cast<DataGridViewImageColumn^>(myEnum1->Current);
         column->ImageLayout = DataGridViewImageCellLayout::Zoom;
         column->Description = L"Zoomed";
      }
   }

   void NormalImage( Object^ sender, EventArgs^ e )
   {
      System::Collections::IEnumerator^ myEnum2 = dataGridView1->Columns->GetEnumerator();
      while ( myEnum2->MoveNext() )
      {
         DataGridViewImageColumn^ column = safe_cast<DataGridViewImageColumn^>(myEnum2->Current);
         column->ImageLayout = DataGridViewImageCellLayout::Normal;
         column->Description = L"Normal";
      }
   }


   void MakeCellsLarger( Object^ sender, EventArgs^ e )
   {
      System::Collections::IEnumerator^ myEnum3 = dataGridView1->Columns->GetEnumerator();
      while ( myEnum3->MoveNext() )
      {
         DataGridViewImageColumn^ column = safe_cast<DataGridViewImageColumn^>(myEnum3->Current);
         column->Width = column->Width * 2;
      }

      System::Collections::IEnumerable^ temp = safe_cast<System::Collections::IEnumerable^>(dataGridView1->Rows);
      System::Collections::IEnumerator^ myEnum4 = temp->GetEnumerator();

      //System::Collections::IEnumerator^ myEnum4 = dataGridView1->Rows->GetEnumerator();
      while ( myEnum4->MoveNext() )
      {
         DataGridViewRow^ row = safe_cast<DataGridViewRow^>(myEnum4->Current);
         if ( row->IsNewRow )
                  break;
         row->Height = (int)(row->Height * 1.5);
      }
   }

   bool IsAWin( DataGridViewCell^ cell )
   {
      if ( ARowIsSame() || AColumnIsSame() || ADiagonalIsSame() )
            return true;
      else
            return false;
   }

   bool ARowIsSame()
   {
      Bitmap^ marking = nullptr;
      System::Collections::IEnumerable^ temp = safe_cast<System::Collections::IEnumerable^>(dataGridView1->Rows);
      System::Collections::IEnumerator^ myEnum5 = temp->GetEnumerator();
      //System::Collections::IEnumerator^ myEnum5 = dataGridView1->Rows->GetEnumerator();
      while ( myEnum5->MoveNext() )
      {
         DataGridViewRow^ row = safe_cast<DataGridViewRow^>(myEnum5->Current);
         if ( row->IsNewRow )
                  break;

         marking = dynamic_cast<Bitmap^>(row->Cells[ 0 ]->Value);
         if ( marking != blank )
         {
            if ( marking == row->Cells[ 1 ]->Value && marking == row->Cells[ 2 ]->Value )
                        return true;
         }
      }

      return false;
   }

   bool AColumnIsSame()
   {
      int columnIndex = 0;
      Bitmap^ marking;
      do
      {
         marking = dynamic_cast<Bitmap^>(dataGridView1->Rows[ 0 ]->Cells[ columnIndex ]->Value);
         if ( marking != blank )
         {
            if ( marking == dynamic_cast<Bitmap^>(dataGridView1->Rows[ 1 ]->Cells[ columnIndex ]->Value) && marking == dynamic_cast<Bitmap^>(dataGridView1->Rows[ 2 ]->Cells[ columnIndex ]->Value) )
                        return true;
         }

         columnIndex = columnIndex + 1;
      }
      while ( columnIndex < dataGridView1->Columns->GetColumnCount( DataGridViewElementStates::Visible ) );

      return false;
   }

   bool ADiagonalIsSame()
   {
      if ( LeftToRightDiagonalIsSame() )
      {
         return true;
      }

      if ( RightToLeftDiagonalIsSame() )
      {
         return true;
      }

      return false;
   }

   bool LeftToRightDiagonalIsSame()
   {
      return IsDiagonalSame( 0, 2 );
   }

   bool RightToLeftDiagonalIsSame()
   {
      return IsDiagonalSame( 2, 0 );
   }

   bool IsDiagonalSame( int startingColumn, int lastColumn )
   {
      Bitmap^ marking = dynamic_cast<Bitmap^>(dataGridView1->Rows[ 0 ]->Cells[ startingColumn ]->Value);
      if ( marking == blank )
            return false;

      if ( marking == dataGridView1->Rows[ 1 ]->Cells[ 1 ]->Value && marking == dataGridView1->Rows[ 2 ]->Cells[ lastColumn ]->Value )
            return true;

      return false;
   }

   void ToggleTurn()
   {
      if ( turn->Text->Equals( xString ) )
      {
         turn->Text = oString;
      }
      else
      {
         turn->Text = xString;
      }
   }

   bool IsOsTurn()
   {
      if ( turn->Text->Equals( oString ) )
            return true;

      return false;
   }


public:

   [STAThread]
   static void Main()
   {
      Application::Run( gcnew TicTacToe );
   }

};

int main()
{
   TicTacToe::Main();
}
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System;

public class TicTacToe : System.Windows.Forms.Form
{
    public TicTacToe()
    {
        blank = new Bitmap(new MemoryStream(blankImage));
        x = new Bitmap(new MemoryStream(xImage));
        o = new Bitmap(new MemoryStream(oImage));

        this.AutoSize = true;
        SetupButtons();
        InitializeDataGridView(null, null);
    }

    private DataGridView dataGridView1;
    private Button Button1 = new Button();
    private Label turn = new Label();
    private Button Button2 = new Button();
    private Button Button3 = new Button();
    private Button Button4 = new Button();
    private Button Button5 = new Button();
    private FlowLayoutPanel Panel1 = new FlowLayoutPanel();

    #region "bitmaps"
    private byte[] oImage = new byte[] {
        0x42, 0x4D, 0xC6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x76, 
        0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0xB, 0x0, 0x0, 0x0, 0xA,
        0x0, 0x0, 0x0, 0x1, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x50,
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10,
        0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
        0x0, 0x80, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x80, 0x80, 0x0, 
        0x80, 0x0, 0x0, 0x0, 0x80, 0x0, 0x80, 0x0, 0x80, 0x80, 0x0, 
        0x0, 0xC0, 0xC0, 0xC0, 0x0, 0x80, 0x80, 0x80, 0x0, 0x0, 0x0,
        0xFF, 0x0, 0x0, 0xFF, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0xFF, 
        0x0, 0x0, 0x0, 0xFF, 0x0, 0xFF, 0x0, 0xFF, 0xFF, 0x0, 0x0, 
        0xFF, 0xFF, 0xFF, 0x0, 0xFF, 0xFF, 0x0, 0xF, 0xFF, 0xF0, 
        0x0, 0x0, 0xFF, 0x0, 0xFF, 0xF0, 0xF, 0xF0, 0x0, 0x0, 0xF0, 
        0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xF0, 0xFF, 0xFF, 
        0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xF, 0xFF, 0xFF, 0xFF, 0xFF, 
        0x0, 0x0, 0x0, 0xF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0, 
        0xF0, 0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xF0, 0xFF, 
        0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xFF, 0x0, 0xFF, 0xF0, 
        0xF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0xF, 0xFF, 0xF0, 0x0, 
        0x0};

    private byte[] xImage = new byte[]{
        0x42, 0x4D, 0xC6, 0x0, 0x0, 0x0, 0x0, 
        0x0, 0x0, 0x0, 0x76, 0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 
        0xB, 0x0, 0x0, 0x0, 0xA, 0x0, 0x0, 0x0, 0x1, 0x0, 0x4, 0x0, 
        0x0, 0x0, 0x0, 0x0, 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
        0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x80, 
        0x0, 0x0, 0x0, 0x80, 0x80, 0x0, 0x80, 0x0, 0x0, 0x0, 0x80, 
        0x0, 0x80, 0x0, 0x80, 0x80, 0x0, 0x0, 0xC0, 0xC0, 0xC0, 0x0,
        0x80, 0x80, 0x80, 0x0, 0x0, 0x0, 0xFF, 0x0, 0x0, 0xFF, 0x0, 
        0x0, 0x0, 0xFF, 0xFF, 0x0, 0xFF, 0x0, 0x0, 0x0, 0xFF, 0x0, 
        0xFF, 0x0, 0xFF, 0xFF, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0x0, 
        0xF0, 0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xFF, 0xF, 
        0xFF, 0xFF, 0xF, 0xF0, 0x0, 0x0, 0xFF, 0xF0, 0xFF, 0xF0, 
        0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 0xF, 0xF, 0xFF, 0xF0, 0x0,
        0x0, 0xFF, 0xFF, 0xF, 0xF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF,
        0xF, 0xF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xF0, 0xFF, 0xF0, 
        0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xF, 0xFF, 0xFF, 0xF, 0xF0, 0x0,
        0x0, 0xF0, 0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xFF, 
        0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0};

    private byte[] blankImage = new byte[] {
        0x42, 0x4D, 0xC6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x76, 
        0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0xB, 0x0, 0x0, 0x0, 0xA,
        0x0, 0x0, 0x0, 0x1, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x50,
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10,
        0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
        0x0, 0x80, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x80, 0x80, 0x0, 
        0x80, 0x0, 0x0, 0x0, 0x80, 0x0, 0x80, 0x0, 0x80, 0x80, 0x0, 
        0x0, 0xC0, 0xC0, 0xC0, 0x0, 0x80, 0x80, 0x80, 0x0, 0x0, 0x0,
        0xFF, 0x0, 0x0, 0xFF, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0xFF, 
        0x0, 0x0, 0x0, 0xFF, 0x0, 0xFF, 0x0, 0xFF, 0xFF, 0x0, 0x0, 
        0xFF, 0xFF, 0xFF, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 
        0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 
        0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 
        0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 
        0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 
        0xFF, 0xFF, 0xFF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 
        0xFF, 0xF0, 0x0, 0x0};
    #endregion

    private Bitmap blank;
    private Bitmap x;
    private Bitmap o;
    private string xString = "X's turn";
    private string oString = "O's turn";
    private string gameOverString = "Game Over";
    private int bitmapPadding = 6;

    private void InitializeDataGridView(object sender,
        EventArgs e)
    {
        this.Panel1.SuspendLayout();
        this.SuspendLayout();

        ConfigureForm();
        SizeGrid();
        CreateColumns();
        CreateRows();

        this.Panel1.ResumeLayout(false);
        this.ResumeLayout(false);
    }

    private void ConfigureForm()
    {
        AutoSize = true;
        turn.Size = new System.Drawing.Size(75, 34);
        turn.TextAlign = ContentAlignment.MiddleLeft;
        turn.Text = xString;

        Panel1.Location = new System.Drawing.Point(0, 8);
        Panel1.Size = new System.Drawing.Size(120, 196);
        Panel1.FlowDirection = FlowDirection.TopDown;

        ClientSize = new System.Drawing.Size(355, 200);
        Controls.Add(this.Panel1);
        Text = "TicTacToe";

        dataGridView1 = new System.Windows.Forms.DataGridView();
        dataGridView1.Location = new Point(120, 0);
        dataGridView1.AllowUserToAddRows = false;
        dataGridView1.CellClick += new
            DataGridViewCellEventHandler(dataGridView1_CellClick);
        dataGridView1.CellMouseEnter += new
            DataGridViewCellEventHandler(dataGridView1_CellMouseEnter);
        dataGridView1.CellMouseLeave += new
            DataGridViewCellEventHandler(dataGridView1_CellMouseLeave);

        Controls.Add(dataGridView1);
    }

    private void SetupButtons()
    {
        Button1.AutoSize = true;
        SetupButton(Button1, "Restart", new EventHandler(Reset));
        Panel1.Controls.Add(turn);
        SetupButton(Button2, "Increase Cell Size", new EventHandler(MakeCellsLarger));
        SetupButton(Button3, "Stretch Images", new EventHandler(Stretch));
        SetupButton(Button4, "Zoom Images", new EventHandler(ZoomToImage));
        SetupButton(Button5, "Normal Images", new EventHandler(NormalImage));
    }

    private void SetupButton(Button button, string buttonLabel, EventHandler handler)
    {
        Panel1.Controls.Add(button);
        button.Text = buttonLabel;
        button.AutoSize = true;
        button.Click += handler;
    }

    private void CreateColumns()
    {
        DataGridViewImageColumn imageColumn;
        int columnCount = 0;
        do
        {
            Bitmap unMarked = blank;
            imageColumn = new DataGridViewImageColumn();

            //Add twice the padding for the left and 
            //right sides of the cell.
            imageColumn.Width = x.Width + 2 * bitmapPadding + 1;

            imageColumn.Image = unMarked;
            dataGridView1.Columns.Add(imageColumn);
            columnCount = columnCount + 1;
        }
        while (columnCount < 3);
    }

    private void CreateRows()
    {
        dataGridView1.Rows.Add();
        dataGridView1.Rows.Add();
        dataGridView1.Rows.Add();
    }

    private void SizeGrid()
    {
        dataGridView1.ColumnHeadersVisible = false;
        dataGridView1.RowHeadersVisible = false;
        dataGridView1.AllowUserToResizeColumns = false;;
        dataGridView1.AllowUserToResizeRows = false;
        dataGridView1.BorderStyle = BorderStyle.None;

        //Add twice the padding for the top of the cell 
        //and the bottom.
        dataGridView1.RowTemplate.Height = x.Height +
            2 * bitmapPadding + 1;

        dataGridView1.AutoSize = true;
    }

    private void Reset(object sender, System.EventArgs e)
    {
        dataGridView1.Dispose();
        InitializeDataGridView(null, null);
    }

    private void dataGridView1_CellClick(object sender,
        DataGridViewCellEventArgs e)
    {

        if (turn.Text.Equals(gameOverString)) { return; }

        DataGridViewImageCell cell = (DataGridViewImageCell)
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

        if (cell.Value == blank)
        {
            if (IsOsTurn())
            {
                cell.Value = o;
            }
            else
            {
                cell.Value = x;
            }
            ToggleTurn();
        }
        if (IsAWin())
        {
            turn.Text = gameOverString;
        }
    }

    private void dataGridView1_CellMouseEnter(object sender,
        DataGridViewCellEventArgs e)
    {
        Bitmap markingUnderMouse = (Bitmap)dataGridView1.
               Rows[e.RowIndex].
               Cells[e.ColumnIndex].Value;

        if (markingUnderMouse == blank)
        {
            dataGridView1.Cursor = Cursors.Default;
        }
        else if (markingUnderMouse == o || markingUnderMouse == x)
        {
            dataGridView1.Cursor = Cursors.No;
            ToolTip(e, true);
        }
    }

    private void ToolTip(DataGridViewCellEventArgs e, bool showTip)
    {
        DataGridViewImageCell cell = (DataGridViewImageCell)
            dataGridView1
            .Rows[e.RowIndex].Cells[e.ColumnIndex];
        DataGridViewImageColumn imageColumn =
            (DataGridViewImageColumn)
            dataGridView1.Columns[cell.ColumnIndex];

        if (showTip)
        {
            cell.ToolTipText = imageColumn.Description;
        }
        else { cell.ToolTipText = String.Empty; }
    }

    private void dataGridView1_CellMouseLeave(object sender,
        DataGridViewCellEventArgs e)
    {
        ToolTip(e, false);
        dataGridView1.Cursor = Cursors.Default;
    }

    private void Stretch(object sender, EventArgs e)
    {
        foreach (DataGridViewImageColumn column in
            dataGridView1.Columns)
        {
            column.ImageLayout = DataGridViewImageCellLayout.Stretch;
            column.Description = "Stretched";
        }
    }

    private void ZoomToImage(object sender, EventArgs e)
    {

        foreach (DataGridViewImageColumn column in
            dataGridView1.Columns)
        {
            column.ImageLayout = DataGridViewImageCellLayout.Zoom;
            column.Description = "Zoomed";
        }
    }

    private void NormalImage(object sender, EventArgs e)
    {

        foreach (DataGridViewImageColumn column in
            dataGridView1.Columns)
        {
            column.ImageLayout = DataGridViewImageCellLayout.Normal;
            column.Description = "Normal";
        }
    }

    private void MakeCellsLarger(object sender, EventArgs e)
    {
        foreach (DataGridViewImageColumn column in
            dataGridView1.Columns)
        {
            column.Width = column.Width * 2;
        }
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.IsNewRow) break;
            row.Height = (int)(row.Height * 1.5);
        }
    }

    private bool IsAWin()
    {
        if (ARowIsSame() || AColumnIsSame() || ADiagonalIsSame())
            return true;
        else return false;
    }

    private bool ARowIsSame()
    {
        Bitmap marking = null;

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.IsNewRow) break;
            marking = (Bitmap)row.Cells[0].Value;
            if (marking != blank)
            {
                if (marking == row.Cells[1].Value &&
                    marking == row.Cells[2].Value) return true;
            }
        }
        return false;
    }

    private bool AColumnIsSame()
    {
        int columnIndex = 0;
        Bitmap marking;
        do
        {
            marking = (Bitmap)
                dataGridView1.Rows[0].Cells[columnIndex].Value;
            if (marking != blank)
            {
                if (marking == (Bitmap)dataGridView1.Rows[1]
                    .Cells[columnIndex].Value
                    && marking == (Bitmap)dataGridView1.Rows[2].
                    Cells[columnIndex].Value) return true;
            }
            columnIndex = columnIndex + 1;
        }
        while (columnIndex < dataGridView1.Columns.GetColumnCount(
            DataGridViewElementStates.Visible));
        return false;
    }

    private bool ADiagonalIsSame()
    {
        if (LeftToRightDiagonalIsSame()) { return true; }
        if (RightToLeftDiagonalIsSame()) { return true; }
        return false;
    }

    private bool LeftToRightDiagonalIsSame()
    {
        return IsDiagonalSame(0, 2);
    }

    private bool RightToLeftDiagonalIsSame()
    {
        return IsDiagonalSame(2, 0);
    }

    private bool IsDiagonalSame(int startingColumn, int lastColumn)
    {
        Bitmap marking = (Bitmap)dataGridView1.Rows[0]
            .Cells[startingColumn].Value;

        if (marking == blank) return false;
        if (marking == dataGridView1.Rows[1].Cells[1]
            .Value && marking == dataGridView1.Rows[2]
            .Cells[lastColumn].Value) return true;

        return false;
    }

    private void ToggleTurn()
    {
        if (turn.Text.Equals(xString)) { turn.Text = oString; }
        else { turn.Text = xString; }
    }

    private bool IsOsTurn()
    {
        if (turn.Text.Equals(oString)) return true;
        return false;
    }

    [STAThread]
    public static void Main()
    {
        Application.Run(new TicTacToe());
    }
}
Imports System.IO
Imports System.Windows.Forms
Imports System.Drawing
Public Class TicTacToe
    Inherits System.Windows.Forms.Form

    Friend WithEvents dataGridView1 As DataGridView
    Friend WithEvents Button1 As Button = New Button()
    Friend WithEvents turn As Label = New Label()
    Friend WithEvents Button2 As Button = New Button()
    Friend WithEvents Button3 As Button = New Button()
    Friend WithEvents Button4 As Button = New Button()
    Friend WithEvents Button5 As Button = New Button()
    Friend WithEvents Panel1 As FlowLayoutPanel = New FlowLayoutPanel()

#Region "bitmaps"
    Private oImage As Byte() = { _
        &H42, &H4D, &HC6, &H0, &H0, &H0, &H0, &H0, _
        &H0, &H0, &H76, &H0, &H0, &H0, &H28, &H0, _
        &H0, &H0, &HB, &H0, &H0, &H0, &HA, &H0, _
        &H0, &H0, &H1, &H0, &H4, &H0, &H0, &H0, _
        &H0, &H0, &H50, &H0, &H0, &H0, &H0, &H0, _
        &H0, &H0, &H0, &H0, &H0, &H0, &H10, &H0, _
        &H0, &H0, &H10, &H0, &H0, &H0, &H0, &H0, _
        &H0, &H0, &H0, &H0, &H80, &H0, &H0, &H80, _
        &H0, &H0, &H0, &H80, &H80, &H0, &H80, &H0, _
        &H0, &H0, &H80, &H0, &H80, &H0, &H80, &H80, _
        &H0, &H0, &HC0, &HC0, &HC0, &H0, &H80, &H80, _
        &H80, &H0, &H0, &H0, &HFF, &H0, &H0, &HFF, _
        &H0, &H0, &H0, &HFF, &HFF, &H0, &HFF, &H0, _
        &H0, &H0, &HFF, &H0, &HFF, &H0, &HFF, &HFF, _
        &H0, &H0, &HFF, &HFF, &HFF, &H0, &HFF, &HFF, _
        &H0, &HF, &HFF, &HF0, &H0, &H0, &HFF, &H0, _
        &HFF, &HF0, &HF, &HF0, &H0, &H0, &HF0, &HFF, _
        &HFF, &HFF, &HF0, &HF0, &H0, &H0, &HF0, &HFF, _
        &HFF, &HFF, &HF0, &HF0, &H0, &H0, &HF, &HFF, _
        &HFF, &HFF, &HFF, &H0, &H0, &H0, &HF, &HFF, _
        &HFF, &HFF, &HFF, &H0, &H0, &H0, &HF0, &HFF, _
        &HFF, &HFF, &HF0, &HF0, &H0, &H0, &HF0, &HFF, _
        &HFF, &HFF, &HF0, &HF0, &H0, &H0, &HFF, &H0, _
        &HFF, &HF0, &HF, &HF0, &H0, &H0, &HFF, &HFF, _
        &H0, &HF, &HFF, &HF0, &H0, &H0}

    Private xImage As Byte() = { _
        &H42, &H4D, &HC6, &H0, &H0, &H0, &H0, &H0, _
        &H0, &H0, &H76, &H0, &H0, &H0, &H28, &H0, _
        &H0, &H0, &HB, &H0, &H0, &H0, &HA, &H0, _
        &H0, &H0, &H1, &H0, &H4, &H0, &H0, &H0, _
        &H0, &H0, &H50, &H0, &H0, &H0, &H0, &H0, _
        &H0, &H0, &H0, &H0, &H0, &H0, &H10, &H0, _
        &H0, &H0, &H10, &H0, &H0, &H0, &H0, &H0, _
        &H0, &H0, &H0, &H0, &H80, &H0, &H0, &H80, _
        &H0, &H0, &H0, &H80, &H80, &H0, &H80, &H0, _
        &H0, &H0, &H80, &H0, &H80, &H0, &H80, &H80, _
        &H0, &H0, &HC0, &HC0, &HC0, &H0, &H80, &H80, _
        &H80, &H0, &H0, &H0, &HFF, &H0, &H0, &HFF, _
        &H0, &H0, &H0, &HFF, &HFF, &H0, &HFF, &H0, _
        &H0, &H0, &HFF, &H0, &HFF, &H0, &HFF, &HFF, _
        &H0, &H0, &HFF, &HFF, &HFF, &H0, &HF0, &HFF, _
        &HFF, &HFF, &HF0, &HF0, &H0, &H0, &HFF, &HF, _
        &HFF, &HFF, &HF, &HF0, &H0, &H0, &HFF, &HF0, _
        &HFF, &HF0, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HF, &HF, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HF, &HF, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HF, &HF, &HFF, &HF0, &H0, &H0, &HFF, &HF0, _
        &HFF, &HF0, &HFF, &HF0, &H0, &H0, &HFF, &HF, _
        &HFF, &HFF, &HF, &HF0, &H0, &H0, &HF0, &HFF, _
        &HFF, &HFF, &HF0, &HF0, &H0, &H0, &HFF, &HFF, _
        &HFF, &HFF, &HFF, &HF0, &H0, &H0}

    Private blankImage As Byte() = { _
        &H42, &H4D, &HC6, &H0, &H0, &H0, &H0, &H0, _
        &H0, &H0, &H76, &H0, &H0, &H0, &H28, &H0, _
        &H0, &H0, &HB, &H0, &H0, &H0, &HA, &H0, _
        &H0, &H0, &H1, &H0, &H4, &H0, &H0, &H0, _
        &H0, &H0, &H50, &H0, &H0, &H0, &H0, &H0, _
        &H0, &H0, &H0, &H0, &H0, &H0, &H10, &H0, _
        &H0, &H0, &H10, &H0, &H0, &H0, &H0, &H0, _
        &H0, &H0, &H0, &H0, &H80, &H0, &H0, &H80, _
        &H0, &H0, &H0, &H80, &H80, &H0, &H80, &H0, _
        &H0, &H0, &H80, &H0, &H80, &H0, &H80, &H80, _
        &H0, &H0, &HC0, &HC0, &HC0, &H0, &H80, &H80, _
        &H80, &H0, &H0, &H0, &HFF, &H0, &H0, &HFF, _
        &H0, &H0, &H0, &HFF, &HFF, &H0, &HFF, &H0, _
        &H0, &H0, &HFF, &H0, &HFF, &H0, &HFF, &HFF, _
        &H0, &H0, &HFF, &HFF, &HFF, &H0, &HFF, &HFF, _
        &HFF, &HFF, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HFF, &HFF, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HFF, &HFF, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HFF, &HFF, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HFF, &HFF, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HFF, &HFF, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HFF, &HFF, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HFF, &HFF, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HFF, &HFF, &HFF, &HF0, &H0, &H0, &HFF, &HFF, _
        &HFF, &HFF, &HFF, &HF0, &H0, &H0}
#End Region

    Private blank As Bitmap = New Bitmap( _
        New MemoryStream(blankImage))
    Private x As Bitmap = New Bitmap(New MemoryStream(xImage))
    Private o As Bitmap = New Bitmap(New MemoryStream(oImage))
    Private xString As String = "X's turn"
    Private oString As String = "O's turn"
    Private gameOverString As String = "Game Over"
    Private bitmapPadding As Integer = 6

    Private Sub InitializeDataGridView(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Me.Load
        SuspendLayout()
        Panel1.SuspendLayout()

        ConfigureForm()
        SizeGrid()
        CreateColumns()
        CreateRows()

        ResumeLayout(False)
        Panel1.ResumeLayout(False)
    End Sub

    Private Sub ConfigureForm()
        AutoSize = True
        turn.Size = New System.Drawing.Size(75, 34)
        turn.TextAlign = ContentAlignment.MiddleLeft
        turn.Text = xString

        Panel1.FlowDirection = FlowDirection.TopDown
        Panel1.Location = New System.Drawing.Point(0, 8)
        Panel1.Size = New System.Drawing.Size(120, 196)

        ClientSize = New System.Drawing.Size(355, 200)
        Controls.Add(Me.Panel1)
        Text = "TicTacToe"

        dataGridView1 = New System.Windows.Forms.DataGridView
        dataGridView1.Location = New Point(120, 0)
        dataGridView1.AllowUserToAddRows = False
        Controls.Add(dataGridView1)
        SetupButtons()
    End Sub

    Private Sub SetupButtons()
        SetupButton(Button1, "Restart")
        Panel1.Controls.Add(Me.turn)
        SetupButton(Button2, "Increase Cell Size")
        SetupButton(Button3, "Stretch Images")
        SetupButton(Button4, "Zoom Images")
        SetupButton(Button5, "Normal Images")
    End Sub

    Private Sub SetupButton(ByVal button As Button, _
        ByVal buttonLabel As String)

        Panel1.Controls.Add(button)
        button.Text = buttonLabel
        button.AutoSize = True

    End Sub

    Private Sub CreateColumns()

        Dim imageColumn As DataGridViewImageColumn
        Dim columnCount As Integer = 0
        Do
            Dim unMarked As Bitmap = blank
            imageColumn = New DataGridViewImageColumn()

            ' Add twice the padding for the left and 
            ' right sides of the cell.
            imageColumn.Width = x.Width + 2 * bitmapPadding + 1

            imageColumn.Image = unMarked
            imageColumn.ImageLayout = DataGridViewImageCellLayout.NotSet
            imageColumn.Description = "default image layout"
            dataGridView1.Columns.Add(imageColumn)
            columnCount = columnCount + 1
        Loop While columnCount < 3
    End Sub

    Private Sub CreateRows()
        dataGridView1.Rows.Add()
        dataGridView1.Rows.Add()
        dataGridView1.Rows.Add()
    End Sub

    Private Sub SizeGrid()
        dataGridView1.ColumnHeadersVisible = False
        dataGridView1.RowHeadersVisible = False
        dataGridView1.AllowUserToResizeColumns = False
        dataGridView1.AllowUserToResizeRows = False
        dataGridView1.BorderStyle = BorderStyle.None

        ' Add twice the padding for the top and bottom of the cell. 
        dataGridView1.RowTemplate.Height = x.Height + _
            2 * bitmapPadding + 1

        dataGridView1.AutoSize = True
    End Sub

    Private Sub reset(ByVal sender As Object, _
        ByVal e As EventArgs) _
        Handles Button1.Click

        dataGridView1.Dispose()
        InitializeDataGridView(Nothing, Nothing)
    End Sub

    Private Sub dataGridView1_CellClick(ByVal sender As Object, _
        ByVal e As DataGridViewCellEventArgs) _
        Handles dataGridView1.CellClick

        If turn.Text.Equals(gameOverString) Then Return

        Dim cell As DataGridViewImageCell = _
            CType(dataGridView1.Rows(e.RowIndex). _
                Cells(e.ColumnIndex), DataGridViewImageCell)
        If (cell.Value Is blank) Then
            If IsOsTurn() Then
                cell.Value = o
            Else
                cell.Value = x
            End If
            ToggleTurn()
            ToolTip(e)
        End If
        If IsAWin() Then
            turn.Text = gameOverString
        End If
    End Sub

    Private Sub dataGridView1_CellMouseEnter(ByVal sender As Object, _
        ByVal e As DataGridViewCellEventArgs) _
        Handles dataGridView1.CellMouseEnter

        Dim markingUnderMouse As Bitmap = _
            CType(dataGridView1.Rows(e.RowIndex). _
                Cells(e.ColumnIndex).Value, Bitmap)

        If markingUnderMouse Is blank Then
            dataGridView1.Cursor = Cursors.Default
        ElseIf markingUnderMouse Is o OrElse markingUnderMouse Is x Then
            dataGridView1.Cursor = Cursors.No
            ToolTip(e)
        End If
    End Sub

    Private Sub ToolTip( _
        ByVal e As DataGridViewCellEventArgs)

        Dim cell As DataGridViewImageCell = _
            CType(dataGridView1.Rows(e.RowIndex). _
            Cells(e.ColumnIndex), DataGridViewImageCell)
        Dim imageColumn As DataGridViewImageColumn = _
            CType(dataGridView1.Columns(cell.ColumnIndex), _
            DataGridViewImageColumn)

        cell.ToolTipText = imageColumn.Description
    End Sub

    Private Sub dataGridView1_CellMouseLeave(ByVal sender As Object, _
        ByVal e As DataGridViewCellEventArgs) _
        Handles dataGridView1.CellMouseLeave

        dataGridView1.Cursor = Cursors.Default
    End Sub

    Private Sub Stretch(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Button3.Click

        For Each column As DataGridViewImageColumn _
            In dataGridView1.Columns
            column.ImageLayout = DataGridViewImageCellLayout.Stretch
            column.Description = "Stretched image layout"
        Next
    End Sub

    Private Sub ZoomToImage(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Button4.Click

        For Each column As DataGridViewImageColumn _
            In dataGridView1.Columns
            column.ImageLayout = DataGridViewImageCellLayout.Zoom
            column.Description = "Zoomed image layout"
        Next
    End Sub

    Private Sub NormalImage(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Button5.Click

        For Each column As DataGridViewImageColumn _
            In dataGridView1.Columns
            column.ImageLayout = DataGridViewImageCellLayout.Normal
            column.Description = "Normal image layout"
        Next
    End Sub

    Private Sub MakeCellsLarger(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Button2.Click

        For Each column As DataGridViewImageColumn _
            In dataGridView1.Columns
            column.Width = column.Width * 2
        Next
        For Each row As DataGridViewRow In dataGridView1.Rows
            If row.IsNewRow Then Continue For
            row.Height = CInt(row.Height * 1.5)
        Next
    End Sub

    Private Function IsAWin() As Boolean
        If ARowIsSame() OrElse AColumnIsSame() OrElse ADiagonalIsSame() Then
            Return True
        Else 
            Return False
        End If
    End Function

    Private Function ARowIsSame() As Boolean
        Dim marking As Bitmap = Nothing
        If marking Is blank Then Return False
        For Each row As DataGridViewRow In dataGridView1.Rows
            If row.IsNewRow Then Continue For
            marking = CType(row.Cells(0).Value, Bitmap)
            If marking IsNot blank Then
                If marking Is row.Cells(1).Value AndAlso _
                    marking Is row.Cells(2).Value Then Return True
            End If
        Next
        Return False
    End Function

    Private Function AColumnIsSame() As Boolean

        Dim columnIndex As Integer = 0
        Dim marking As Bitmap
        Do
            marking = CType(dataGridView1.Rows(0).Cells(columnIndex).Value, _
                Bitmap)
            If marking IsNot blank Then
                If marking Is _
                    dataGridView1.Rows(1).Cells(columnIndex).Value _
                    AndAlso marking Is _
                    dataGridView1.Rows(2).Cells(columnIndex).Value _
                    Then Return True
            End If
            columnIndex = columnIndex + 1
        Loop While columnIndex < _
            dataGridView1.Columns.GetColumnCount( _
            DataGridViewElementStates.Visible)
        Return False
    End Function

    Private Function ADiagonalIsSame() As Boolean

        If LeftToRightDiagonalIsSame() Then Return True
        If RightToLeftDiagonalIsSame() Then Return True
        Return False
    End Function

    Private Function LeftToRightDiagonalIsSame() As Boolean
        Return IsDiagonalSame(0, 2)
    End Function

    Private Function RightToLeftDiagonalIsSame() As Boolean
        Return IsDiagonalSame(2, 0)
    End Function

    Private Function IsDiagonalSame(ByVal startingColumn As Integer, _
        ByVal lastColumn As Integer) As Boolean

        Dim marking As Bitmap = CType( _
            dataGridView1.Rows(0).Cells(startingColumn).Value, Bitmap)
        If marking Is blank Then Return False
        If marking Is dataGridView1.Rows(1).Cells(1).Value AndAlso _
            marking Is dataGridView1.Rows(2).Cells(lastColumn).Value _
            Then Return True
    End Function

    Private Sub ToggleTurn()
        If turn.Text.Equals(xString) Then turn.Text = oString _
        Else turn.Text = xString
    End Sub

    Private Function IsOsTurn() As Boolean
        If turn.Text.Equals(oString) Then Return True _
        Else  : Return False
    End Function

    <STAThread> Public Shared Sub Main()
        Application.Run(New TicTacToe())
    End Sub

End Class

설명

클래스는 DataGridViewImageColumn 이미지를 표시하는 셀을 DataGridViewColumn 논리적으로 호스트하는 데 사용되는 특수 형식의 클래스입니다. A DataGridViewImageColumn 에 연결 된 DataGridViewImageCell 에서 모든 DataGridViewRow 과 교차 하는 합니다. 각 셀은 속성 값에 따라 또는 IconDataGridViewImageCell.ValueIsIcon 포함 Image 할 수 있습니다. 아이콘 표시는 투명도가 있는 이미지를 수용하는 데 유용합니다.

기본적으로 빈 셀에는 기본 오류 그래픽이 표시됩니다. 또는 DBNull.Value와 같은 null 셀 값에 대해 이 그래픽이 나타나지 않도록 하려면 컨트롤에 행을 추가하기 전에 속성에서 반환하는 DefaultCellStyle 셀 스타일 개체의 속성을 로 null 설정합니다DataGridViewCellStyle.NullValue. 그러나 새 레코드의 행에는 영향을 주지 않습니다. 컨트롤 속성 값이 일 때 새 레코드의 행에 오류 그래픽이 true표시되지 않도록 하려면 컨트롤 RowsAddedAllowUserToAddRows 이벤트의 처리기에서 셀 값을 null 로 명시적으로 설정하거나 열 CellTemplate 속성을 를 반환null하는 재정 DefaultNewRowValue 의된 속성이 있는 파생 형식의 DataGridViewImageCellinstance 설정해야 합니다.

이 열 형식에 대 한 기본 정렬 모드 NotSortable합니다.

상속자 참고

파생 하는 경우 DataGridViewImageColumn 파생된 클래스에 새 속성 추가 재정의 해야 합니다 Clone() 복제 작업 중 새 속성을 복사 하는 방법입니다. 또한 기본 클래스를 호출 해야 Clone() 메서드는 기본 클래스의 속성이 새로운 셀에 복사 되도록 합니다.

생성자

DataGridViewImageColumn()

DataGridViewImageColumn 클래스의 새 인스턴스를 초기화하고, 이 인스턴스를 Image 형식의 셀 값에 사용할 수 있도록 구성합니다.

DataGridViewImageColumn(Boolean)

DataGridViewImageColumn 클래스의 새 인스턴스를 초기화하고, 선택적으로 이 인스턴스를 Icon 셀 값에 사용할 수 있도록 구성합니다.

속성

AutoSizeMode

열의 너비를 자동으로 조정하는 모드를 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
CellTemplate

셀을 새로 만드는 데 사용되는 템플릿을 가져오거나 설정합니다.

CellType

셀 템플릿의 런타임 형식을 가져옵니다.

(다음에서 상속됨 DataGridViewColumn)
ContextMenuStrip

열의 바로 가기 메뉴를 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
DataGridView

이 요소와 관련된 DataGridView 컨트롤을 가져옵니다.

(다음에서 상속됨 DataGridViewElement)
DataPropertyName

DataGridViewColumn이 바인딩되는 데이터베이스 열이나 데이터 소스 속성의 이름을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
DefaultCellStyle

열의 기본 셀 스타일을 가져오거나 설정합니다.

DefaultHeaderCellType

기본 머리글 셀의 런타임 형식을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewBand)
Description

열의 이미지를 설명하는 문자열을 가져오거나 설정합니다.

Displayed

밴드가 현재 화면에 표시되는지 여부를 나타내는 값을 가져옵니다.

(다음에서 상속됨 DataGridViewBand)
DisplayIndex

현재 표시된 열에 상대적인 열의 표시 순서를 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
DividerWidth

열 구분선의 너비(픽셀)를 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
FillWeight

열이 컨트롤에 있는 다른 채우기 모드 열의 너비에 상대적인 채우기 모드에 있는 경우 열의 너비를 나타내는 값을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
Frozen

DataGridView 컨트롤을 가로로 스크롤하면 열이 이동할지 여부를 나타내는 값을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
HasDefaultCellStyle

DefaultCellStyle 속성이 설정되었는지 여부를 나타내는 값을 가져옵니다.

(다음에서 상속됨 DataGridViewBand)
HeaderCell

열 머리글을 나타내는 DataGridViewColumnHeaderCell을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
HeaderCellCore

DataGridViewBand의 머리글 셀을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewBand)
HeaderText

열의 머리글 셀에 있는 캡션 텍스트를 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
Icon

셀의 Value 속성이 설정되어 있지 않고 셀의 ValueIsIcon 속성이 true로 설정된 경우 이 열의 셀에 표시된 아이콘을 가져오거나 설정합니다.

Image

셀의 Value 속성이 설정되어 있지 않고 셀의 ValueIsIcon 속성이 false로 설정된 경우 이 열의 셀에 표시된 이미지를 가져오거나 설정합니다.

ImageLayout

이 열의 셀에 있는 이미지 레이아웃을 가져오거나 설정합니다.

Index

DataGridView 컨트롤에서 밴드의 상대적 위치를 가져옵니다.

(다음에서 상속됨 DataGridViewBand)
InheritedAutoSizeMode

열에 적용되는 크기 조정 모드를 가져옵니다.

(다음에서 상속됨 DataGridViewColumn)
InheritedStyle

열에 현재 적용된 셀 스타일을 가져옵니다.

(다음에서 상속됨 DataGridViewColumn)
IsDataBound

열이 데이터 소스에 바인딩되어 있는지 여부를 나타내는 값을 가져옵니다.

(다음에서 상속됨 DataGridViewColumn)
IsRow

밴드가 행을 나타내는지 여부를 나타내는 값을 가져옵니다.

(다음에서 상속됨 DataGridViewBand)
MinimumWidth

열의 최소 너비(픽셀)를 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
Name

열 이름을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
ReadOnly

열의 셀을 편집할 수 있는지 여부를 나타내는 값을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
Resizable

열의 크기를 조정할 수 있는지 여부를 나타내는 값을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
Selected

밴드가 선택한 UI(사용자 인터페이스) 상태에 있는지 여부를 나타내는 값을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewBand)
Site

열의 사이트를 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
SortMode

열의 정렬 모드를 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
State

요소의 UI(사용자 인터페이스) 상태를 가져옵니다.

(다음에서 상속됨 DataGridViewElement)
Tag

밴드에 연결할 데이터가 포함된 개체를 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewBand)
ToolTipText

도구 설명에 사용되는 텍스트를 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
ValuesAreIcons

이 열의 셀에서 Icon 값을 표시할지 여부를 나타내는 값을 가져오거나 설정합니다.

ValueType

열의 셀 값에 대한 데이터 형식을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
Visible

열이 표시되는지 여부를 나타내는 값을 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)
Width

열의 현재 너비를 가져오거나 설정합니다.

(다음에서 상속됨 DataGridViewColumn)

메서드

Clone()

이 열과 정확하게 일치하는 복사본을 만듭니다.

Dispose()

DataGridViewBand에서 사용하는 모든 리소스를 해제합니다.

(다음에서 상속됨 DataGridViewBand)
Dispose(Boolean)

DataGridViewBand에서 사용하는 관리되지 않는 리소스를 해제하고, 관리되는 리소스를 선택적으로 해제할 수 있습니다.

(다음에서 상속됨 DataGridViewColumn)
Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetPreferredWidth(DataGridViewAutoSizeColumnMode, Boolean)

지정된 조건에 따라 열의 이상적인 너비를 계산합니다.

(다음에서 상속됨 DataGridViewColumn)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
OnDataGridViewChanged()

밴드가 다른 DataGridView에 연결될 때 호출됩니다.

(다음에서 상속됨 DataGridViewBand)
RaiseCellClick(DataGridViewCellEventArgs)

CellClick 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
RaiseCellContentClick(DataGridViewCellEventArgs)

CellContentClick 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
RaiseCellContentDoubleClick(DataGridViewCellEventArgs)

CellContentDoubleClick 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
RaiseCellValueChanged(DataGridViewCellEventArgs)

CellValueChanged 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
RaiseDataError(DataGridViewDataErrorEventArgs)

DataError 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
RaiseMouseWheel(MouseEventArgs)

MouseWheel 이벤트를 발생시킵니다.

(다음에서 상속됨 DataGridViewElement)
ToString()

열을 설명하는 문자열을 가져옵니다.

이벤트

Disposed

DataGridViewColumn이 삭제될 때 발생합니다.

(다음에서 상속됨 DataGridViewColumn)

적용 대상

추가 정보