2

I am trying to show an indicative circle filled with green/red color without a stroke color in a grid's cell. The grid shows numerous other similar entities but can also have a numeric value instead of the intended circle.

For reference I am including the following code to show that circle only in the first cell. But the method used depends on the stroke kind to not be set to TBrushKind.None and also doesn't offer custom fill color, margins or padding capability:-

procedure TUI.CR_UL_UsersGridDrawColumnCell
          (       Sender : TObject;
            const Canvas : TCanvas;
            const Column : TColumn;
            const Bounds : TRectF;
            const Row    : Integer;
            const Value  : TValue;
            const State  : TGridDrawStates );
begin

  if ( Column.Index = 0 ) and ( Row = 0 ) then
  begin  
    Canvas.DrawEllipse ( Bounds, 100 );   
  end
  else
    CR_UL_UsersGrid.DefaultDrawColumnCell 
    ( Canvas, Column, Bounds, Row, Value, State );

end;

If possible, is there a way to actually add a TCircle to the cell or any other solution?

5
  • I don't understand your difficulty. Can't you just set the brush and pen properties to whatever you need? Can't you adjust the bounds to draw wherever you want? May 2, 2014 at 15:22
  • FireMonkey grid cells are recycled on a regular basis, which means you will need to keep redrawing the circle on a regular basis. I would advise you to create a custom class for your cells, with a style which contains a TCircle which can be shown/hidden/recoloured at will. You cell will need to set things as appropriate every time ApplyStyle or SetData are called and you'll need way to determine what to show (e.g. via data passed in or an event used as a callback). Much of this stuff is covered in my site at monkeystyler.com/guide May 3, 2014 at 14:13
  • @MikeSutton yes I have red your guide completely. Why a style would be better than drawing on the canvas, wouldn't they be almost same? I don't think additional controls are created from draw calls on the canvas as compared to styles. May 4, 2014 at 19:17
  • @RobKennedy There is no brush and pen property for Canvas in FMX. Though I have successfully made that ellipse/circle through FillEllipse command and some calculated modifications to the bounds object. May 4, 2014 at 19:22
  • Drawing yourself is just hard work. Let FMX do that for you. May 5, 2014 at 18:31

1 Answer 1

1

All Draw[shape] procedures only draw the outline of the [shape] in question thus if the stroke brush kind is set to None then nothing will be drawn.

Instead the Fill[shape] procedures draw color filled [shape] in question.

The code below draws a 5x5 circle in the middle of the first cell. The Bounds rect is modified to be 5x5 pixels in dimension and also centered in the cell through the calculation shown. The condition IsServerAlive determines the state of the connected server and thus the color to be filled is selected accordingly.

procedure TUI.UsersGridDrawColumnCell
          (       Sender : TObject;
            const Canvas : TCanvas;
            const Column : TColumn;
            const Bounds : TRectF;
            const Row    : Integer;
            const Value  : TValue;
            const State  : TGridDrawStates );
var
  Rect : TRectF;
begin

  if ( Column.Index = 0 ) and ( Row = 0 ) then
  begin

    Rect        := Bounds;
    Rect.Left   := Rect.Left + (( Rect.Width / 2 ) - 2.5 );
    Rect.Right  := Rect.Left + 5;
    Rect.Top    := Rect.Top + (( Rect.Height / 2 ) - 2.5 );
    Rect.Bottom := Rect.Top + 5;

    if IsServerAlive then Canvas.Fill.Color := TAlphaColorRec.Green
    else                  Canvas.Fill.Color := TAlphaColorRec.Red;

    Canvas.Fill.Kind   := TBrushKind.Solid;
    Canvas.FillEllipse ( Rect, 1 );

  end
  else
    UsersGrid.DefaultDrawColumnCell 
    ( Canvas, Column, Bounds, Row, Value, State );

end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.