segunda-feira, 23 de janeiro de 2012

Delphi - Stringgrid com Alinhamento à Direita, à Esquerda e Centralizado


Nesta dica iremos montar um StringGrid com rotinas para alinhar os valores à Direita, à Esquerda e Centralizado.
private
  { Private declarations }
  TempString: array[0..80] of char;
  Labor: double;
  Materiais: double;
  Shipping: double;
  COGS: double;
  Price: double;
  Sales: double;
  Revenue: double;
  Income: double;
  GrowthRate: double;
  procedure InitGrid;
  procedure Calculate;
public
  { Public declarations }
end;

var
  Form1: TForm1;

implementation

uses printers;
{$R *.DFM}

type
  THack = class(TStringGrid)
  end;
const
  Totals: set of 0..10 = [3, 8, 10];
  FmtCentered = DT_SingleLine or DT_VCenter or DT_NoClip or DT_Center;
  FmtLeft = DT_SingleLine or DT_VCenter or DT_NoClip or DT_Left;
  FmtRight = DT_SingleLine or DT_VCenter or DT_NoClip or DT_Right;
procedure TForm1.FormCreate(Sender: TObject);
begin
  GrowthRate := 10.0;
  Labor := 0.0;
  Materiais := 0.0;
  Shipping := 0.0;
  COGS := 0.0;
  Sales := 1000.0;
  Price := 25.0;
  Revenue := 0.0;
  Income := 0.0;
  InitGrid;
  Calculate;
end;

procedure TForm1.Calculate;
const
  LbrRate = 2.00;
  MtrlRate = 1.50;
  ShpRate = 1.00;
  FmtMoney = '%1.0m';
  FmtNumber = '%1.0n';
var
  i: integer;
  OldSales: double;
begin
  OldSales := Sales;
  for i := 1 to StringGrid1.RowCount - 1 do
  begin
    Revenue := Sales * Price;
    Labor := LbrRate * Sales;
    Materiais := MtrlRate * Sales;
    Shipping := ShpRate * Sales;
    COGS := Labor + Materiais + Shipping;
    Income := Revenue - COGS;
    with StringGrid1 do
    begin
      Cells[i, 1] := Format(FmtMoney, [Price]);
      Cells[i, 2] := Format(FmtMoney, [Sales]);
      Cells[i, 3] := Format(FmtMoney, [Revenue]);
      Cells[i, 5] := Format(FmtMoney, [Labor]);
      Cells[i, 6] := Format(FmtMoney, [Materiais]);
      Cells[i, 7] := Format(FmtMoney, [Shipping]);
      Cells[i, 8] := Format(FmtMoney, [COGS]);
      Cells[i, 10] := Format(FmtMoney, [Income]);
    end;
    Sales := Sales * GrowthRate;
  end;
  Sales := OldSales;
end;

procedure TForm1.InitGrid;
begin
  with StringGrid1 do
  begin
    ColWidths[0] := 100;
    Cells[1, 0] := 'Qtr 1';
    Cells[2, 0] := 'Qtr 2';
    Cells[3, 0] := 'Qtr 3';
    Cells[4, 0] := 'Qtr 4';
    Cells[0, 1] := 'Price';
    Cells[0, 2] := 'Amount Sold';
    Cells[0, 3] := 'Total Revenue';
    Cells[0, 5] := 'Labor';
    Cells[0, 6] := 'Materiais';
    Cells[0, 7] := 'Shipping';
    Cells[0, 8] := 'Cost Of Goods Sold';
    Cells[0, 10] := 'Income';
  end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Longint; Rect: TRect; State: TGridDrawState);
var
  ACol: integer absolute Col;
  ARow: integer absolute Row;
  Format: integer;
begin
  if ACol = 0 then
    Exit;
  with StringGrid1 do
  begin
    Canvas.Font := Font;
    if State = [gdFixed] then
      Format := FmtCentered
    else
    begin
      // Define centralizado
      Format := FmtCentered;
      // Define alinhado a direita
      Format := FmtRight;
      if ARow in Totals then
        Canvas.Font.Style := [fsBold]
      else
        Canvas.Font.Style := [];
      if State = [gdSelected, gdFocused] then
      begin
        Canvas.Font.Color := clHighLightText;
        Canvas.Brush.Color := clHighLight;
      end
      else
      begin
        Font.Color := clWindowText;
        Canvas.Brush.Color := clWindow;
      end;
    end;
    Canvas.FillRect(Rect);
    Rect.Right := Rect.Right - 3;
    StrPCopy(@(TempString[0]), StringGrid1.Cells[ACol, ARow]);
    DrawText(Canvas.Handle, TempString, -1, Rect, Format);
  end;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; Col, Row: Longint;
  var CanSelect: Boolean);
begin
  if (Col = 1) and ((Row = 1) or (Row = 2)) then
    StringGrid1.Options := StringGrid1.Options + [goEditing]
  else
    StringGrid1.Options := StringGrid1.Options - [goEditing];
end;

procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol, ARow: Longint; const Value: string);
begin
  if Value <> '' then
    if StringGrid1.EditorMode = False then
    begin
      case ARow of
        1: Price := StrToInt(Value);
        2: Sales := StrToInt(Value);
      end;
      if THack(StringGrid1).InplaceEditor.Modified then
        Calculate;
    end;
end;

procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol, ARow: Longint; var Value: string);
begin
  case ARow of
    1: Value := IntToStr(Trunc(Price));
    2: Value := IntToStr(Trunc(Sales));
  end;
end;

end.

Nenhum comentário:

Postar um comentário