热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开源:.NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现与应用(2)

前篇说了.NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现,现在到了我们应用它们的时候了。我们需要做下面的一些事情:1、将上篇中写的程序输出类型改为类库并编
前篇说了.NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现,现在到了我们应用它们的时候了。
 
  我们需要做下面的一些事情:
  1、将上篇中写的程序输出类型改为类库并编译成PrinterPageSetting.dll;
  2、建立C#或VB.net项目,并在项目的引用处添加引用,在弹出的对话框的.NET标签中浏览并选择PrinterPageSetting.dll;
  3、在默认窗口Form1上增加一个TextBox文本框控件,默认名TextBox1(实际项目中建议采用命名及编码规范,如frmTestPrint,txtPrint)
  4、从工具箱中拖一个MainMenu到窗口Form1,创建mainMenu1菜单对象,建立主菜单menuFile
  5、单击menuFile,在其下方输入处分别建立menuFilePageSetup、menuFilePrintPreview、menuFilePrint菜单。我想大家明白这几个菜单的意义了,这是命名规范最基本的。

  准备工作做完了,看看我们怎么使用PrinterPageSetting轻松完成打印页面设置、打印机设置、打印预览对话框。

  第一步:在窗口类中申明并实例化PrinterPageSetting,当然实例化对象可以放在构造函数中。
    C#:
    private GoldPrinter.PrinterPageSetting printerPageSetting = new GoldPrinter.PrinterPageSetting();
    VB.net:
    Private printerPageSetting As New GoldPrinter.PrinterPageSetting

  第二步:写一个实现打印的具体过程
   C#:
   private void PrintDocument_PrintPage_Handler(object o,System.Drawing.Printing.PrintPageEventArgs e)
    {
        System.Drawing.Graphics g = e.Graphics;
        if (Draw(g))
        {
            e.HasMorePages = true;  //要分页打印
        }
        else
        {
            e.HasMorePages = false; //打印结束
        }
    }

    VB.net:
    Private Sub printDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Dim g As System.Drawing.Graphics = e.Graphics
        If Me.Draw(g) Then
            e.HasMorePages = True   '要分页打印
        Else
            e.HasMorePages = False  '打印结束
        End If
    End Sub

  大家可以看到我们是怎么使程序在打印时自动分页的,就是设置HasMorePages属性为真就可以了。为了清晰可见,我将真正打印的具体过程独立出来用Draw()实现。
  在第二步中我们实现打印的具体过程,在PrinterPageSetting类中,我们并不知道打印的具体实现,就设计了PrintPage委托,让调用者自己实现,然后告诉PrinterPageSetting是用哪一个方法实现,也就是第三步了。

  第三步:打印委托
  在窗口空白处双击,在Form1_Load事件中输入相关语句。当然也可以放在构造函数中,这里为了描述的方便。
    C#:
    private void Form1_Load(object sender, System.EventArgs e)
    {
        this.printerPageSetting.PrintPage += new GoldPrinter.PrintPageDelegate(PrintDocument_PrintPage_Handler);
    }
 
  如果你不知上一句是什么意思,就用这一句吧:
  this.printerPageSetting.PrintPageValue = new GoldPrinter.PrintPageDelegate(PrintDocument_PrintPage_Handler);
  意思就是告诉printerPageSetting打印的具体实现过程是PrintDocument_PrintPage_Handler(第二步)
 
    VB.net:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.printerPageSetting.PrintPageValue = New GoldPrinter.PrintPageDelegate(AddressOf printDocument_PrintPage)
    End Sub

  大家还可以一试啊,
  Private WithEvents printDocument As System.Drawing.Printing.PrintDocument  '第一步:在申明窗口级变量
  Me.printDocument = Me.printerPageSetting.PrintDocument                     '第二步:在此将两个量联系起来
  将printDocument_PrintPage(...)加上    Handles printDocument.PrintPage     


  第四步:显示页面设置/打印机设置/打印预览对话框
  分别在几个菜单上单击,具体代码如下:

    C#:
     private void menuFilePageSetup_Click(object sender, System.EventArgs e)
    {
        this.printerPageSetting.ShowPageSetupDialog();      //显示页面设置对话框
    }

    private void menuFilePrintPreview_Click(object sender, System.EventArgs e)
    {
        this.printerPageSetting.ShowPrintPreviewDialog();   //显示打印预览对话框
    }

    private void menuFilePrint_Click(object sender, System.EventArgs e)
    {
        this.printerPageSetting.ShowPrintSetupDialog();     //显示打印预览对话框
    }

    VB.net:
    Private Sub menuFilePageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuFilePageSetup.Click
        Me.printerPageSetting.ShowPageSetupDialog()
    End Sub

    Private Sub menuFilePrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuFilePrintPreview.Click
        Me.printerPageSetting.ShowPrintPreviewDialog()
    End Sub

    Private Sub menuFilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuFilePrint.Click
        Me.printerPageSetting.ShowPrintSetupDialog()
    End Sub

  哈哈,到现在为此,我们做的工作基本完成了,不过不要忘了Draw()噢。
  C#:
  private bool Draw(System.Drawing.Graphics g)
  {
       g.DrawString("Hello World!",new Font("宋体",15),Brushes.Black,new PointF(0,0));
       return false;
  }

  VB.net:
    Private Function Draw(ByVal g As System.Drawing.Graphics) As Boolean
        g.DrawString("Hello World!", New Font("宋体", 15), Brushes.Black, New PointF(0, 0))
        Return False
    End Function

  大家可不要笑这个Draw()太简单哟!运行看看,嘿,程序确实能够完成打印页面设置、打印机设置、打印预览对话框的功能,还能设置横向纵向打印,改变纸张大小等设置呢。

 

   扩展:

  当然,既然PrintDocument_PrintPage_Handler()与printDocument_PrintPage()组织的清晰且能够分页,那我就不让大家遗憾了。
  我现在分别用VB.net和C#实现一个能够分页打印的例子,而且上面的工作不白做,仅仅改一下Draw()就可以了。
 
  VB.net:
    Private Function Draw(ByVal g As System.Drawing.Graphics) As Boolean
        Return DrawText(g, Me.printerPageSetting.PrintDocument, Me.TextBox1.Text)
    End Function

    '这段代码改编自MSDN
    Private Function DrawText(ByVal g As System.Drawing.Graphics, ByVal pdoc As System.Drawing.Printing.PrintDocument, ByVal text As String) As Boolean
        g.DrawString("Hello World!", New Font("宋体", 15), Brushes.Black, New PointF(0, 0))
        Return False

        Static intCurrentChar As Int32
        Dim font As New Font("宋体", 10)

        Dim intPrintAreaHeight, intPrintAreaWidth As Int32
        With pdoc.DefaultPageSettings
            intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
        End With

        ' 横向打印,宽与高交换
        If pdoc.DefaultPageSettings.Landscape Then
            Me.Swap(intPrintAreaWidth, intPrintAreaHeight)
        End If

        '定义打印区域
        Dim rectPrintingArea As New RectangleF(pdoc.DefaultPageSettings.Margins.Left, pdoc.DefaultPageSettings.Margins.Top, intPrintAreaWidth, intPrintAreaHeight)

        Dim fmt As New StringFormat(StringFormatFlags.LineLimit)

        Dim intLinesFilled, intCharsFitted As Int32
        g.MeasureString(Mid(text, intCurrentChar + 1), font, _
                    New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
                    intCharsFitted, intLinesFilled)

        g.DrawString(Mid(text, intCurrentChar + 1), font, _
            Brushes.Black, rectPrintingArea, fmt)

        intCurrentChar += intCharsFitted

        If intCurrentChar             Return True
        Else
            intCurrentChar = 0
            Return False
        End If
    End Function

    '两个数的值互换
    Private Sub Swap(ByRef i As Int32, ByRef j As Int32)
        Dim tmp As Int32 = i
        i = j
        j = tmp
    End Sub
 
  VB.net实现了,那我就换一个面向对象的实现方法吧。
 
  C#:
  在C#项目上右键选择添加/添加类,命名为DrawText.cs,这里列出全部内容:
    ///
    /// 在绘图表面区域内绘制文本
    ///
    public class DrawText
    {
     private Graphics _graphics;
     private RectangleF _rectangleF;
     private string _text;
     private Font _font;
     private Brush _brush;
     private StringFormat _stringFormat;

     private int _startChar;
     private int _linesFilled;
     private int _charsFitted;
     
     #region 字段属性  
     public Graphics Graphics
     {
      get
      {
       return _graphics;
      }
      set
      {
       _graphics = value;
      }  
     }

     public RectangleF RectangleF
     {
      get
      {
       return _rectangleF;
      }
      set
      {
       _rectangleF = value;
      }  
     }

     public string Text
     {
      get
      {
       return _text;
      }
      set
      {
       _text = value;
      }  
     }

     public Font Font
     {
      get
      {
       return _font;
      }
      set
      {
       if (value != null)
       {
        _fOnt= value;
       }
      }  
     }

     public Brush Brush
     {
      get
      {
       return _brush;
      }
      set
      {
       if (value != null)
       {
        _brush = value;
       }
      }  
     }

     public StringFormat StringFormat
     {
      get
      {
       return _stringFormat;
      }
      set
      {
       _stringFormat = value;
      }  
     }


     public int StartChar
     {
      get
      {
       return _startChar;
      }
      set
      {
       _startChar = value;
       if (_startChar <0)
       {
        _startChar = 0;
       }
      }  
     }

     public int CharsFitted
     {
      get
      {
       return _charsFitted;
      }
     }

     public int LinesFilled
     {
      get
      {
       return _linesFilled;
      }
     }
     #endregion


     public DrawText()
     {
      _text = "";
      _fOnt= new Font("宋体",10);
      _rectangleF = new RectangleF(0,0,0,_font.Height);
      _brush = Brushes.Black;

      _startChar = 0;
      _linesFilled = 0;
      _charsFitted = 0;

      _stringFormat = new StringFormat(StringFormatFlags.LineLimit);
     }

     public DrawText(string text):this()
     {
      _text = text;
     }


     public void Draw()
     {
      if (_graphics != null)
      {
       int intLinesFilled, intCharsFitted;

       _graphics.MeasureString(_text.Substring(_startChar),_font,new SizeF(_rectangleF.Width, _rectangleF.Height),_stringFormat,out intCharsFitted,out intLinesFilled);
      
       _graphics.DrawString(_text.Substring(_startChar),_font,_brush,_rectangleF,_stringFormat);

       this._linesFilled = intLinesFilled;
       this._charsFitted = intCharsFitted;
      }
     }


  然后将原来的Draw()用下面的语句替换掉:

    private static int intCurrentCharIndex;
    private bool Draw(System.Drawing.Graphics g)
    {
     float width,height;
     width = this.printerPageSetting.PrintDocument.DefaultPageSettings.PaperSize.Width - this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Left - this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Right;
     height = this.printerPageSetting.PrintDocument.DefaultPageSettings.PaperSize.Height -this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Top - this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Bottom;
     //横向打印,宽与高交换
     if (this.printerPageSetting.PrintDocument.DefaultPageSettings.Landscape)
     {
      Swap(ref width,ref height);
     }

     RectangleF recPrintArea = new RectangleF(this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Left,this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Top,width,height);
      
      

     DrawText drawText = new DrawText(this.TextBox1.Text);
     drawText.Graphics = g;
     drawText.RectangleF = recPrintArea;
     drawText.StartChar = intCurrentCharIndex;
     drawText.Draw();

     intCurrentCharIndex += drawText.CharsFitted;

     if (intCurrentCharIndex      {
      return true;
     }
     else
     {
      intCurrentCharIndex = 0;
      return false;
     }
    }

    //两个数的值互换
    private void Swap(ref float i,ref float j)
    {
     float tmp = i;
     i = j;
     j = tmp;
    }

    在VB.net和C#的实现中,都分别写了一个互换两个数的值Swap()函数,供页面横向打印时调用,这里不再敖述。

    还需要补充说明的是在上篇:
    开源:.NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现
    http://blog.csdn.net/flygoldfish/archive/2004/08/17/77208.aspx
    中,我又稍有改动,列表如下:
    1、IPrinterPageSetting.cs中将PrintPage改为PrintPageValue,增加event PrintPageDelegate PrintPage;
    2、WebPrinterPageSetting.cs中将PrintPage改为PrintPageValue,增加public event GoldPrinter.PrintPageDelegate PrintPage;
    3、WinPrinterPageSetting.cs中PrintPage改为PrintPageValue,增加

 public event PrintPageDelegate PrintPage
 {
  add
  {
   _printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(value);
   _printPageValue = value;
  }
  remove
  {
   _printDocument.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(value);
   _printPageValue = null;
  }  
 }
    4、PrinterPageSetting.cs中PrintPage改为PrintPageValue,增加
 public event PrintPageDelegate PrintPage
 {
  add
  {
   _printerPageSetting.PrintPage += new PrintPageDelegate(value);
  }
  remove
  {
   _printerPageSetting.PrintPage -= new PrintPageDelegate(value);
  }  
 }

    5、PrinterPageSetting.cs中将PrinterPageSetting的有参构造函数public PrinterPageSetting(PrintDocument printDocument)
       的_printerPageSetting.PrintDocument = printDocument;加一个判断,即
    if (printDocument != null)
    {
        _printerPageSetting.PrintDocument = printDocument;
    } 
    这样,系统会提供一个默认的PrintDocument对象。

 

    用一个打印预览的页面总结一下主要步骤以完成本文吧!



本篇源码下载>>:
 


推荐阅读
  • 判断编码是否可立即解码的程序及电话号码一致性判断程序
    本文介绍了两个编程题目,一个是判断编码是否可立即解码的程序,另一个是判断电话号码一致性的程序。对于第一个题目,给出一组二进制编码,判断是否存在一个编码是另一个编码的前缀,如果不存在则称为可立即解码的编码。对于第二个题目,给出一些电话号码,判断是否存在一个号码是另一个号码的前缀,如果不存在则说明这些号码是一致的。两个题目的解法类似,都使用了树的数据结构来实现。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 本文介绍了C函数ispunct()的用法及示例代码。ispunct()函数用于检查传递的字符是否是标点符号,如果是标点符号则返回非零值,否则返回零。示例代码演示了如何使用ispunct()函数来判断字符是否为标点符号。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 本文介绍了在C#中SByte类型的GetHashCode方法,该方法用于获取当前SByte实例的HashCode。给出了该方法的语法和返回值,并提供了一个示例程序演示了该方法的使用。 ... [详细]
  • ①页面初始化----------收到客户端的请求,产生相应页面的Page对象,通过Page_Init事件进行page对象及其控件的初始化.②加载视图状态-------ViewSta ... [详细]
  • 第一步:PyQt4Designer设计程序界面该部分设计类同VisvalStudio内的设计,改下各部件的objectName!设计 ... [详细]
  • WPF之Binding初探
      初学wpf,经常被Binding搞晕,以下记录写Binding的基础。首先,盗用张图。这图形象的说明了Binding的机理。对于Binding,意思是数据绑定,基本用法是:1、 ... [详细]
author-avatar
手机用户2502896021
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有