C öâåòîì ÿ÷åéêè DBGrid íå òàê âñå ïðîñòî, âåäü â Delphi 1.0 ðàçóêðàñèòü ÿ÷åéêó ìîæíî â îáðàáîò÷èêå ñîáûòèÿ OnDrawDataCell, òîãäà êàê â Delphi 2.0 âàì ïðèäåòñÿ ïðèáåãíóòü ê ñîáûòèþ OnDrawColumnCell. Äëÿ òîãî ÷òîáû ñîçäàòü êîä, êîòîðûé áû ðàáîòàë â ÎÁÎÈÕ âåðñèõ Delphi, âàì íåîáõîäèìî ïðèáåãíóòü ê ìåõàíèçìó óñëîâíîé êîìïèëÿöèè, ñ ïîìîùüþ êîòîðîãî âû ìîæåòå ÑÊÐÛÒÜ îáðàáîò÷èê OnDrawColumnCell â Delphi 1.0 è ÂÊËÞ×ÈÒÜ åãî â Delphi 2.0. Âîò ïðèìåð, â êîòîðîì âñå ÿ÷åéêè ñòðîêè ðèñóþòñÿ êðàñíûì, åñëè êîëîíêà 'Preferred' ñîäåðæèò 'True':
...
private
{ Private declarations }
{$IFDEF Win32}
procedure DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
{$ENDIF}
procedure DrawField(const Value: String; const Rect: TRect;
vCanvas: TCanvas; vFont: TFont; vAlignment: TAlignment);
... procedure TForm1.DrawField(const Value: String; const Rect: TRect; vCanvas: TCanvas; vFont: TFont; vAlignment: TAlignment); VAR X : Integer; BEGIN vCanvas.Font := vFont; vCanvas.Font.Color := clRed; vCanvas.Font.Style := vCanvas.Font.Style + [fsUnderline]; CASE vAlignment OF taRightJustify : BEGIN SetTextAlign(vCanvas.Handle, TA_RIGHT); X := Rect.Right-2; END; taLeftJustify : BEGIN SetTextAlign(vCanvas.Handle, TA_LEFT); X := Rect.Left+2; END; taCenter : BEGIN SetTextAlign(vCanvas.Handle, TA_CENTER); X := (Rect.Right+Rect.Left) DIV 2; END; END; vCanvas.TextRect(Rect, X, Rect.Top+2, Value); SetTextAlign(vCanvas.Handle, TA_LEFT); END; procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin WITH Sender AS TDBGrid, DataSource.DataSet DO BEGIN IF FieldByName('Preferred').AsString <> 'True' THEN Exit; DrawField(Field.DisplayText, Rect, Canvas, Canvas.Font, Field.Alignment); END; end; {$IFDEF Win32} procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin WITH Sender AS TDBGrid, DataSource.DataSet DO BEGIN IF FieldByName('Preferred').AsString <> 'True' THEN Exit; DrawField(Column.Field.DisplayText, Rect, Canvas, Column.Font, Column.Alignment); END; end; {$ENDIF} procedure TForm1.FormCreate(Sender: TObject); begin {$IFDEF Win32} DBGrid1.OnDrawDataCell := NIL; DBGrid1.OnDrawColumnCell := DBGrid1DrawColumnCell; {$ENDIF} end; |
- Neil Rubenking [000968]