Ir ao topo

Tecnobyte

Logomarca da Tecnobyte
Contato por WhatsApp

WhatsApp

(69) 3421-6756

Contato por Telefone

(69) 3421-6756

(69) 3421-6757

Enviar mensagem

Enviar

mensagem

Contato por Facebook

Facebook

Vídeos

Vídeos

Atendimento de segunda a sexta, das 08h00 às 19h00 (horário de Brasília).

Banner

Delphi - Outros

Como alinhar ao centro e à direita em StringGrid?

1. Coloque no formulário um componente TStringGrid.
2. No evento OnCreate do formulário escreva:

procedure TForm1.FormCreate(Sender: TObject);
begin
  { Número de linhas }
  StringGrid1.RowCount := 5;
  { Número de colunas }
  StringGrid1.ColCount := 3;
  { Linhas fixas }
  StringGrid1.FixedRows := 1;
  { Colunas fixas }
  StringGrid1.FixedCols := 0;
  { Largura padrao das colunas (em pontos) }
  StringGrid1.DefaultColWidth := 80;
  { Permite editar }
  StringGrid1.Options :=
    StringGrid1.Options + [goEditing];
  { Cabeçalho }
  StringGrid1.Cells[0,0] := 'Esquerda';
  StringGrid1.Cells[1,0] := 'Centro';
  StringGrid1.Cells[2,0] := 'Direita';
end;

3. No evento OnDrawCell do StringGrid escreva:

var
  LarguraTexto, AlturaTexto, X, Y: integer;
  Texto: string;
begin
  { Pega o texto da célula }
  Texto := StringGrid1.Cells[ACol, ARow];

  { Calcura largura e altura (em pontos) do texto }
  LarguraTexto := StringGrid1.Canvas.TextWidth(Texto);
  AlturaTexto := StringGrid1.Canvas.TextHeight(Texto);

  { Calcula a posição horizontal do início do texto }
  if ACol = 0 then { Esquerda }
    X := Rect.Left + 2
  else if ACol = 1 then { Centro }
    X := Rect.Left + (Rect.Right - Rect.Left) div 2 -
      LarguraTexto div 2
  else { Direita }
    X := Rect.Right - LarguraTexto - 2;

  { Calcula a posição vertical do início do texto para
    que seja impresso no centro (verticalmente) da célula }
  Y := Rect.Top + (Rect.Bottom - Rect.Top) div 2 -
    AlturaTexto div 2;

  { Pinta o texto }
  StringGrid1.Canvas.TextRect(Rect, X, Y, Texto);
end;

Observações

Uma técnica semelhante a esta pode ser usada para pintar figuras nas células do StringGrid.

O conteúdo desta página pode ajudar alguém? Compartilhe!