Форум: "Основная";
Текущий архив: 2002.04.18;
Скачать: [xml.tar.bz2];
ВнизОтдельный Font для каждой ячейки StringGrid и т.д. Найти похожие ветки
← →
reonid (2002-04-05 14:39) [12]Печать стринггрида:
Printer.BeginDoc;
K := Printer.Canvas.Font.PixelsPerInch / Canvas.Font.PixelsPerInch;
PrintStringGrid(StrGrid,
K, // Коэффициент
200, //отступ от края листа в пихелах по Х
200, // --"-- по Y
Printer.PageHeight - 200 // нижний предел
);
Printer.EndDoc;
{--------------------------------------------------------------}
unit GrdPrnEx;
interface
uses
Windows, Classes, Graphics, Grids, Printers, SysUtils;
procedure PrintStringGrid(Grid: TStringGrid; Scale: Double; dX, dY, Yfloor: Integer);
function DrawStringGridEx(Grid: TStringGrid; Scale: Double; FromRow, dX, dY,
Yfloor: Integer; ACanvas: TCanvas): Integer;
// возвращает номер строки, которая не поместилась до Y = Yfloor
// Недоработки:
// не проверяет, вылезает ли общая длина таблицы за пределы страницы
// Слишком длинное слово обрежется
implementation
procedure PrintStringGrid(Grid: TStringGrid; Scale: Double; dX, dY, Yfloor: Integer);
var NextRow: Integer;
begin
//Printer.BeginDoc;
if not Printer.Printing then raise Exception.Create("function PrintStringGrid must be called between Printer.BeginDoc and Printer.EndDoc");
NextRow := 0;
repeat
NextRow := DrawStringGridEx(Grid, Scale, NextRow, dX, dY, Yfloor, Printer.Canvas);
if NextRow <> -1 then Printer.NewPage;
until NextRow = -1;
//Printer.EndDoc;
end;
function DrawStringGridEx(Grid: TStringGrid; Scale: Double; FromRow, dX, dY,
Yfloor: Integer; ACanvas: TCanvas): Integer;
// возвращает номер строки, которая не поместилась до Y = Yfloor
var
i, j, d, TotalPrevH, TotalPrevW, CellH, CellW: Integer;
R: TRect;
s: string;
function ScaleRect(R: TRect): TRect;
begin
Result.Left := dX + Round(R.Left*Scale);
Result.Right := dX + Round((R.Right)*Scale);
Result.Top := dY + Round(R.Top*Scale);
Result.Bottom := dY + Round((R.Bottom)*Scale);
end;
procedure CorrectCellHeight(ARow: Integer); // вычисление правильной высоты ячейки
// с учетом многострочного текста
// Текст рабивается только по словам
// слишком длинное слово обрубается
var
i, H: Integer;
R: TRect;
s: string;
begin
s := ":)"; // ?
CellH := DrawText(Grid.Canvas.Handle, PChar(s), Length(s), R,
DT_LEFT or DT_TOP or DT_WORDBREAK or DT_CALCRECT) + 4;
for i := 0 to Grid.ColCount-1 do
begin
R := Rect(0, 0, Grid.ColWidths[i], CellH);
InflateRect(R, -2, -2);
s := Grid.Cells[i, ARow];
H := DrawText(Grid.Canvas.Handle, PChar(s), Length(s), R,
DT_LEFT or DT_TOP or DT_WORDBREAK or DT_CALCRECT or DT_NOPREFIX); // Вычисление ширины и высоты текста
if CellH < H + 4 then CellH := H + 4;
// if CellW < R.Right - R.Left then Слишком длинное слово - не помещается в одну строку;
end;
end;
begin
Result := -1; // все строки уместились между dY и Yfloor
if (FromRow < 0)or(FromRow >= Grid.RowCount) then Exit;
d := Round(2*Scale);
ACanvas.Brush.Style := bsClear;
ACanvas.Font := Grid.Font;
ACanvas.Font.Height := Round(Grid.Font.Height*Scale);
TotalPrevH := 0;
for j := 0 to Grid.RowCount-1 do
begin
if (j >= Grid.FixedRows) and (j < FromRow) then Continue;
// Fixed Rows рисуются на каждой странице
TotalPrevW := 0;
CellH := Grid.RowHeights[j];
CorrectCellHeight(j);
if dY + Round( (TotalPrevH + CellH)*Scale) > YFloor then
begin
Result := j; // j-я строка не помещается в заданный диапазон
Exit;
end;
for i := 0 to Grid.ColCount-1 do
begin
CellW := Grid.ColWidths[i];
R := Rect(TotalPrevW, TotalPrevH, TotalPrevW + CellW, TotalPrevH + CellH);
R := ScaleRect(R);
if (i < Grid.FixedCols)or(j < Grid.FixedRows) then ACanvas.Pen.Width := 3
else ACanvas.Pen.Width := 1;
ACanvas.Rectangle(R.Left, R.Top, R.Right+1, R.Bottom+1);
InflateRect(R, -d, -d);
s := Grid.Cells[i, j];
DrawText(ACanvas.Handle, PChar(s), Length(s), R,
DT_LEFT or DT_TOP or DT_WORDBREAK or DT_NOPREFIX);
TotalPrevW := TotalPrevW + CellW;
end;
TotalPrevH := TotalPrevH + CellH;
end;
end;
end.
Страницы: 1 вся ветка
Форум: "Основная";
Текущий архив: 2002.04.18;
Скачать: [xml.tar.bz2];
Память: 0.45 MB
Время: 0.006 c