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

📄 superpr.pas

📁 wptools5 pro 完整源代码 Msword界面的文本编辑器源代码
💻 PAS
字号:
unit SuperPr;

interface

{$I WPINC.INC}
{$DEFINE WPPDFEX}

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, wpDefActions, ExtCtrls, StdCtrls, WPTbar, WPPanel, Buttons,
  WPCTRRich, WPRTEDefs, WPCTRMemo, WPRuler, WPUtil, ComCtrls, WPAction, WPCTRPrint,
  ExtDlgs, ToolWin, ImgList, Printers
{$IFDEF WPPDFEX}
  , WPPDFr1, WPPDFr2 // TWPPDFPrinter
{$ENDIF}
  ;

type
  TWPSuperPrintTest = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    WPRuler1: TWPRuler;
    WPVertRuler1: TWPVertRuler;
    WPRichText1: TWPRichText;
    Splitter1: TSplitter;
    WPDefaultActions1: TWPDefaultActions;
    WPToolPanel1: TWPToolPanel;
    WPToolButton1: TWPToolButton;
    WPToolButton2: TWPToolButton;
    WPToolButton3: TWPToolButton;
    WPToolButton4: TWPToolButton;
    WPToolButton5: TWPToolButton;
    WPToolButton6: TWPToolButton;
    WPToolButton7: TWPToolButton;
    WPToolButton8: TWPToolButton;
    WPToolButton9: TWPToolButton;
    WPToolButton10: TWPToolButton;
    WPToolButton11: TWPToolButton;
    WPToolButton12: TWPToolButton;
    WPToolButton13: TWPToolButton;
    WPToolButton14: TWPToolButton;
    WPToolButton15: TWPToolButton;
    WPComboBox1: TWPComboBox;
    WPComboBox2: TWPComboBox;
    WPComboBox3: TWPComboBox;
    WPComboBox4: TWPComboBox;
    WPComboBox5: TWPComboBox;
    SizeCombo: TWPComboBox;
    PageControl1: TPageControl;
    ColRowTab: TTabSheet;
    labColCount: TLabel;
    labRowCount: TLabel;
    valColCount: TWPValueEdit;
    valRowCount: TWPValueEdit;
    BackGrTab: TTabSheet;
    Bevel1: TBevel;
    imgBackGrImage: TImage;
    btnLoadImage: TButton;
    grmMarginsBackground: TGroupBox;
    valBTopMargin: TWPValueEdit;
    valBLeftMargin: TWPValueEdit;
    valBRightMargin: TWPValueEdit;
    valBBottomMargin: TWPValueEdit;
    Panel3: TPanel;
    PaintBox1: TPaintBox;
    labCopyPages: TLabel;
    grpMarginsPages: TGroupBox;
    valPTopMargin: TWPValueEdit;
    valPLeftMargin: TWPValueEdit;
    valPRightMargin: TWPValueEdit;
    valPBottomMargin: TWPValueEdit;
    valCopyCount: TWPValueEdit;
    Label1: TLabel;
    Label2: TLabel;
    valPHorzMargin: TWPValueEdit;
    valPVertMargin: TWPValueEdit;
    chkTileImage: TCheckBox;
    grpPlacement: TGroupBox;
    AlignImages: TImageList;
    ToolBar1: TToolBar;
    PL0: TToolButton;
    PL1: TToolButton;
    PL2: TToolButton;
    PL3: TToolButton;
    PL4: TToolButton;
    PL5: TToolButton;
    PL6: TToolButton;
    chkStretchImage: TCheckBox;
    PL7: TToolButton;
    PL8: TToolButton;
    btnLockAR: TSpeedButton;
    btnImageOnEachPage: TSpeedButton;
    TwoUpBooklet: TCheckBox;
    AssignPrintToTextSize: TButton;
    procedure FormCreate(Sender: TObject);
    procedure valColCountChange(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
    procedure valPBottomMarginUnitChange(Sender: TObject);
    procedure btnLoadImageClick(Sender: TObject);
    procedure chkTileImageClick(Sender: TObject);
    procedure PL0Click(Sender: TObject);
    procedure StartPrint(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure WPDefaultActions1Init(ActionModule: TWPDefAct);
    procedure WPDefaultActions1Info2(Sender: TObject);
    procedure TwoUpBookletClick(Sender: TObject);
    procedure AssignPrintToTextSizeClick(Sender: TObject);
    procedure WPDefaultActions1Export(Sender: TObject;
      ActionModule: TWPDefAct; WPRichText: TWPCustomRichText);
  private
    FLockProp: Boolean;
  public
    WPSuperPrint1: TWPSuperPrint;
    PageInc, PageDec: Integer;
    procedure UpdatePreview;
    procedure DoUpdate(Sender: TObject);
    procedure DoCalcPage(Sender: TObject; RectangleNr: Integer;
      OnPageNr: Integer; var TextPageNr: Integer; var Rotated: Boolean);
  end;

var
  WPSuperPrintTest: TWPSuperPrintTest;

implementation

{$R *.dfm}

procedure TWPSuperPrintTest.FormCreate(Sender: TObject);
begin
//GlobalValueUnit := euInch; // D7/WP4  for the Americans out there
  WPSuperPrint1 := TWPSuperPrint.Create(Self);
  WPSuperPrint1.EditBox := WPRichText1;
  WPSuperPrint1.Preview := PaintBox1;
  WPSuperPrint1.OnCalcPageNumber := DoCalcPage;
  WPSuperPrint1.OnUpdate := DoUpdate;
  WPSuperPrint1.Landscape := wpprLandscapeAuto;
  WPSuperPrint1.Mode := wpprMultiPage;

  // Get the page size from the printer
  WPRichText1.ReadPrinterProperties; 
end;

procedure TWPSuperPrintTest.FormShow(Sender: TObject);
begin
  UpdatePreview;
end;

procedure TWPSuperPrintTest.WPDefaultActions1Init(ActionModule: TWPDefAct);
begin
  ActionModule.PrintPreview1.Visible := FALSE;
  ActionModule.Print1.Action := nil;
  ActionModule.Print1.OnClick := StartPrint;
  ActionModule.Print2.Visible := FALSE;
end;

procedure TWPSuperPrintTest.btnLoadImageClick(Sender: TObject);
var dia: TOpenPictureDialog;
begin
  dia := TOpenPictureDialog.Create(self);
  try
    dia.InitialDir := ExtractFilePath(WPSuperPrint1.BackgroundImageName);
    dia.FileName := WPSuperPrint1.BackgroundImageName;
    if dia.Execute then
    begin
      imgBackGrImage.Picture.LoadFromFile(dia.FileName);

      WPSuperPrint1.BackgroundImage := imgBackGrImage.Picture;
      WPSuperPrint1.BackgroundImageName := dia.FileName;
    end;
  finally
    dia.Free;
  end;
end;

procedure TWPSuperPrintTest.valColCountChange(Sender: TObject);
begin
  UpdatePreview;
end;

procedure TWPSuperPrintTest.UpdatePreview;
var mode: TWPSuperPrintImageModes;
begin
  if FLockProp then exit;
  WPSuperPrint1.Columns := valColCount.IntValue;
  WPSuperPrint1.Rows := valRowCount.IntValue;
  WPSuperPrint1.Copies := valCopyCount.IntValue;
  WPSuperPrint1.MarginTop := valPTopMargin.Value;
  WPSuperPrint1.MarginLeft := valPLeftMargin.Value;
  WPSuperPrint1.MarginRight := valPRightMargin.Value;
  WPSuperPrint1.MarginBottom := valPBottomMargin.Value;
  WPSuperPrint1.InbetweenHorz := valPHorzMargin.Value;
  WPSuperPrint1.InbetweenVert := valPVertMargin.Value;

  WPSuperPrint1.BGMarginLeft := valBLeftMargin.Value;
  WPSuperPrint1.BGMarginTop := valBTopMargin.Value;
  WPSuperPrint1.BGMarginRight := valBRightMargin.Value;
  WPSuperPrint1.BGMarginBottom := valBBottomMargin.Value;

  if chkTileImage.Checked then
    WPSuperPrint1.BackgroundImageModes := [wpImgTile]
  else
  begin
    if chkStretchImage.Checked then
    begin
      if btnLockAR.Down then
        mode := [wpImgStretchAspectRatio]
      else mode := [wpImgStretch];
    end
    else mode := [];

    if not (wpImgStretch in mode) then
    begin
      if pl1.Down then mode := mode + [wpImgHCenter]
      else if pl2.Down then mode := mode + [wpImgRight]
      else if pl3.Down then mode := mode + [wpImgRight, wpImgVCenter]
      else if pl4.Down then mode := mode + [wpImgRight, wpImgBottom]
      else if pl5.Down then mode := mode + [wpImgHCenter, wpImgBottom]
      else if pl6.Down then mode := mode + [wpImgBottom]
      else if pl7.Down then mode := mode + [wpImgVCenter]
      else if pl8.Down then mode := mode + [wpImgVCenter, wpImgHCenter];
    end;

    if btnImageOnEachPage.Down then
      mode := mode + [wpImagMultiPage];

    WPSuperPrint1.BackgroundImageModes := mode;
  end;
end;

procedure TWPSuperPrintTest.DoUpdate(Sender: TObject);
var mode: TWPSuperPrintImageModes;
begin
  FLockProp := TRUE;
  try
    if (WPSuperPrint1.Rows <> 1) or (WPSuperPrint1.Columns <> 2) then
    begin
      TwoUpBooklet.Checked := FALSE;
      WPSuperPrint1.SetTwoUpBooklet(FALSE, 0, 0);
    end;
    valColCount.IntValue := WPSuperPrint1.Columns;
    valRowCount.IntValue := WPSuperPrint1.Rows;
    valCopyCount.IntValue := WPSuperPrint1.Copies;
    valPTopMargin.Value := WPSuperPrint1.MarginTop;
    valPLeftMargin.Value := WPSuperPrint1.MarginLeft;
    valPRightMargin.Value := WPSuperPrint1.MarginRight;
    valPBottomMargin.Value := WPSuperPrint1.MarginBottom;
    valPHorzMargin.Value := WPSuperPrint1.InbetweenHorz;
    valPVertMargin.Value := WPSuperPrint1.InbetweenVert;
    valBLeftMargin.Value := WPSuperPrint1.BGMarginLeft;
    valBTopMargin.Value := WPSuperPrint1.BGMarginTop;
    valBRightMargin.Value := WPSuperPrint1.BGMarginRight;
    valBBottomMargin.Value := WPSuperPrint1.BGMarginBottom;
  // Image props
    mode := WPSuperPrint1.BackgroundImageModes;
    chkTileImage.Checked := wpImgTile in mode;
    btnLockAR.Down := wpImgStretchAspectRatio in mode;
    chkStretchImage.Checked := btnLockAR.Down or (wpImgStretch in mode);
    btnImageOnEachPage.Down := wpImagMultiPage in mode;
    mode := mode * [wpImgHCenter, wpImgVCenter, wpImgRight, wpImgBottom];
    pl1.Down := mode = [wpImgHCenter];
    pl2.Down := mode = [wpImgRight];
    pl3.Down := mode = [wpImgRight, wpImgVCenter];
    pl4.Down := mode = [wpImgRight, wpImgBottom];
    pl5.Down := mode = [wpImgHCenter, wpImgBottom];
    pl6.Down := mode = [wpImgBottom];
    pl7.Down := mode = [wpImgVCenter];
    pl8.Down := mode = [wpImgVCenter, wpImgHCenter];
  finally
    FLockProp := FALSE;
  end;
end;

procedure TWPSuperPrintTest.chkTileImageClick(Sender: TObject);
begin
  grpPlacement.Enabled := not chkTileImage.Checked;
  chkStretchImage.Enabled := not chkTileImage.Checked;
  UpdatePreview;
end;

procedure TWPSuperPrintTest.PaintBox1Paint(Sender: TObject);
begin
  if not WPSuperPrint1.LockPreview then
  begin
    PageInc := 0;
    PageDec := 0;
    WPSuperPrint1.PaintPreview;
  end;
end;

procedure TWPSuperPrintTest.valPBottomMarginUnitChange(Sender: TObject);
begin
  valBTopMargin.UnitType := (Sender as TWPValueEdit).UnitType;
  valBLeftMargin.UnitType := (Sender as TWPValueEdit).UnitType;
  valBRightMargin.UnitType := (Sender as TWPValueEdit).UnitType;
  valBBottomMargin.UnitType := (Sender as TWPValueEdit).UnitType;
  valPTopMargin.UnitType := (Sender as TWPValueEdit).UnitType;
  valPLeftMargin.UnitType := (Sender as TWPValueEdit).UnitType;
  valPRightMargin.UnitType := (Sender as TWPValueEdit).UnitType;
  valPBottomMargin.UnitType := (Sender as TWPValueEdit).UnitType;
  valPHorzMargin.UnitType := (Sender as TWPValueEdit).UnitType;
  valPVertMargin.UnitType := (Sender as TWPValueEdit).UnitType;
end;

procedure TWPSuperPrintTest.PL0Click(Sender: TObject);
begin
  PL0.Down := FALSE;
  PL1.Down := FALSE;
  PL2.Down := FALSE;
  PL3.Down := FALSE;
  PL4.Down := FALSE;
  PL5.Down := FALSE;
  PL6.Down := FALSE;
  PL7.Down := FALSE;
  PL8.Down := FALSE;
  (sender as TToolButton).Down := TRUE;
  UpdatePreview;
end;

procedure TWPSuperPrintTest.DoCalcPage(Sender: TObject; RectangleNr: Integer;
  OnPageNr: Integer; var TextPageNr: Integer; var Rotated: Boolean);
var center: Integer;
  pagecount: Integer;
begin
  if (WPSuperPrint1.Columns = 2) and (WPSuperPrint1.Rows = 1) then
  begin
  // Example:
  // 2 page on one page booklet
  // Rows=1, Columns=2
    pagecount := (WPSuperPrint1.EditBox.PageCount * WPSuperPrint1.Columns + 1) div
      WPSuperPrint1.Columns;
//  if (RectangleNr and 1) = 1 then // This is the right side of the page
    if (RectangleNr and 1) = 0 then // This is the right side of the page {rd}
    begin
      inc(PageDec);
      TextPageNr := pagecount - PageDec;
    end else // This is the left side of the page
    begin
      TextPageNr := PageInc;
      inc(PageInc);
    end;
  end;
end;

procedure TWPSuperPrintTest.StartPrint(Sender: TObject);
var n, xoff, yoff: Integer;
begin
  Printer.Title := 'superprint-' + WPSuperPrint1.EditBox.LastFileName;
  if WPSuperPrint1.PrintLandscape then
    Printer.Orientation := poLandscape
  else Printer.Orientation := poPortrait;
  Printer.BeginDoc;

  xoff := GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX);
  yoff := GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY);

  n := 0;
  PageInc := 0;
  PageDec := 0;
  WPSuperPrint1.LockPreview := TRUE;
  try
    while n < WPSuperPrint1.EditBox.PageCount do
    begin
      n := WPSuperPrint1.Paint(
        Printer.Canvas,
        -xoff, -yoff,
        Printer.Canvas.Font.PixelsPerInch / 1440,
        [wpPrintFrame], // prints frame 
        n, Printer.PageNumber);
      if n < WPSuperPrint1.EditBox.PageCount then Printer.NewPage;
    end;
  finally
    Printer.EndDoc;
    WPSuperPrint1.LockPreview := FALSE;
  end;
