11. januar 2002 - 17:01
#1
Er du en ørn til Delphi. ?
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;
{ StringGrid1 event: OnDrawCell }
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Longint;
Rect: TRect; State: TGridDrawState);
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;
{ Formens event: OnCreate }
procedure TForm1.FormCreate(Sender: TObject);
begin
DrawBitmap := TBitmap.Create;
DrawBitmap.Monochrome := True;
end;
{ Formens event: OnDestroy }
procedure TForm1.FormDestroy(Sender: TObject);
begin
DrawBitmap.Free;
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.