Print

Recently I do some study about print. So I want to do some summary.

  • PrintDocument

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);
memoryGraphics.CopyFromScreen(
this.Location.X, this.Location.Y, 00, s);
e.Graphics.DrawImage(memoryImage, 
00);

 

  • Show all the valid Printer
    代码

                
    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;
                    }
                }

     

  • Show the Printer Properties
    代码
      [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);
            }

     

 

 

posted on 2010-07-02 18:00  林骄  阅读(443)  评论(0)    收藏  举报