C # The wuziqi program for learning WinForms mouse events and using GDI +

Source: Internet
Author: User

I wrote a small program that can be used to learn C #.

The program is compiled using the VS. NET environment. You only need to install the. NET Framework SDK on your machine to run it.

Source code and execution files can be downloaded

Http://www.wh-adv.com/download/five.zip

You do not want to download or read the source code (image resources need to be downloaded ).

Namespace Leimom. FiveChess

{

Using System;

Using System. Drawing;

Using System. Collections;

Using System. ComponentModel;

Using System. WinForms;

Using System. Data;

/// <Summary>

/// Summary description for Form1.

/// </Summary>

Public class FiveForm: System. WinForms. Form

{

/// <Summary>

/// Required designer variable.

/// </Summary>

Private System. ComponentModel. Container components;

Private System. WinForms. ImageList imageListbw;

// Define the hot Rectangle

Private Rectangle [] pointSquares;

// Chess information

Private int [] chessTable;

Private int nextTurn;

Private const int bTurn = 1;

Private const int wTurn = 2;

Private Stack chessIndex;

Public FiveForm ()

{

//

// Required for Windows Form Designer support

//

InitializeComponent ();

//

// TODO: Add any constructor code after InitializeComponent call

//

ChessIndex = new Stack ();

NextTurn = bTurn;

ChessTable = new int [225];

PointSquares = new Rectangle [225];

Size size = new Size (18, 18 );

Int x = 0;

Int y = 0;

For (int I = 0; I <225; I ++)

{

X = I % 15;

Y = I/15;

PointSquares [I]. Size = size;

PointSquares [I]. Offset (9 + x * 20, 6 + y * 20 );

ChessTable [I] = 0;

}

}

Protected override void OnPaint (PaintEventArgs e)

{

// You may paint

Graphics g = e. Graphics;

}

Protected override void OnMouseDown (System. WinForms. MouseEventArgs e)

{

Switch (e. Button)

{

// Take left button down

Case MouseButtons. Left:

OnLButtonDown (new Point (e. X, e. Y ));

Break;

// Take right button down

Case MouseButtons. Right:

OnRButtonDown (new Point (e. X, e. Y ));

Break;

}

Base. OnMouseDown (e );

}

Private void OnLButtonDown (Point p)

{

Int nPos = GetRectID (p );

// Click hot Rectangle witch have no chess

If (nPos! =-1 & chessTable [nPos] = 0)

{

Graphics g = this. CreateGraphics ();

If (nextTurn = bTurn)

{

// Draw white chess

DrawBlack (g, nPos );

ChessTable [nPos] = bTurn;

NextTurn = wTurn;

ChessIndex. Push (bTurn );

ChessIndex. Push (nPos );

}

Else

{

// Draw Black chess

DrawWhite (g, nPos );

ChessTable [nPos] = wTurn;

NextTurn = bTurn;

ChessIndex. Push (wTurn );

ChessIndex. Push (nPos );

}

G. Dispose ();

// Witch win

CheckGameResult (nPos, nextTurn );

}

}

Private void CheckGameResult (int nPos, int nextTurn)

{

// Witch win

Stack isFive = new Stack ();

Int thisTurn = (nextTurn = bTurn )? WTurn: bTurn;

Int x = nPos % 15;

Int y = nPos/15;

// Scan x have five

For (int I = 0; I <15; I ++)

{

If (chessTable [y * 15 + I] = thisTurn)

{

IsFive. Push (y * 15 + I );

If (isFive. Count = 5)

{

MessageBox. Show ("Game Over", "Notes", MessageBox. OK );

ReSetGame ();

Return;

}

}

Else

{

IsFive. Clear ();

}

}

IsFive. Clear ();

// Scan y have five

For (int I = 0; I <15; I ++)

{

If (chessTable [I * 15 + x] = thisTurn)

{

IsFive. Push (I * 15 + x );

If (isFive. Count = 5)

{

MessageBox. Show ("Game Over", "Notes", MessageBox. OK );

ReSetGame ();

Return;

}

}

Else

{

IsFive. Clear ();

}

}

IsFive. Clear ();

// Scan x = y have five

For (int I =-14; I <15; I ++)

{

If (x + I <0 | x + I> 14 | y-I <0 | y-I> 14)

{

Continue;

}

Else

{

If (chessTable [(y-I) * 15 + x + I] = thisTurn)

{

IsFive. Push (y-I) * 15 + x + I );

If (isFive. Count = 5)

{

MessageBox. Show ("Game Over", "Notes", MessageBox. OK );

ReSetGame ();

Return;

}

}

Else

{

IsFive. Clear ();

}

}

}

IsFive. Clear ();

// Scan x =-y have five

For (int I =-14; I <15; I ++)

{

If (x + I <0 | x + I> 14 | y + I <0 | y + I> 14)

{

Continue;

}

Else

{

If (chessTable [(y + I) * 15 + x + I] = thisTurn)

{

IsFive. Push (y + I) * 15 + x + I );

If (isFive. Count = 5)

{

MessageBox. Show ("Game Over", "Notes", MessageBox. OK );

ReSetGame ();

Return;

}

}

Else

{

IsFive. Clear ();

}

}

}

IsFive. Clear ();

}

Private void ReSetGame ()

{

// Reset game

NextTurn = bTurn;

For (int I = 0; I <225; I ++)

{

ChessTable [I] = 0;

}

This. Invalidate ();

}

Private int GetRectID (Point p)

{

// Get witch rectangle click

For (int I = 0; I <225; I ++)

{

If (pointSquares [I]. Contains (p ))

{

Return I;

}

}

Return-1;

}

Private void OnRButtonDown (Point p)

{

// Regret chess

Int nPos, x, y;

If (chessIndex. Count! = 0)

{

NPos = (int) chessIndex. Pop ();

X = nPos % 15;

Y = nPos/15;

ChessTable [nPos] = 0;

NextTurn = (int) chessIndex. Pop ();

This. Invalidate (new Rectangle (new Point (8 + x * 20, 5 + y * 20), new Size (20, 20 )));

}

}

Private void DrawBlack (Graphics g, int nPos)

{

// Draw Black chess

Int x, y;

X = nPos % 15;

Y = nPos/15;

ImageListbw. DrawImage (g, 8 + 20 * x, 5 + 20 * y, 20, 20, 0, 0 );

}

Private void DrawWhite (Graphics g, int nPos)

{

// Draw White chess

Int x, y;

X = nPos % 15;

Y = nPos/15;

ImageListbw. DrawImage (g, 8 + 20 * x, 5 + 20 * y, 20, 20, 0, 1 );

}

/// <Summary>

/// Clean up any resources being used.

/// </Summary>

Public override void Dispose ()

{

Base. Dispose ();

Components. Dispose ();

}

/// <Summary>

/// Required method for Designer support-do not modify

/// The contents of this method with the code editor.

/// </Summary>

Private void InitializeComponent ()

{

System. Resources. ResourceManager resources = new System. Resources. ResourceManager (typeof (FiveForm ));

This. components = new System. ComponentModel. Container ();

This. imageListbw = new System. WinForms. ImageList ();

// @ This. TrayHeight = 90;

// @ This. TrayLargeIcon = false;

// @ This. TrayAutoArrange = true;

// @ ImageListbw. SetLocation (new System. Drawing. Point (7,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.