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

推荐订阅源

W
WeLiveSecurity
T
Tenable Blog
Project Zero
Project Zero
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
S
Schneier on Security
Scott Helme
Scott Helme
S
Securelist
Know Your Adversary
Know Your Adversary
Vercel News
Vercel News
IT之家
IT之家
V
V2EX
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
博客园_首页
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
有赞技术团队
有赞技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
S
SegmentFault 最新的问题
AWS News Blog
AWS News Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost

博客园 - 林骄

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