Jeg har lavet en form der kan gøre det. Implementationsdelen ser således ud:
implementation
{$R *.DFM}
var DrawBitmap: TBitmap;
function Max(X, Y: Integer): Integer; begin Result := Y; if X > Y then Result := X; end;
procedure WriteText(ACanvas: TCanvas; const ARect: TRect; DX, DY: Integer; const Text: string; Format: Word); var S: array[0..255] of Char; B, R: TRect; begin if Format = DT_LEFT then ExtTextOut(ACanvas.Handle, ARect.Left + DX, ARect.Top + DY, ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil) else if Format = DT_RIGHT then with ACanvas do ExtTextOut(Handle, ARect.Right - TextWidth(Text) - 3, ARect.Top + DY, ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil) else begin with DrawBitmap, ARect do begin Width := Max(Width, Right - Left); Height := Max(Height, Bottom - Top); R := Rect(DX, DY, Right - Left - 1, Bottom - Top - 1); B := Rect(0, 0, Right - Left, Bottom - Top); end; with DrawBitmap.Canvas do begin Font := ACanvas.Font; Font.Color := clBlack; FillRect(B); DrawText(Handle, StrPCopy(S, Text), Length(Text), R, Format); end; with DrawBitmap, ARect do ACanvas.CopyRect(ARect, Canvas, B); end; end;
procedure Display(const S: string; Alignment: TAlignment); const Formats: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX); begin WriteText(StringGrid1.Canvas, Rect, 2, 2, S, Formats[Alignment]); end;
begin { Her kan der laves forskellig justering alt efter hvilken kolonne der skal skrives ud. } {Display(StringGrid1.Cells[Col, Row], taLeftJustify);} { Venstre } {Display(StringGrid1.Cells[Col, Row], taCenter);} { Centreret } Display(StringGrid1.Cells[Col, Row], taRightJustify); { Højre } end;
Den korrekte måde er selvfølgelig at lave en ny komponent der bygger på TStringGrid som overskriver proceduren DrawCell og indeholder en property hvor man kan angive kolonnernes Alignment. Men det er der ikke plads til her.
Synes godt om
Slettet bruger
14. januar 2002 - 08:03#2
Tak for det store arbejde (for kun 15 p). Jeg har ikke chekket om dte virker, men jeg kan se pricippet i det.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.