Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Font for StringGrid OnDrawCell

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
I have a five column stringgrid which I want to use a different font colour (for the entire row) based on the value of column 2 (Col 1 in code). So far it works, but only changes the font colour for column 2. I have tried numerous things, but the best I can do is change the colour for columns 1 (Col 0) 0and 2 (Col 1).

Any ideas?

Here is my code:

Code:
procedure TfrmMain.sgInvoiceDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S,strItemType: String;
  drawrect :trect;
begin
  //paint column backcolour
  Case ACol of
    1:
      begin
      //check whether item has been configured to a service/music/hardware item
      //and paint font colour to that of the colours used in item configuration
      //listboxes
      if sgInvoice.Cells[ACol,ARow] = '' then exit;
      strItemType := RetrieveTYPE(sgInvoice.Cells[ACol,ARow]);
      if strItemType = 's' then
        begin
          sgInvoice.Canvas.Font.Color := clMaroon;
          sgInvoice.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, sgInvoice.Cells[ACol, ARow]);
        end
      else if strItemType = 'm' then
        begin
          sgInvoice.Canvas.Font.Color := clTeal;
          sgInvoice.Canvas.TextOut(Rect.Left + 2 , Rect.Top + 2, sgInvoice.Cells[ACol, ARow]);
        end
      else if strItemType = 'h' then
        begin
          sgInvoice.Canvas.Font.Color := clOlive;
          sgInvoice.Canvas.TextOut(Rect.Left + 2 , Rect.Top + 2, sgInvoice.Cells[ACol, ARow]);
        end;
      end;
    3:
      begin
      sgInvoice.Canvas.Brush.Color := clCream; //clBtnFace;
      sgInvoice.Canvas.FillRect(Rect);
      sginvoice.Canvas.Font.Color := clBlack;
      sgInvoice.Canvas.TextOut(Rect.Left + 2 , Rect.Top + 2, sgInvoice.Cells[ACol, ARow]);
      end;
    else
      begin
      sgInvoice.Canvas.Brush.Color := clWindow;
      sgInvoice.Canvas.FillRect(Rect);
      sgInvoice.Canvas.Pen.Color := clBlack;
      sgInvoice.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, sgInvoice.Cells[ACol, ARow]);
      end;
  end; {case}
  ......
  ......

end;




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
What I have foun is that the ARow and Acol variables are being updated dynamicly (in the background)
Create 2 locals and immediatl transfer the ARow and ACol values to these, and use these in the procedure.

This is a working example chages the font and background colour of a row.
there is also a commented out line showing how a cell 'object' can be used to hold color information.

Code:
procedure TEmergiForm.emergidataDrawCell(Sender: TObject; Col,
  Row: Integer; Rect: TRect; State: TGridDrawState);
var count, r: integer;
begin
   r := row;  // Copy variable 'row' as it only  seems to be valid for a short time
   Count :=  (Row - 1) * emergidata.colcount + Col;
   if (count > 0) and (count <= list.count) then
   with (sender as tstringgrid) do
   begin
      if state = [] then  // dont overwrite fixed areas
        begin
           if R  = Foundrow then
               canvas.brush.color := FindColour
           else
              begin
                 if pos('*',list.strings[count]) <> 0 then
                 //if pos('fault',list.strings[count]) <> 0 then

                     begin
                   //     canvas.brush.color := (objects[0, R] as TCellcol).colour;

                        canvas.brush.color := ErrorColour;//clRed;
                        canvas.font.color := clwhite;
                     end
                 else
                     begin
                        canvas.brush.color := OKColour;//clwindow;
                     end;
              end;
          canvas.fillrect(rect);
          canvas.textout(rect.left + 2, rect.top + 2,list.strings[count]);
       end;
  end;

Steve
Those who know me have no need of my name. SD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top