![]() |
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. |
Re: StringGrid Inhalte zentrieren.
Ein Hallöle von
![]()
Delphi-Quellcode:
http://www.delphipraxis.net/images/common/divider.jpg
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; ![]() ![]() ![]() |
Re: StringGrid Inhalte zentrieren.
Irgenndwie will das nicht bei mir.
Delphi-Quellcode:
Fehlermeldung: er kennt ACol, ARow, Max und TextWidth sind nicht definiert. Aber das sind doch systemvariabeln, oder?
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; |
Re: StringGrid Inhalte zentrieren.
kann mir noch einer eine andere Lösung sagen.
|
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 |
AW: StringGrid Inhalte zentrieren.
@ Huberlix
diese variante geht aber nicht wenn man sein Grid.DrawingStyle := gdsGradient umgestellt hat. |
AW: StringGrid Inhalte zentrieren.
Vlt. kannst du das hier umstricken:
Delphi-Quellcode:
[Edit]
//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; ![]() http://www.delphi-forum.de/topic_Text+im+StringGrid+rechtsbuendig+ausrichten_ 6188,0.html [/Edit] |
AW: StringGrid Inhalte zentrieren.
Hier noch ein Beispiel dazu von Peter Below (TeamB)
Delphi-Quellcode:
Gruß madtom :twisted:
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; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:54 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 by Thomas Breitkreuz