Click here to Skip to main content
15,896,606 members
Articles / Mobile Apps

A C# implementation of Reversi (Othello) Game for PocketPC and Windows

Rate me:
Please Sign up or sign in to vote.
4.75/5 (13 votes)
4 Aug 20043 min read 91.8K   4.4K   36  
A C# implementation of Reversi (Othello) Game for PocketPC and Windows.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace gma.Mobile.Controls
{
	public class ImageButton : Control
		{
			private Image image;
			//private bool bPushed;
			private Bitmap m_bmpOffscreen;

			public Image Image
			{
				get
				{
					return image;	
				}
				set
				{
					image = value;
				}
			}
		
			public ImageButton()
			{
				//bPushed = false;
				//default minimal size
				this.Size = new Size(21, 21); 
			}

			protected override void OnPaint(System.Windows.Forms.PaintEventArgs e )
			{
				Graphics gxOff;	   //Offscreen graphics
				Rectangle imgRect; //image rectangle
				//Brush backBrush;   //brush for filling a backcolor

				if (m_bmpOffscreen == null) //Bitmap for doublebuffering
				{
					m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
				}
			
				gxOff = Graphics.FromImage(m_bmpOffscreen);

				gxOff.Clear(this.BackColor);
			
				/*
				if (!bPushed) 
					backBrush = new SolidBrush(Parent.BackColor);
				else //change the background when it's pressed
					backBrush = new SolidBrush(Color.LightGray);
					*/

				//gxOff.FillRectangle(backBrush, this.ClientRectangle);

				if (image != null)
				{
					//Center the image relativelly to the control
					int imageLeft = (this.Width - image.Width) / 2;
					int imageTop = (this.Height - image.Height) / 2;

					//if (!bPushed)
					{
						imgRect = new Rectangle(imageLeft, imageTop, image.Width, image.Height);
					}
					//else //The button was pressed
					{
						//Shift the image by one pixel
						//imgRect = new Rectangle(imageLeft + 1 , imageTop +1, image.Width, image.Height);
					}
					//Set transparent key
					ImageAttributes imageAttr = new ImageAttributes();
					//imageAttr.SetColorKey(BackgroundImageColor(image), BackgroundImageColor(image));
					//Draw image
					gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr); 	
				}
				else
				{
					//Rahmen
					Rectangle rc = this.ClientRectangle;
					rc.Width--;
					rc.Height--;
					//gxOff.DrawRectangle(new Pen(Color.Gray), rc);
					// Text
					Font textFont = new Font("Impact", 10, FontStyle.Bold);
					SolidBrush textBrush = new SolidBrush(Color.Navy);
					gxOff.DrawString(this.Text, textFont, textBrush, new Rectangle(5, 10,  this.Width, this.Height));
				}

				/*
				if (bPushed) //The button was pressed
				{
					//Prepare rectangle
					Rectangle rc = this.ClientRectangle;
					rc.Width--;
					rc.Height--;
					//Draw rectangle
					gxOff.DrawRectangle(new Pen(Color.Black), rc);
				}
				*/

				//Draw from the memory bitmap
				e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);

				base.OnPaint(e);
			}

			protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e )
			{
				//Do nothing
			}

			protected override void OnMouseDown ( System.Windows.Forms.MouseEventArgs e )
			{
				//bPushed = true;
				this.Invalidate();
			}

			protected override void OnMouseUp ( System.Windows.Forms.MouseEventArgs e )
			{
				//bPushed = false;
				this.Invalidate();
			}

			private Color BackgroundImageColor(Image image)
			{
				Bitmap bmp = new Bitmap(image);
				return bmp.GetPixel(0, 0);
			}

			private object _tag;
			public object Tag
			{
				get {return _tag;}
				set {_tag=value;}
			}

		}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Germany Germany
Tweeter: @gmamaladze
Google+: gmamaladze
Blog: gmamaladze.wordpress.com

Comments and Discussions