UNIT Unit1;
{$mode objfpc}{$H+}
INTERFACE
USES
Classes,SysUtils,Forms,Controls,Graphics;
TYPE
TBoardRec = Record
R:TRect;
End;
TYPE
TForm1 = class(TForm)
Procedure FormCreate (Sender:TObject);
Procedure FormPaint (Sender:TObject);
Procedure FormMouseMove (Sender:TObject;Shift:TShiftState;X,Y:Integer);
Procedure FormMouseDown (Sender:TObject;Button:TMouseButton;
Shift:TShiftState;X,Y:Integer);
Procedure FormClose (Sender:TObject;Var CloseAction:TCloseAction);
Procedure GetRect (iX,iY:Integer;Out iCX,iCY:Integer);
PRIVATE
arrBoard: Array[1..8,1..8] Of TBoardRec;
rBoard:TRect;iCell,iMX,iMY:Integer;BMP:TBitmap; // DoubleBuffering ...
End;
VAR
Form1: TForm1;
IMPLEMENTATION
{$R *.lfm}
Procedure TForm1.FormPaint(Sender:TObject);
Var
iy,ix,iCount,iSpaceX,iSpaceY:Integer;booColor:Boolean;
Begin
booColor:= True;
iSpaceX:= -iCell;
iSpaceY:= 0;
iCount:= 0;
For iy:= 1 To 8
Do
For ix:= 1 To 8
Do
Begin
Inc(iCount);
If iCount = 9
Then
Begin
iCount:= 1;
Inc(iSpaceY,iCell);
iSpaceX:= -iCell;
booColor:= Not booColor;
End;
Inc(iSpaceX,iCell);
// Set Color
If booColor
Then BMP.Canvas.Brush.Color:= clWhite
Else BMP.Canvas.Brush.Color:= clBlack;
// Fill RectArray
arrBoard[iy,ix].R:= Rect(ClientRect.Left+iSpaceX,
ClientRect.Top+iSpaceY,ClientRect.Left+iSpaceX+iCell,
ClientRect.Top+iSpaceY+iCell);
// Draw Cell
BMP.Canvas.FillRect(arrBoard[iy,ix].R);
booColor:= Not booColor;
End;
// Draw BMP Buffer On Form Canvas
Canvas.Draw(0,0,BMP);
End;
Function InsideRect(R:TRect;X,Y:Integer):Boolean;
Begin
Result:= False;
If (X >= R.Left) And (Y <= R.Bottom) And (X <= R.Right) And (Y >= R.Top)
Then Result:= True;
End;
Procedure TForm1.FormMouseMove(Sender:TObject;Shift:TShiftState;X,Y:Integer);
Begin
// If You Need X,Y In OnPaint
iMX:= X;iMY:= Y;
Invalidate;
End;
Procedure TForm1.GetRect(iX,iY:Integer;Out iCX,iCY:Integer);
Var
d:Double;
Begin
d:= (iY-rBoard.Top)/iCell;
iCY:= Trunc(d);
If Frac(d) <> 0 Then Inc(iCY);
If iCY > 8 Then iCY:= 8;
If iCY < 1 Then iCY:= 1;
d:= (iX-rBoard.Left)/iCell;
iCX:= Trunc(d);
If Frac(d) <> 0 Then Inc(iCX);
If iCX > 8 Then iCX:= 8;
If iCX < 1 Then iCX:= 1;
End;
Procedure TForm1.FormMouseDown
(Sender:TObject;Button:TMouseButton;Shift:TShiftState;X,Y:Integer);
Var
iCX,iCY:Integer;
arrAH: Array[1..8] Of String = ('a','b','c','d','e','f','g','h');
Begin
If InsideRect(rBoard,X,Y)
Then
Begin
GetRect(X,Y,iCX,iCY);
Caption:= 'Array Cell (iy, ix): '+IntToStr(iCY)+' , '+IntToStr(iCX)+
' Chess Notation: '+arrAH[iCX]+IntToStr(((8-iCY)+1));
End;
End;
Procedure TForm1.FormCreate(Sender:TObject);
Begin
iCell:= 50;
rBoard:= Rect(ClientRect.Left,ClientRect.Top,
ClientRect.Left+400,ClientRect.Top+400);
Caption:= 'Click on a cell ...';
BMP:= TBitmap.Create;
BMP.SetSize(400,400);
End;
Procedure TForm1.FormClose(Sender:TObject;Var CloseAction:TCloseAction);
Begin
BMP.Free;
End;
END.