⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jvqgridpreviewform.pas

📁 East make Tray Icon in delphi
💻 PAS
📖 第 1 页 / 共 2 页
字号:
      //Text Rect
      R := Rect(X, Y, X + ScaleX(Grid.ColWidths[ICol]), Y + ScaleY(RowHeights[IRow]));
      //Draw on the Canvas
      if DoPaint then
      begin
        if GridPrinter.PrintOptions.BorderStyle = bsSingle then
        begin
          ACanvas.Brush.Style := bsClear;
          ACanvas.Rectangle(R.Left, R.Top, R.Right + ScaleX(2), R.Bottom + ScaleY(1));
        end;
        drs := Grid.Cells[ICol, IRow];
        nr := False;
        if (IRow = 0) and (ICol > 0) then
          ACanvas.Font.Style := ACanvas.Font.Style + [fsBold]
        else
          ACanvas.Font.Style := ACanvas.Font.Style - [fsBold];
        R.Left := R.Left + ScaleX(GridPrinter.PrintOptions.LeftPadding);
        if GridPrinter.WordWrap and (ICol <> 0) and (IRow <> 0) then
        begin
          if GridPrinter.NumbersalRight and (not nr) then
          try
            v := StrToFloat(drs);
            nr := True;
            drs := Format(GridPrinter.NumberFormat, [v]);
          except
            // do nothing
          end;
          if nr then
            DrawText(ACanvas.Handle, PChar(drs), -1, R, DT_WORDBREAK or DT_RIGHT)
          else
            DrawText(ACanvas.Handle, PChar(drs), -1, R, DT_WORDBREAK or DT_LEFT);
        end
        else
        begin
          if GridPrinter.NumbersalRight and (not nr) then
          try
            v := StrToFloat(drs);
            nr := True;
            drs := Format(GridPrinter.NumberFormat, [v]);
          except
            // do nothing
          end;
          if nr then
            DrawText(ACanvas.Handle, PChar(drs), -1, R, DT_SINGLELINE or DT_RIGHT)
          else
            DrawText(ACanvas.Handle, PChar(drs), -1, R, DT_SINGLELINE or DT_LEFT);
        end;
      end;
    end;
    Inc(Y, ScaleY(RowHeights[IRow]));
  end;

  procedure DrawTitle; //draw Header and Footer
  var
    S, fstr: string;
    List: TStringList;
    I: Integer;
    TmpFont: TFont; //I have no idea why you can't use gettextwidth when ACanvas = printer.Canvas, it returns wrong Value
  begin
    TmpFont := nil;
    if DoPaint then
    begin
      ACanvas.Font.Size := GridPrinter.PrintOptions.HeaderSize;
      TmpFont := Grid.Font;
      Grid.Canvas.Font := ACanvas.Font;
    end;
    //Title
    Y := ScaleY(GridPrinter.PrintOptions.MarginTop);
    S := GridPrinter.PrintOptions.PageTitle;
    HHeader := Grid.Canvas.TextHeight(S);
    if HasLogo then
      if LogoPic.Height > HHeader then
        HHeader := LogoPic.Height;
    if DoPaint then
    begin
      if HasLogo then
      begin
        ACanvas.Draw(ScaleX(GridPrinter.PrintOptions.MarginLeft), Y, LogoPics);
      end;
      ACanvas.TextOut((PageWidth div 2) - (ScaleX(Grid.Canvas.TextWidth(S) div 2)), Y, S);
    end;
    Y := Y + ScaleY(HHeader);
    //Page nr
    S := Format(RsPaged, [PageRow]);
    if (ToCol < Grid.ColCount - 1) or (PageCol > 1) then
      S := S + '-' + IntToStr(PageCol);
    fstr := GridPrinter.PrintOptions.PageFooter;
    HFooter := Grid.Canvas.TextHeight(fstr);
    if fstr <> '' then
      if DoPaint then
      begin
        ACanvas.Font.Size := GridPrinter.PrintOptions.FooterSize;
        Grid.Canvas.Font := ACanvas.Font;
        HFooter := Grid.Canvas.TextHeight(fstr);
        List := TStringList.Create;
        List.Text := StringReplace(fstr, '|', cr, [rfreplaceall]);
        while List.Count < 3 do
          List.Append('');
        for I := 0 to 2 do
        begin
          List[I] := StringReplace(List[I], 'date', FormatDateTime(GridPrinter.PrintOptions.DateFormat, Now), []);
          List[I] := StringReplace(List[I], 'time', FormatDateTime(GridPrinter.PrintOptions.TimeFormat, Now), []);
          List[I] := StringReplace(List[I], 'page', S, []);
        end;
        //paint Left footer
        if List[0] <> '' then
          ACanvas.TextOut(ScaleX(Integer(GridPrinter.PrintOptions.MarginLeft) + Grid.Canvas.TextWidth(List[0])),
            PageHeight - ScaleY(Integer(GridPrinter.PrintOptions.MarginBottom) + Grid.Canvas.TextHeight(List[0])),
            List[0]);
        //paint center footer
        if List[1] <> '' then
          ACanvas.TextOut((PageWidth div 2) - (ScaleX(Grid.Canvas.TextWidth(List[1])) div 2), PageHeight -
            ScaleY(Integer(GridPrinter.PrintOptions.MarginBottom) + Grid.Canvas.TextHeight(List[1])), List[1]);
        //paint Right footer
        if List[2] <> '' then
          ACanvas.TextOut(PageWidth - ScaleX(Integer(GridPrinter.PrintOptions.MarginRight) +
            Grid.Canvas.TextWidth(List[2]) + 10), PageHeight - ScaleY(Integer(GridPrinter.PrintOptions.MarginBottom) +
            Grid.Canvas.TextHeight(List[2])), List[2]);
        List.Free;
      end;

    if DoPaint then
    begin
      ACanvas.Font.Size := Grid.Font.Size;
      Grid.Canvas.Font := TmpFont;
    end;
    Y := Y + ScaleY(GridPrinter.PrintOptions.PageTitleMargin);
    DrawCells(0);
  end;

begin
  // Do not set the Printer's orientation after BeginDoc because this might lead
  // to a blank page.
  if Mode = pmPreview then
    Printer.Orientation := GridPrinter.PrintOptions.Orientation;

  //page size
  PageWidth := Printer.PageWidth;
  PageHeight := Printer.PageHeight;
  if Mode = pmPreview then
  begin
    PageWidth := PageWidth div ((GetDeviceCaps(Printer.Handle, LOGPIXELSX) div Screen.PixelsPerInch));
    PageHeight := PageHeight div ((GetDeviceCaps(Printer.Handle, LOGPIXELSY) div Screen.PixelsPerInch));
    FPrintImage.Width := PageWidth;
    FPrintImage.Height := PageHeight;
    ACanvas.Brush.Color := clWhite;
    ACanvas.FillRect(Rect(0, 0, PageWidth, PageHeight));
  end;
  HasLogo := False;
  if GridPrinter.PrintOptions.Logo <> '' then
    if FileExists(GridPrinter.PrintOptions.Logo) then
    begin
      LogoPic := TBitmap.Create;
      LogoPic.LoadFromFile(GridPrinter.PrintOptions.Logo);
      HasLogo := True;
      LogoPics := TBitmap.Create;
      LogoPics.Width := ScaleX(LogoPic.Width);
      LogoPics.Height := ScaleY(LogoPic.Height);
      LogoPic.PixelFormat := pf24bit;
      LogoPics.PixelFormat := pf24bit;
      TJvPaintFX.SmoothResize(LogoPic, LogoPics);
    end;

  if Mode <> pmPageCount then
  begin
    ACanvas.Font := Grid.Font;
    ACanvas.Font.Color := clBlack;
  end;
  PageCol := 0;
  FromCol := -1;
  ToCol := -0;
  //scan cols
  repeat
    //Scan missing cols
    if FromCol = ToCol then
      Inc(FromCol)
    else
      FromCol := ToCol + 1;
    Inc(ToCol);
    //Get Cols with Width that fits page
    X := GridPrinter.PrintOptions.MarginLeft;
    for I := FromCol to Grid.ColCount - 1 do
    begin
      Inc(X, ScaleX(Grid.ColWidths[I] + 1));
      if X <= (PageWidth - Integer(GridPrinter.PrintOptions.MarginRight)) then
        ToCol := I;
    end;
    PageRow := 1;
    Inc(PageCol);
    //Mode = PageCount
    Inc(FPageCount);
    //preview mode
    DoPaint := (((Mode = pmPreview) and (FPageCount = GridPrinter.PrintOptions.PreviewPage)) or (Mode = pmPrint));
    //Header & Footer
    DrawTitle;
    //Contents
    IRow := FromRow;
    repeat
      //      Inc(Y, ScaleY(RowHeights[IRow]));
      if (Y + ScaleY(RowHeights[IRow])) <= (PageHeight - ScaleY(Integer(GridPrinter.PrintOptions.MarginBottom) + 20 +
        HFooter)) then
      begin //draw contents to Canvas
        DrawCells(IRow);
        Inc(IRow);
      end
      else //New page
      begin
        if DoPaint and (Mode = pmPreview) then
          Exit;
        if Mode = pmPrint then
          Printer.NewPage;
        Inc(FPageCount); //pagecount
        DoPaint := (((Mode = pmPreview) and (FPageCount = GridPrinter.PrintOptions.PreviewPage)) or (Mode = pmPrint));
        Inc(PageRow);
        DrawTitle;
      end;
      if (IRow = ToRow + 1) and (ToCol < Grid.ColCount - 1) and (Y <= PageHeight - ScaleY(20)) then
      begin
        if DoPaint and (Mode = pmPreview) then
          Exit;
        if Mode = pmPrint then
          Printer.NewPage;
        DrawTitle;
      end;
    until
      IRow = ToRow + 1;
  until
    ToCol = Grid.ColCount - 1;
  if HasLogo then
  begin
    LogoPic.Free;
    LogoPics.Free;
  end;
