240 lines
5.8 KiB
C#
240 lines
5.8 KiB
C#
using System;
|
|
using System.Drawing.Printing;
|
|
using System.Globalization;
|
|
using System.Windows.Forms;
|
|
|
|
namespace GoldPrinter
|
|
{
|
|
public class WinPrinterPageSetting : IPrinterPageSetting
|
|
{
|
|
private PrintPageDelegate _printPageValue;
|
|
|
|
private ImportExcelDelegate _importExcelValue;
|
|
|
|
private PrintDocument _printDocument;
|
|
|
|
public event PrintPageDelegate PrintPage
|
|
{
|
|
add
|
|
{
|
|
this._printDocument.PrintPage += new PrintPageEventHandler(value.Invoke);
|
|
this._printPageValue = value;
|
|
}
|
|
remove
|
|
{
|
|
this._printDocument.PrintPage -= new PrintPageEventHandler(value.Invoke);
|
|
this._printPageValue = null;
|
|
}
|
|
}
|
|
|
|
public PrintDocument PrintDocument
|
|
{
|
|
get
|
|
{
|
|
return this._printDocument;
|
|
}
|
|
set
|
|
{
|
|
this._printDocument = value;
|
|
}
|
|
}
|
|
|
|
public PrintPageDelegate PrintPageValue
|
|
{
|
|
get
|
|
{
|
|
return this._printPageValue;
|
|
}
|
|
set
|
|
{
|
|
this._printPageValue = value;
|
|
this._printDocument.PrintPage += new PrintPageEventHandler(this._printPageValue.Invoke);
|
|
}
|
|
}
|
|
|
|
public ImportExcelDelegate ImportExcelValue
|
|
{
|
|
get
|
|
{
|
|
return this._importExcelValue;
|
|
}
|
|
set
|
|
{
|
|
this._importExcelValue = value;
|
|
}
|
|
}
|
|
|
|
public WinPrinterPageSetting() : this(null)
|
|
{
|
|
}
|
|
|
|
public WinPrinterPageSetting(PrintDocument printDocument)
|
|
{
|
|
if (printDocument != null)
|
|
{
|
|
this._printDocument = printDocument;
|
|
}
|
|
else
|
|
{
|
|
this._printDocument = new PrintDocument();
|
|
}
|
|
}
|
|
|
|
public PageSettings ShowPageSetupDialog()
|
|
{
|
|
return this.ShowPageSetupDialog(this._printDocument);
|
|
}
|
|
|
|
public PrinterSettings ShowPrintSetupDialog()
|
|
{
|
|
return this.ShowPrintSetupDialog(this._printDocument);
|
|
}
|
|
|
|
public void ShowPrintPreviewDialog()
|
|
{
|
|
this.ShowPrintPreviewDialog(this._printDocument);
|
|
}
|
|
|
|
protected virtual PageSettings ShowPageSetupDialog(PrintDocument printDocument)
|
|
{
|
|
this.ThrowPrintDocumentNullException(printDocument);
|
|
PageSettings result = new PageSettings();
|
|
PageSetupDialog pageSetupDialog = new PageSetupDialog();
|
|
result = printDocument.DefaultPageSettings;
|
|
try
|
|
{
|
|
pageSetupDialog.Document = printDocument;
|
|
Margins margins = printDocument.DefaultPageSettings.Margins;
|
|
if (RegionInfo.CurrentRegion.IsMetric)
|
|
{
|
|
margins = PrinterUnitConvert.Convert(margins, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
|
|
}
|
|
PageSettings pageSettings = (PageSettings)printDocument.DefaultPageSettings.Clone();
|
|
pageSetupDialog.PageSettings = pageSettings;
|
|
pageSetupDialog.PageSettings.Margins = margins;
|
|
DialogResult dialogResult = pageSetupDialog.ShowDialog();
|
|
if (dialogResult == DialogResult.OK)
|
|
{
|
|
result = pageSetupDialog.PageSettings;
|
|
printDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
|
|
}
|
|
}
|
|
catch (InvalidPrinterException e)
|
|
{
|
|
this.ShowInvalidPrinterException(e);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.ShowPrinterException(ex);
|
|
}
|
|
finally
|
|
{
|
|
pageSetupDialog.Dispose();
|
|
pageSetupDialog = null;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
protected virtual PrinterSettings ShowPrintSetupDialog(PrintDocument printDocument)
|
|
{
|
|
this.ThrowPrintDocumentNullException(printDocument);
|
|
PrinterSettings result = new PrinterSettings();
|
|
PrintDialog printDialog = new PrintDialog();
|
|
try
|
|
{
|
|
printDialog.AllowSomePages = true;
|
|
printDialog.Document = printDocument;
|
|
DialogResult dialogResult = printDialog.ShowDialog();
|
|
if (dialogResult == DialogResult.OK)
|
|
{
|
|
result = printDialog.PrinterSettings;
|
|
printDocument.Print();
|
|
}
|
|
}
|
|
catch (InvalidPrinterException e)
|
|
{
|
|
this.ShowInvalidPrinterException(e);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.ShowPrinterException(ex);
|
|
}
|
|
finally
|
|
{
|
|
printDialog.Dispose();
|
|
printDialog = null;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private void Excel_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
|
|
{
|
|
ToolBarButton button = e.Button;
|
|
string toolTipText = button.ToolTipText;
|
|
if (toolTipText == "Import Excel" && this._importExcelValue != null)
|
|
{
|
|
this._importExcelValue.BeginInvoke(sender, null, null, null);
|
|
}
|
|
}
|
|
|
|
protected virtual void ShowPrintPreviewDialog(PrintDocument printDocument)
|
|
{
|
|
this.ThrowPrintDocumentNullException(printDocument);
|
|
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
|
|
printPreviewDialog.Text = printDocument.DocumentName;
|
|
printPreviewDialog.WindowState = FormWindowState.Maximized;
|
|
if (this._importExcelValue != null)
|
|
{
|
|
if (printPreviewDialog.Controls[1] is ToolBar)
|
|
{
|
|
ToolBar toolBar = (ToolBar)printPreviewDialog.Controls[1];
|
|
ToolBarButton toolBarButton = new ToolBarButton();
|
|
toolBarButton.ToolTipText = "Import Excel";
|
|
toolBarButton.ImageIndex = 2;
|
|
toolBar.ButtonClick += new ToolBarButtonClickEventHandler(this.Excel_ButtonClick);
|
|
toolBar.Buttons.Add(toolBarButton);
|
|
}
|
|
}
|
|
try
|
|
{
|
|
printPreviewDialog.Document = printDocument;
|
|
DialogResult dialogResult = printPreviewDialog.ShowDialog();
|
|
if (dialogResult == DialogResult.OK)
|
|
{
|
|
}
|
|
}
|
|
catch (InvalidPrinterException e)
|
|
{
|
|
this.ShowInvalidPrinterException(e);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.ShowPrinterException(ex);
|
|
}
|
|
finally
|
|
{
|
|
printPreviewDialog.Dispose();
|
|
printPreviewDialog = null;
|
|
}
|
|
}
|
|
|
|
protected virtual void ThrowPrintDocumentNullException(PrintDocument printDocument)
|
|
{
|
|
if (printDocument == null)
|
|
{
|
|
throw new Exception("关联的打印文档不能为空!");
|
|
}
|
|
}
|
|
|
|
protected virtual void ShowInvalidPrinterException(InvalidPrinterException e)
|
|
{
|
|
MessageBox.Show("未安装打印机,请进入系统控制面版添加打印机!", "打印", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
}
|
|
|
|
protected virtual void ShowPrinterException(Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "打印", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
}
|
|
}
|
|
}
|