TStringGrid = class(Grids.TStringGrid) private FGraphic: TGraphic;
FStretched: Boolean; function BackgroundVisible(var ClipRect: TRect): Boolean; procedure PaintBackground; protected procedure Paint; override; procedure Resize; override; procedure TopLeftChanged; override; function CreateEditor: TInplaceEdit;override;
public property BackgroundGraphic: TGraphic read FGraphic write FGraphic; property BackgroundStretched: Boolean read FStretched write FStretched; end;
function TStringGrid.CreateEditor: TInplaceEdit; begin Result := TInplaceEdit.Create(Self); Result.Brush.Color := clBlue; TEdit(Result).Font.Color:=clred; //result.Parent:=Self;
end;
procedure TStringGrid.PaintBackground; var R: TRect; begin if (FGraphic <> nil) and BackgroundVisible(R) then begin with R do ExcludeClipRect(Canvas.Handle, Left, Top, Right, Bottom); if FStretched then Canvas.StretchDraw(ClientRect, FGraphic) else Canvas.Draw(0, 0, FGraphic); end; end;
procedure TStringGrid.Resize; begin inherited Resize; PaintBackground; end;
procedure TStringGrid.TopLeftChanged; begin inherited TopLeftChanged; PaintBackground; end;
function TStringGrid.BackgroundVisible(var ClipRect: TRect): Boolean; var Info: TGridDrawInfo; R: TRect; begin CalcDrawInfo(Info); SetRect(ClipRect, 0, 0, Info.Horz.GridBoundary, Info.Vert.GridBoundary); R := ClientRect; Result := (ClipRect.Right < R.Right) or (ClipRect.Bottom < R.Bottom); end;
procedure TStringGrid.Paint; begin inherited Paint; PaintBackground; end;
procedure TForm2.FormCreate(Sender: TObject); var r:TRect; begin
StringGrid1.options:=StringGrid1.options-[goRowSelect]; // StringGrid1.Canvas.Brush.Color:=clRed;
// StringGrid1.DefaultDrawing := false;
FBg := TBitmap.Create; //FBg.LoadFromFile('C:\Users\Andreas Rejbrand\Pictures\Sample.bmp'); FBg.Width:=600; FBg.Height:=100; FBg.Canvas.Brush.Color:=clBlue; FBg.Canvas.Brush.Style:=bsSolid; r:=Rect(0,0,600,100); FBg.Canvas.FillRect(r);
StringGrid1.BackgroundGraphic :=FBg;// Image.Picture.Graphic; StringGrid1.BackgroundStretched := True;
end;
procedure TForm2.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var r: TRect; begin if not (Sender is TStringGrid) then Exit; BitBlt(TStringGrid(Sender).Canvas.Handle, Rect.Left, Rect.Top, Rect.Right - Rect.Left, Rect.Bottom - Rect.Top, FBg.Canvas.Handle, Rect.Left, Rect.Top, SRCCOPY); if gdSelected in State then InvertRect(TStringGrid(Sender).Canvas.Handle, Rect); r := Rect; TStringGrid(Sender).Canvas.Brush.Style := bsClear; DrawText(TStringGrid(Sender).Canvas.Handle, TStringGrid(Sender).Cells[ACol, ARow], length(TStringGrid(Sender).Cells[ACol, ARow]), r, DT_SINGLELINE or DT_VCENTER or DT_END_ELLIPSIS); end;
|