Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi StringGrid Inhalte zentrieren. (https://www.delphipraxis.net/12704-stringgrid-inhalte-zentrieren.html)

Piro 2. Dez 2003 17:44


StringGrid Inhalte zentrieren.
 
Moin

ich wollte gerne mal wissen mit welcher Anweisung man den Inhalt eines StringGrid zentriert.

d.h.: Überschrift und Spalteninhalte sollen zentriert werden

Delphi 3 Prof. benutze ich.

Danke.

himitsu 2. Dez 2003 17:56

Re: StringGrid Inhalte zentrieren.
 
Ein Hallöle von http://www.FrankNStein.de/Smiley-Wolke.gif,

Delphi-Quellcode:
Procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
  Begin
    If (ACol = 4) and (ARow = 2) Then Begin {Zelle auswählen}
      StringGrid1.Canvas.Brush.Color := StringGrid1.Color;
      StringGrid1.Canvas.FillRect(Rect);
      StringGrid1.Canvas.Font := StringGrid1.Font;
      StringGrid1.Canvas.TextRect(Rect, Rect.Left + Max(Rect.Right - Rect.Left - 
        TextWidth(StringGrid1.Cells[ACol, ARow]), 0), Rect.Top + 2, StringGrid1.Cells[ACol, ARow]);
    End;
  End;
http://www.delphipraxis.net/images/common/divider.jpg
http://www.FrankNStein.de/Smiley-Kuss.gif * * http://www.FrankNStein.de/Smiley-Spinne.gif * * * http://www.FrankNStein.de/Smiley-Winken.gif

Piro 2. Dez 2003 18:29

Re: StringGrid Inhalte zentrieren.
 
Irgenndwie will das nicht bei mir.

Delphi-Quellcode:
procedure Tfrm_setup.sg_preislisteDrawCell(Sender: TObject; Col,
  Row: Integer; Rect: TRect; State: TGridDrawState);
begin
If (ACol = 4) and (ARow = 2) Then Begin {Zelle auswählen}
      sg_preisliste.Canvas.FillRect(Rect);
      sg_preisliste.Canvas.TextRect(Rect, Rect.Left + Max(Rect.Right - Rect.Left -
        TextWidth(sg_preisliste.Cells[ACol, ARow]), 0), Rect.Top + 2, sg_preisliste.Cells[ACol, ARow]);
    End;
end;
Fehlermeldung: er kennt ACol, ARow, Max und TextWidth sind nicht definiert. Aber das sind doch systemvariabeln, oder?

Piro 2. Dez 2003 20:42

Re: StringGrid Inhalte zentrieren.
 
kann mir noch einer eine andere Lösung sagen.

huberlix 3. Dez 2003 19:10

Re: StringGrid Inhalte zentrieren.
 
Code:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  s: string;
begin
  // Text der Zelle holen
  s:=StringGrid1.Cells[ACol,ARow]
  StringGrid1.Canvas.FillRect(Rect); // Zellfarbe zeichnen
  DrawText(StringGrid1.Canvas.Handle, PChar(s), Length(s), Rect, DT_SINGLELINE or DT_Center or DT_VCENTER); // Text zentriert zeichnen
...........
...........
..........

DT_CENTER zentriert horizontal, DT_VCENTER zentriert vertikal.

Gruß,Bernd

whiteF 26. Jun 2011 12:57

AW: StringGrid Inhalte zentrieren.
 
@ Huberlix

diese variante geht aber nicht wenn man sein Grid.DrawingStyle := gdsGradient umgestellt hat.

Wolfgang Mix 26. Jun 2011 13:52

AW: StringGrid Inhalte zentrieren.
 
Vlt. kannst du das hier umstricken:

Delphi-Quellcode:
//http://www.delphi-forum.de
//procedure WriteTextAligned und
//procedure TForm1.StringGrid1DrawCell
procedure WriteTextAligned(Canvas: TCanvas; Rect: TRect; Alignment: TAlignment; Text: string);
var xPos, yPos: integer;
begin
xPos := Rect.Left + 2; // Standard verhalten
yPos := Rect.Top + 2; // Standard verhalten
with Canvas do
  begin
    xPos := Rect.Left + (Rect.Right - Rect.Left - TextWidth(Text) - 2);
    TextRect(Rect, xPos, yPos, Text);
  end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
begin
  // Alle Spalten rechtsbündig
  WriteTextAligned(TStringGrid(Sender).Canvas, Rect, taRightJustify, TStringGrid(Sender).Cells[Acol, ARow]);
end;
[Edit]

http://www.delphi-forum.de/topic_Text+im+StringGrid+rechtsbuendig+ausrichten_ 6188,0.html

[/Edit]

madtom 27. Jun 2011 21:46

AW: StringGrid Inhalte zentrieren.
 
Hier noch ein Beispiel dazu von Peter Below (TeamB)

Delphi-Quellcode:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; nCol, nRow: Longint; nRect: TRect;
  State: TGridDrawState);
var
  Cpos: Integer;
begin
  with (Sender as TStringGrid) do
    with Canvas do
    begin
      Font := (Sender as TStringGrid).Font;

      if (gdSelected in State) and (Sender = ActiveControl) then
      begin
        Brush.Color := clHighlight;
        Font.Color := clHighlightText;
      end
      else if (gdFixed in State) then
        Brush.Color := (Sender as TStringGrid).FixedColor
      else
        Brush.Color := (Sender as TStringGrid).Color;
      FillRect(nRect);
      SetBkMode(Handle, TRANSPARENT);

      if (nRow = 0) or (nCol = 2) then
      { Centered Text }
      begin
        SetTextAlign(Handle, TA_CENTER);
        with nRect do
          Cpos := Left + ((Right - Left) div 2) - 1;
        TextOut(Cpos, nRect.Top + 2, Cells[nCol, nRow]);
      end
      else if (nCol = 3) or (nCol = 4) then
      { Right Justified Text }
      begin
        SetTextAlign(Handle, TA_Right);
        TextOut(nRect.Right - 2, nRect.Top + 2, Cells[nCol, nRow]);
      end
      else
      begin
        { normal Left Justified Text }
        SetTextAlign(Handle, TA_LEFT);
        TextOut(nRect.Left + 2, nRect.Top + 2, Cells[nCol, nRow]);
      end;
    end;
end;
Gruß madtom :twisted:


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:14 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz