unit HangManPainter;
interface
uses
Classes, ExtCtrls;
type
THangManPainter =
class( TComponent )
private
FPaintBox : TPaintBox;
FCharCount : Integer;
FChars :
array of Char;
procedure SetCharCount(
const Value : Integer );
function GetChars(
const index : Integer ) : Char;
procedure SetChars(
const index : Integer;
const Value : Char );
function GetCharCount : Integer;
protected
procedure OnPaint( Sender : TObject );
public
constructor Create( APaintBox : TPaintBox );
reintroduce;
property CharCount : Integer
read GetCharCount
write SetCharCount;
property Chars[
const index : Integer] : Char
read GetChars
write SetChars;
end;
implementation
uses
Math, Graphics, Types;
{ THangManPainter }
constructor THangManPainter.Create( APaintBox : TPaintBox );
begin
inherited Create( APaintBox );
FPaintBox := APaintBox;
FPaintBox.OnPaint := OnPaint;
end;
function THangManPainter.GetCharCount : Integer;
begin
Result := High( FChars ) - Low( FChars ) + 1;
end;
function THangManPainter.GetChars(
const index : Integer ) : Char;
begin
Result := FChars[
Index];
end;
procedure THangManPainter.OnPaint( Sender : TObject );
var
LIdx : Integer;
LCanvas : TCanvas;
LRect : TRect;
LText :
string;
begin
if CharCount <= 0
then
Exit;
LCanvas := FPaintBox.Canvas;
LCanvas.Brush.Style := bsSolid;
LCanvas.Pen.Color := clBlack;
LCanvas.Pen.Width := 4;
LCanvas.Pen.Style := psSolid;
LCanvas.Font.Height := 30;
for LIdx := 0
to Pred( CharCount )
do
begin
if FChars[LIdx] = #0
then
LCanvas.Brush.Color := clWhite
else
LCanvas.Brush.Color := clYellow;
LRect.Left := 50 * LIdx + 30;
LRect.Top := 30;
LRect.Width := 30;
LRect.Height := 50;
LCanvas.RoundRect( LRect, 5, 5 );
InflateRect( LRect, - 3, - 3 );
LText := FChars[LIdx];
LCanvas.TextRect( LRect, LText, [tfCenter, tfSingleLine, tfVerticalCenter] );
// LCanvas.TextRect( LRect, LText );
end;
end;
procedure THangManPainter.SetCharCount(
const Value : Integer );
begin
SetLength( FChars, Value );
FPaintBox.Invalidate;
end;
procedure THangManPainter.SetChars(
const index : Integer;
const Value : Char );
begin
FChars[
Index] := Value;
FPaintBox.Invalidate;
end;
end.