end;

procedure TJvGridPreviewForm.SetGrid(const Value: TStringGrid);
begin
  FGrid := Value;
end;

procedure TJvGridPreviewForm.UpdateRowHeights;
var
  C, MaxH, H, ARow: Integer;
  R: TRect;
  S: string;
begin
  SetLength(RowHeights, Grid.RowCount);
  RowHeights[0] := Grid.RowHeights[0];
  MaxH := Grid.DefaultRowHeight;
  for ARow := 1 to Grid.RowCount - 1 do
  begin
    for C := 0 to Grid.ColCount - 1 do
    begin
      S := Grid.Cells[C, ARow];
      R := Grid.CellRect(C, ARow);
      DrawText(Grid.Canvas.Handle, PChar(S), -1, R, DT_CALCRECT or DT_WORDBREAK);
      H := R.Bottom - R.Top + 1;
      if H > MaxH then
        MaxH := H;
    end;
    if GridPrinter.WordWrap then
      RowHeights[ARow] := MaxH
    else
      RowHeights[ARow] := Grid.RowHeights[ARow];
  end;
end;

function TJvGridPreviewForm.PageCount: Integer;
begin
  Result := 0;
  if not Assigned(FGrid) then
    Exit;
  UpdateRowHeights;
  FPageCount := 0;
  DrawToCanvas(nil, pmPageCount, 1, Grid.RowCount - 1);
  Result := FPageCount;
end;

procedure TJvGridPreviewForm.UpdatePreview(ACanvas: TCanvas);
begin
  FPageCount := 0;
  DrawToCanvas(ACanvas, pmPreview, 1, Grid.RowCount - 1);
end;

procedure TJvGridPreviewForm.Print;
begin
  if not Assigned(FGrid) then
    Exit;
  UpdateRowHeights;
  if Printer.Printers.Count = 0 then
  begin
    MessageDlg(RsNoPrinterIsInstalled, mtError, [mbOK], 0);
    Exit;
  end;
  Printer.Title := GridPrinter.PrintOptions.JobTitle;
  Printer.Copies := GridPrinter.PrintOptions.Copies;
  Printer.Orientation := GridPrinter.PrintOptions.Orientation;
  Printer.BeginDoc;
  DrawToCanvas(Printer.Canvas, pmPrint, 1, Grid.ColCount - 1);
  Printer.EndDoc;
end;

procedure TJvGridPreviewForm.MarginClick(Sender: TObject;
  Button: TUDBtnType);
begin
  case Margins.ItemIndex of
    -1:
      Exit;
    0:
      GridPrinter.PrintOptions.MarginTop := Margin.Position;
    1:
      GridPrinter.PrintOptions.PageTitleMargin := Margin.Position;
    2:
      GridPrinter.PrintOptions.MarginLeft := Margin.Position;
    3:
      GridPrinter.PrintOptions.MarginRight := Margin.Position;
    4:
      GridPrinter.PrintOptions.MarginBottom := Margin.Position;
    5:
      GridPrinter.PrintOptions.LeftPadding := Margin.Position;
    6:
      GridPrinter.PrintOptions.HeaderSize := Margin.Position;
    7:
      GridPrinter.PrintOptions.FooterSize := Margin.Position;
  end;
  if cklive.Checked then
    btnshow.Click;
end;

procedure TJvGridPreviewForm.PreviewPageClick(Sender: TObject;
  Button: TUDBtnType);
begin  
  if PreviewPage.Position < PreviewPage.Min then
    PreviewPage.Position := PreviewPage.Min;
  if PreviewPage.Position > PreviewPage.Max then
    PreviewPage.Position := PreviewPage.Max; 
  GridPrinter.PrintOptions.PreviewPage := PreviewPage.Position;
  if cklive.Checked then
    btnshow.Click;
end;

{$IFDEF UNITVERSIONING}
const
  UnitVersioning: TUnitVersionInfo = (
    RCSfile: '$RCSfile: JvQGridPreviewForm.pas,v $';
    Revision: '$Revision: 1.13 $';
    Date: '$Date: 2004/09/10 22:05:57 $';
    LogPath: 'JVCL\run'
  );

initialization
  RegisterUnitVersion(HInstance, UnitVersioning);

finalization
  UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -