AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

StringGrid Inhalte zentrieren.

Ein Thema von Piro · begonnen am 2. Dez 2003 · letzter Beitrag vom 27. Jun 2011
Antwort Antwort
huberlix

Registriert seit: 10. Nov 2003
46 Beiträge
 
#1

Re: StringGrid Inhalte zentrieren.

  Alt 3. Dez 2003, 19:10
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
  Mit Zitat antworten Zitat
whiteF

Registriert seit: 14. Okt 2008
45 Beiträge
 
#2

AW: StringGrid Inhalte zentrieren.

  Alt 26. Jun 2011, 12:57
@ Huberlix

diese variante geht aber nicht wenn man sein Grid.DrawingStyle := gdsGradient umgestellt hat.
  Mit Zitat antworten Zitat
Benutzerbild von Wolfgang Mix
Wolfgang Mix

Registriert seit: 13. Mai 2009
Ort: Lübeck
1.222 Beiträge
 
Delphi 2005 Personal
 
#3

AW: StringGrid Inhalte zentrieren.

  Alt 26. Jun 2011, 13:52
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]
Wolfgang Mix
if you can't explain it simply you don't understand it well enough - A. Einstein
Mein Baby:http://www.epubli.de/shop/buch/Grund...41818516/52824

Geändert von Wolfgang Mix (26. Jun 2011 um 14:49 Uhr)
  Mit Zitat antworten Zitat
madtom

Registriert seit: 24. Feb 2005
Ort: Hamburg
115 Beiträge
 
Delphi XE7 Professional
 
#4

AW: StringGrid Inhalte zentrieren.

  Alt 27. Jun 2011, 21:46
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
Thomas
Delphi Programming
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:22 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz