Einzelnen Beitrag anzeigen

Benutzerbild von Andidreas
Andidreas

Registriert seit: 27. Okt 2005
1.110 Beiträge
 
Delphi 10.1 Berlin Enterprise
 
#6

AW: Delphi XE zentrierter Text im StringGrid

  Alt 3. Jan 2012, 08:11
vorab nochmals vielen dank an RaSoWa1... dein DRAWRECT muster hat mir viel ärger erspart

ich hab mir aus dem ganzen jetzt eine procedure gebastetlt die ich im OnDrawCell aufruf anbei der source...

procedure
ist sicher noch ausbaufählig, aber für meine bedürfnise reichts vorab erst mal
Delphi-Quellcode:
//******************************************************************************
// prFormatStringGrid --> Format Cells in String Grid *
// Parameter --> strGrid = TStringGrid (Name of the Component) *
// iCol = Integer (String Grid Column No.) *
// iRow = Integer (String Grid Row No.) *
// Rect = TRect *
// State = TGridDrawState(e.g. fdFixed, fdSelected)*
// strGridDrawingStyle = TGridDrawingStyle (e.g. gdsGradient) *
// Alignment = TAlignment(e.g. taCenter, taLeftJustify)*
// FontColor = TColor (Font Color) *
// FontSize = Integer (Font Size) *
// blFontBold = Boolean (Font Bold) *
// Return --> *
//******************************************************************************

procedure prFormatStringGrid(strGrid : TStringGrid; iCol, iRow : Integer; Rect : TRect; State : TGridDrawState; strGridDrawingStyle : TGridDrawingStyle;
                             Alignment : TAlignment; FontColor : TColor; FontSize : Integer; blFontBold : Boolean);

var
s : String;

begin

  //=== Canvas Settings for gdsGardient Drawing Style ==========================
  If (strGrid.DrawingStyle = gdsGradient) And ((gdFixed in State) or (gdSelected in State)) Then
  Begin
    If Not(goFixedVertLine In strGrid.Options) Then inc(Rect.Right);
    If Not(goFixedHorzLine In strGrid.Options) Then inc(Rect.Bottom);
    If (gdHotTrack In State) Or (gdPressed In State) Then
    Begin
      If (gdPressed in State) Then
      Begin
        GradientFillCanvas(strGrid.canvas, strGrid.GradientEndColor, strGrid.GradientStartColor, Rect, gdVertical)
      End Else
      Begin
        GradientFillCanvas(strGrid.canvas, GetHighlightColor(strGrid.GradientStartColor), GetHighlightColor(strGrid.GradientEndColor), Rect, gdVertical);
      End;
    End Else
    Begin
      GradientFillCanvas(strGrid.canvas, strGrid.GradientStartColor, strGrid.GradientEndColor, Rect, gdVertical);
    End;
    strGrid.canvas.Brush.Style := bsClear;
  End Else
  Begin
    strGrid.canvas.FillRect(Rect);
  End;
  //============================================================================

  //=== Font Settings for each Cell ============================================
  s := strGrid.Cells[iCol, iRow];
  strGrid.Canvas.Font.Color := FontColor;
  strGrid.Canvas.Font.Size := FontSize;
  If Not (blFontBold) Then strGrid.Canvas.Font.Style := strGrid.Canvas.Font.Style - [fsBold];
  If (blFontBold) Then strGrid.Canvas.Font.Style := strGrid.Canvas.Font.Style + [fsBold];
  //Center
  If Alignment = taCenter Then DrawText(strGrid.Canvas.Handle, PChar(s), Length(s), Rect, DT_SINGLELINE or DT_CENTER or DT_VCENTER);
  //Left
  If Alignment = taLeftJustify Then DrawText(strGrid.Canvas.Handle, PChar(s), Length(s), Rect, DT_SINGLELINE or DT_LEFT or DT_VCENTER);
  //Right
  If Alignment = taRightJustify Then DrawText(strGrid.Canvas.Handle, PChar(s), Length(s), Rect, DT_SINGLELINE or DT_RIGHT or DT_VCENTER);
  //============================================================================

end;

aufruf in OnDrawCell des stringgrids

Delphi-Quellcode:
procedure Tmain_form.strgrid_dataDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);

begin

  //=== Column Title ===========================================================
  If ARow = 0 Then
  Begin
    prFormatStringGrid(strGrid_data, ACol, ARow, Rect, State, strgrid_data.DrawingStyle, taCenter, clBlack, 9, True);
  End;
  //============================================================================


  //=== Data Rows ==============================================================
  If ARow <> 0 Then
  Begin
    prFormatStringGrid(strGrid_data, ACol, ARow, Rect, State, strgrid_data.DrawingStyle, taLeftJustify, clBlack, 8, False);
  End;
  //============================================================================


end;
Ein Programmierer Programmiert durchschnittlich 15 Code Zeilen pro Tag
Wir sind hier doch nicht bei SAP!!!

Aber wir habens bald
  Mit Zitat antworten Zitat