unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, ComCtrls, StdCtrls;
type
TForm1 =
class (TForm)
edValuePrecision: TEdit;
UpDown1: TUpDown;
StringGrid1: TStringGrid;
procedure UpDown1ChangingEx(Sender: TObject;
var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection);
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
private
{ Private-Deklarationen }
fFormatValueString :
String;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Options := StringGrid1.Options + [goEditing];
UpDown1.Min := 0;
UpDown1.Max := 16;
UpDown1.Position := 2;
end;
Procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
Var aCellString :
String;
aCellValue : Double;
begin
aCellString := StringGrid1.Cells [aCol, aRow];
//do we display a data cell?
If (aRow >= StringGrid1.FixedRows)
and (aCol >= StringGrid1.FixedCols)
THen
Begin
//adjust background colour when cell is selected
If gdSelected
In State
THen
Begin
StringGrid1.Canvas.Brush.Color := clSkyBlue;
StringGrid1.Canvas.FillRect (Rect);
end
Else
Begin
StringGrid1.Canvas.Brush.Color := clWhite;
StringGrid1.Canvas.FillRect (Rect);
end;
// does the cell contain a value?
If TryStrToFloat (aCellString, aCellValue)
THen
Begin
// if so, display formatted value
StringGrid1.Canvas.Font.Color := clBlack;
aCellString := Format (form1.fFormatValueString, [aCellValue]);
DrawText (StringGrid1.Canvas.Handle,
PChar (aCellString),
Length (aCellString),
Rect,
DT_SINGLELINE
or DT_RIGHT );
end
Else
Begin
// otherwise display the raw string in a different colour
StringGrid1.Canvas.Font.Color := clRed;
DrawText (StringGrid1.Canvas.Handle,
PChar (aCellString),
Length (aCellString),
Rect,
DT_SINGLELINE
or DT_LEFT );
end;
Exit;
end;
// upper left corner for matrix title
If (aRow = 0)
And (aCol = 0)
THen
Begin
StringGrid1.Canvas.Brush.Color := clMenu;
StringGrid1.Canvas.FillRect (Rect);
//reduce the grid rectangle to keep the borders
Inc (Rect.Left, 2);
Dec (Rect.Right, 2);
// display the title of the matrix in the upper left corner
StringGrid1.Canvas.Font.Color := clRed;
DrawText (StringGrid1.Canvas.Handle,
PChar (aCellString),
Length (aCellString),
Rect,
DT_SINGLELINE
or DT_LEFT );
Exit;
end;
// do we perform actions in the cells for the column-/row-captions?
If (aRow = StringGrid1.FixedRows-1)
And (aCol > StringGrid1.FixedCols-1)
Or
(aRow > StringGrid1.FixedRows-1)
And (aCol = StringGrid1.FixedCols-1)
THen
Begin
StringGrid1.Canvas.Brush.Color := cl3dLight;
StringGrid1.Canvas.FillRect (Rect);
//reduce the grid rectangle to keep the borders
Inc (Rect.Left, 2);
Dec (Rect.Right, 2);
StringGrid1.Canvas.Font.Color := clBlue;
Drawtext (StringGrid1.Canvas.Handle,
PChar (aCellString),
Length (aCellString),
Rect,
DT_SINGLELINE
or DT_LEFT );
end;
end;
procedure TForm1.UpDown1ChangingEx(Sender: TObject;
var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection);
begin
// renew the format string
If NewValue >= 0
THen fFormatValueString := '
%8.'+IntToStr (NewValue)+'
f'
Else fFormatValueString := '
%8.0f';
// force the stringgrid to update
StringGrid1.Invalidate;
StringGrid1.Repaint;
end;
end.