惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
博客园 - 聂微东
有赞技术团队
有赞技术团队
The Cloudflare Blog
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
T
The Blog of Author Tim Ferriss
D
Docker
L
LangChain Blog
Vercel News
Vercel News
C
Check Point Blog
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
人人都是产品经理
人人都是产品经理

博客园 - 林骄

Android note 2010.08.02 v2 2010.08.03 Android note Android Note 2010.08.02 Android Note 2011.08.01 [Biztalk]问题集 - 林骄 - 博客园 Winform导出Excel Excel单元格颜色 判断系统版本 ListView && XmlReader How to open a folder with explorer - 林骄 Can't open Infragistics help document How to get local machine date format.如何获取本机时间格式 cannot open user default database WebService Tips 中文全半角转换 Windows Service 装机备忘 Db2中的时间
Print
林骄 · 2010-07-02 · via 博客园 - 林骄

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