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

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Security Latest
Security Latest
P
Privacy International News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
S
Secure Thoughts
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
D
DataBreaches.Net
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
罗磊的独立博客
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
V
V2EX
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tenable Blog
T
Threat Research - Cisco Blogs
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
Lohrmann on Cybersecurity
F
Full Disclosure
H
Help Net Security
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
A
Arctic Wolf
O
OpenAI News
S
Securelist

博客园 - 林骄

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