Posted by webmaster Guido on June 18, 2003
In Reply to: TDrawGrid elements posted by Edouard Marmier, p12332 on June 17, 2003:
: I'm having a bad time with all these new classes and the documentation given or not given in the help system.. Both the TStringGrid and the TDrawGrid are derived from TCustomGrid, but TDrawGrid has less properties than TStringGrid. Derived from TCustomGrid: The Help says this about the properties of TStringGrid: In TStringGrid: procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var S: string; begin S := 'C' + IntToStr(ACol) + ' , R' + IntToStr(ARow); with (Sender as TDrawGrid) do begin with Canvas do begin if (ARow = 2) and (ACol = 3) then begin Font.Color := clBlack; Brush.Color := clYellow; FillRect(Rect); // fill rectangle with yellow end else if (gdSelected in State) then begin Font.Color := clWhite; Brush.Color := clNAvy; FillRect(Rect); // fill rectangle with navy end; // Next, draw the text in the rectangle TextOut(Rect.Left + 2, Rect.Top + 2, S); end; end; end; A TStringGrid on the other hand, is drawn "automatically": the contents of each "Cell" is drawn in the color TStringGrid.Font.Color, on a background with color TStringGrid.Color; for example: black on white. So, if you just want to show text strings, it's easier to use a TStringGrid. Also in a TStringGrid you can change the colors, e.g. display the cells with a yellow background, if they contain a text that starts with a "minus sign": procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var S: string; begin with (Sender as TStringGrid) do begin S := Cells[ACol, ARow]; if (Length(S) > 0) and (S[1] = '-') then begin with Canvas do begin Brush.Color := clYellow; FillRect(Rect); // fill rectangle with yellow // Next, draw the text in the rectangle TextOut(Rect.Left + 2, Rect.Top + 2, S); end; end; end; end;
[ DelphiLand: free Delphi source code, tips, tutorials ] |
|