Я добавил дополнительное свойство LinesPerRow. Установка значений данного свойства соответственно изменяет высоту строки, в зависимости от текущего шрифта. Текст в ячейках будет переноситься, если значение LinesPerRow больше чем единица. Все это произведение искусств оказалось чрезвычайно полезным и удивительно простым, так что я публикую его здесь в надежде, что оно пригодится кому-нибудь еще. Код простой, но для его понимания необходимо изучение исходного кода VCL.
Я протестировал данный код и он отлично работал. Небольшая доводка все-же нужна (обработка blob-полей, обработка ошибок и пр.), но это не сложно.
unit Dbmygrid; interface uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,Forms, Dialogs, DB, DBTables, StdCtrls, ExtCtrls, Grids, DBGrids; type TMultiLineDBGrid = class(TDBGrid)privateFLinesPerRow: Integer;procedure DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState);procedure LayoutChanged; override;procedure SetLinesPerRow(ALinesPerRow: Integer);publicproperty LinesPerRow: Integer read FLinesPerRow write SetLinesPerRow default 1;constructor Create(AOwner: TComponent); override;end; implementation constructor TMultiLineDBGrid.Create(AOwner: TComponent); begin inherited Create(AOwner);FLinesPerRow := 1;OnDrawDataCell := DrawDataCell;end; procedure TMultiLineDBGrid.LayOutChanged; begin inherited LayOutChanged;DefaultRowHeight := DefaultRowHeight * LinesPerRow;end; procedure TMultiLineDBGrid.DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); var Format: Word;C: array[0..255] of Char;begin if LinesPerRow = 1 thenFormat := DT_SINGLELINE or DT_LEFTelseFormat := DT_LEFT or DT_WORDBREAK; Canvas.FillRect(Rect); StrPCopy(C, Field.AsString);WinProcs.DrawText(Canvas.Handle, C, StrLen(C), Rect, Format);end; procedure TMultiLineDBGrid.SetLinesPerRow(ALinesPerRow: Integer); begin if ALinesPerRow <> FLinesPerRow thenbeginFLinesPerRow := ALinesPerRow;LayoutChanged;end;end; end. |
Chris Hall [000668]