Link to home
Start Free TrialLog in
Avatar of Roger Alcindor
Roger Alcindor

asked on

FMX StringGrid1->Canvas->FillRect Problem

I am writing an application that uses a DrawColumnCell event handler to populate a StringGrid (FMX).

I wish to highlight various cells with a colour. The Canvas->FillRect function would seem to be the function to use and I have succeeded in doing this but not in the way that I would prefer.
The FillRect function has two forms, one in which the fill is done using the current Canvas->Fill->Color and a second form that accepts an additional parameter TBrush whose color value is used to fill the Rect. I can't get the second form of the FillRect funtion to work.

What I do is to temporarily store the Canvas->Fill->Color , change it to a colour that I want, use the first form of the FillRect function and then restore Canvas->Fill->Color to its' original value.
Can you see what I am doing wrong with my use of the second form of the FillRect function ?
My code is as follows:

void __fastcall TForm2::StringGrid1DrawColumnCell(TObject *Sender, TCanvas * const Canvas,
              TColumn * const Column, const TRectF &Bounds, const int Row,
              const TValue &Value, const TGridDrawStates State)
{
      //TRect rect = Rect;
      TValue v = Value;
      TRectF bounds = Bounds;
      TFillTextFlags ff;
      TTextAlign ta;
      System::Uitypes::TAlphaColor tmp;

      ff.Clear();

      UnicodeString X = ((TValue)Value).ToString();
      if(Column->Index==1)
      {
      // draw text in this column in red
            StringGrid1->Canvas->Fill->Color = claRed; // red text
            if(Row==5)
            {   // set his row to a green (lime) backgroud
                  // this is the way I do it
                  tmp = StringGrid1->Canvas->Fill->Color;
                  StringGrid1->Canvas->Fill->Color = claLime;
                  StringGrid1->Canvas->FillRect(bounds,0,0,AllCorners, 1);
                  StringGrid1->Canvas->Fill->Color = tmp;
                  // but this should be able to be done in one line as follows
                  // the next two lines of code compiles but don't do anything
/***
                  TBrush *b = new TBrush(TBrushKind::None,claLime);
                  StringGrid1->Canvas->FillRect(bounds,0,0,AllCorners,1,b);
                  delete b;
***/
            }
      }
      else
      {
            StringGrid1->Canvas->Fill->Color = claBlack;// black text;
      }
      StringGrid1->Canvas->FillText(bounds,X,false,1,ff,ta);

}
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Roger Alcindor
Roger Alcindor

ASKER

Thanks Sara,
 If I were to use CreateSolidBrush, would any memory allocated be freed up when the TBrush instance was deleted ?

Roger
the TBrush has a destructor which frees all memory allocated. i am pretty sure that will also include the resource handle HBRUSH returned by CreateSolidBrush since for native windows graphic (gdi) you would need to create a gdi object, to select it, and finally to delete the object. all those functions were encapsulated into the TBrush and or the TCanvas and you don't have to bother with.

note, solid brushes are resources which were cached by windows. so, if you didn't set a rare color the resource would not really be freed.

Sara