Home » Tipps & Tricks » Komponenten » TStringGrid » Zellen im StringGrid mit einem Fadenkreuz markieren
Zellen im StringGrid mit einem Fadenkreuz markieren
//Textausrichten mit oder ohne Zeilenumbruch im StringGrid procedure GridAlignment(Grid: TStringGrid; Rect: TRect; ACol, ARow: Integer; Alignment: TAlignment; LineBreak: Boolean); var TextOut: String; begin Grid.Canvas.FillRect(Rect); TextOut := Grid.Cells[ACol,ARow]; if LineBreak = false then begin if Alignment = taLeftJustify then DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect, DT_LEFT); if Alignment = taCenter then DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect, DT_CENTER); if Alignment = taRightJustify then DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect, DT_RIGHT); end else begin if Alignment = taLeftJustify then DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect, DT_LEFT or DT_WORDBREAK); if Alignment = taCenter then DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect, DT_CENTER or DT_WORDBREAK); if Alignment = taRightJustify then DrawText(Grid.Canvas.Handle, PChar(TextOut), StrLen(PChar(TextOut)), Rect, DT_RIGHT or DT_WORDBREAK); end; end; //Fadenkreuz im Stringgrid erzeugen procedure DrawReticle(Grid: TStringGrid; ACol, ARow: Integer; Rect: TRect; ReticleFontColor, ReticleBackColor, GridFontColor, GridBackColor, SelFontColor, SelBackColor: TColor); begin if (ACol > Grid.FixedCols-1) And (ARow > Grid.FixedRows-1) then begin With Grid Do begin //Zellen im Fadenkreuz if (ACol = Grid.Col) or (ARow = Grid.Row) then begin Canvas.Font.Color := ReticleFontColor; Canvas.Brush.Color := ReticleBackColor; //Zellentext ausgeben GridAlignment(Grid, Rect, ACol, ARow, taCenter, False); Canvas.FrameRect(Rect); end else //Zellen außerhalb des Fadenkreuz begin Canvas.Font.Color := GridFontColor; Canvas.Brush.Color := GridBackColor; //Zellentext ausgeben GridAlignment(Grid, Rect, ACol, ARow, taLeftJustify, False); Canvas.FrameRect(Rect); end; //markierte Zelle if (ACol = Grid.Col) and (ARow = Grid.Row) then begin Canvas.Font.Color := SelFontColor; Canvas.Brush.Color := SelBackColor; //Zellentext ausgeben GridAlignment(Grid, Rect, ACol, ARow, taCenter, False); Canvas.FrameRect(Rect); end; end; end; end; //Für neue Zellenselektion, Grid neu Zeichnen procedure TForm1.GridClick(Sender: TObject); begin Grid.Invalidate; end;
Beispiel-Aufruf:
//Procedure im OnDrawCell des Grid aufrufen procedure TForm1.GridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin //Fadenkreuz erzeugen DrawReticle(Grid, ACol, ARow, Rect, clWindow, clNavy, clWindowText, clInfoBk, clWindowText, clWindow); end;