end;

procedure TWPSuperPrintTest.WPDefaultActions1Info2(Sender: TObject);
begin
  ShowMessage('This is a demo of the component TWPSuperPrint.');
end;

procedure TWPSuperPrintTest.TwoUpBookletClick(Sender: TObject);
begin
  WPSuperPrint1.SetTwoUpBooklet(TwoUpBooklet.Checked,
    WPInchToTwips(8.5),
    WPInchToTwips(11)
    );
end;

procedure TWPSuperPrintTest.AssignPrintToTextSizeClick(Sender: TObject);
begin
  WPSuperPrint1.EditBox.Header.SetPageWH(
    WPSuperPrint1.Width,
    WPSuperPrint1.Height,
    0, 0, 0, 0);
end;

{$IFNDEF WPPDFEX}

procedure TWPSuperPrintTest.WPDefaultActions1Export(Sender: TObject;
  ActionModule: TWPDefAct; WPRichText: TWPCustomRichText);
begin
end;
{$ELSE}

procedure TWPSuperPrintTest.WPDefaultActions1Export(Sender: TObject;
  ActionModule: TWPDefAct; WPRichText: TWPCustomRichText);
var pdf: TWPPDFPrinter;
  n, PageNr, res: Integer;
  PrinterPageWidth,PrinterPageHeight  : Integer;
begin
  pdf := TWPPDFPrinter.Create(nil);
  try
    pdf.FileName := 'c:\superpdf.pdf';
    pdf.AutoLaunch := TRUE;
    pdf.CanvasReference := wprefPrinter;
    res := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
    // Attention: This is NOT Printer.PageWidth
    PrinterPageWidth := GetDeviceCaps(Printer.Handle, PHYSICALWIDTH);
    // Attention: This is NOT Printer.PageHeight
    PrinterPageHeight := GetDeviceCaps(Printer.Handle, PHYSICALHEIGHT);

    if Printer.Orientation=poLandscape then
    begin
       n := PrinterPageWidth;
       PrinterPageWidth := PrinterPageHeight;
       PrinterPageHeight := n;
    end;

    // pdf.DebugMode := TRUE;
    n := 0;
    PageInc := 0;
    PageDec := 0;
    PageNr := 1;
    WPSuperPrint1.LockPreview := TRUE;
    pdf.BeginDoc;
    try
      while n < WPSuperPrint1.EditBox.PageCount do
      begin
        if (WPSuperPrint1.Columns and 1) = 0 then
          pdf.StartPage(PrinterPageHeight, PrinterPageWidth, res, res, 0)
        else
          pdf.StartPage(PrinterPageWidth,
            PrinterPageHeight, res, res, 0);
        n := WPSuperPrint1.Paint(
          pdf.Canvas,
          0, 0,
          res / 1440,
          [wpPrintFrame], // prints frame if True
          n, PageNr);
        inc(PageNr);
        pdf.EndPage;
      end;
    finally
      pdf.EndDoc;
      WPSuperPrint1.LockPreview := FALSE;
    end;
  finally
    pdf.Free;
  end;
end;
{$ENDIF}

end.

⌨️ 快捷键说明

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