






















Recently I do some study about print. So I want to do some summary.
The main class is PrintDocument. And there are there dialog used to do some configurations.
1.PrintDialog.
It's used to configure print settings.Main function:Select printer
2.PageSetupDialog
It's used to configure page size and so on.Main function:Select page size,orientation,Margin
3.PrintPreviewDialog
It is used to preview.It's very useful.
Set dialogs' document as PrintDocument instance. Then the configurations done on the dialog will affect the PrintDocument.
The PrintPage Event is the most import event of PrintDocument. When call the Print method or show the PrintPreviewDialog will trigger this event.
We use this event to draw graphics or text, which will really present on the page.
This code is used to print a txt document.
代码
FileStream fs = new FileStream(filePath, FileMode.Open);
using (StreamReader sr = new StreamReader(fs))
{
stringToPrint = sr.ReadToEnd();
}int charactersOnePage = 0;
int linesPerPage = 0;
e.Graphics.MeasureString(stringToPrint, this.Font, e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnePage, out linesPerPage);
e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);
stringToPrint = stringToPrint.Substring(charactersOnePage);
e.HasMorePages = (stringToPrint.Length > 0);
And this code is used to get current form screen
代码
Size s = this.Size;
Graphics myGraphics = this.CreateGraphics();
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics
= Graphics.FromImage(memoryImage);
代码
string defaultPrinter = printDocument.DefaultPageSettings.PrinterSettings.PrinterName;
foreach (string installedPrinter in PrinterSettings.InstalledPrinters)
{
printDocument.DefaultPageSettings.PrinterSettings.PrinterName = installedPrinter;
if (printDocument.DefaultPageSettings.PrinterSettings.IsValid)
{
comboPrinters.Items.Add(installedPrinter);
if (installedPrinter == defaultPrinter)
comboPrinters.SelectedIndex = comboPrinters.Items.Count - 1;
}
}
代码
[DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter, [MarshalAs(UnmanagedType.LPWStr)] string pDeviceName, IntPtr pDevModeOutput, ref IntPtr pDevModeInput, int fMode);
[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll")]
static extern bool GlobalUnlock(IntPtr hMem);
[DllImport("kernel32.dll")]
static extern bool GlobalFree(IntPtr hMem);//ShowPrintDialog
private void OpenPrinterPropertiesDialog(string printName)
{
PrinterSettings printerSettings=new PrinterSettings();
printerSettings.PrinterName=printName;
IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
IntPtr pDevMode = GlobalLock(hDevMode);
int sizeNeeded = DocumentProperties
(this.Handle, IntPtr.Zero, printerSettings.PrinterName, pDevMode, ref pDevMode, 0);
IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded);
DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, devModeData, ref pDevMode, 14);
GlobalUnlock(hDevMode);
printerSettings.SetHdevmode(devModeData);
printerSettings.DefaultPageSettings.SetHdevmode(devModeData);
GlobalFree(hDevMode);
Marshal.FreeHGlobal(devModeData);
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。