Dotnet总结(3)--打印

打印

eg:
private DataGridPrinter m_oDataGridPrinter;
protected System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private System.Windows.Forms.PrintDialog printDialog1;
public System.Drawing.Printing.PrintDocument printDocument1;
// 预览
try
            {   
m_oDataGridPrinter = new DataGridPrinter(this.dataGrid_Brand, printDocument1, (DataTable)this.dataGrid_Brand.DataSource);
                this.printPreviewDialog1.ShowDialog();
catch
            {
                MessageBox.Show("没有找到打印机,不能预览!");
            }

// 打印
        public DialogResult Print()
        {
            try
            {
//                DataTable oDTTmp = (DataTable)this.dataGrid_Brand.DataSource;
//                if (oDTTmp==null || oDTTmp.Rows.Count<=0)
//                {
//                    MessageBox.Show("主窗口中没有数据,请进行\"查询\"或者\"显示所有\"等操作,将您要打印的数据显示在主窗口中,然后再进行打印!");
//                    return DialogResult.Cancel;                   
//                }
                m_oDataGridPrinter = new DataGridPrinter(this.dataGrid_Brand, printDocument1, (DataTable)this.dataGrid_Brand.DataSource);
               
                if (this.printDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.printDocument1.Print();
                    return DialogResult.OK;
                }
                else
                {
                    return DialogResult.Cancel;
                }
            }
            catch
            {
                MessageBox.Show("没有找到打印机,不能打印!");
                return DialogResult.Cancel;
            }
            return DialogResult.OK;
        }

// 事件定义
this.printDocument1.DocumentName = "Brand";
            this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);

// 事件响应
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {                   
            e.HasMorePages = m_oDataGridPrinter.DrawDataGrid(e.Graphics);
            if (e.HasMorePages)
            {
                m_oDataGridPrinter.PageNumber += 1;
            }
            else
            {
                m_oDataGridPrinter.PageNumber = 1;
                m_oDataGridPrinter.RowCount = 0;
            }

// 基类定义
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports System.Drawing

Public Class DataGridPrinter

    Public RowCount As Integer = 0
    Public PageNumber As Integer = 1

    Private m_DataTable As DataTable
    Private m_DataGrid As DataGrid
    Private m_ImageArray(2) As Image

    Private m_PageWidth As Integer
    Private m_PageWidthMinusMargins As Integer
    Private m_PageHeight As Integer
    Private m_AdjColumnBy As Integer
    Private m_IsTooWide As Boolean
    Private m_DataGridWidth As Integer

    Private m_sSelectString As String

    Private Const c_TopMargin As Integer = 50
    Private Const c_BottomMargin As Integer = 50
    Private Const c_LeftMargin As Integer = 50
    Private Const c_RightMargin As Integer = 50
    Private Const c_VerticalCellLeeway As Integer = 10

    Public Sub New(ByVal dg As DataGrid, ByVal pd As PrintDocument, ByVal dt As DataTable)
        m_sSelectString = ""

        m_DataGrid = dg
        m_DataTable = dt

        'set the document as landscape
        pd.DefaultPageSettings.Landscape = True

        'extract our width and height values
        m_PageHeight = pd.DefaultPageSettings.PaperSize.Width
        m_PageWidth = pd.DefaultPageSettings.PaperSize.Height
        m_PageWidthMinusMargins = m_PageWidth - (c_LeftMargin + c_RightMargin)

        'hard-coded images
        'm_ImageArray(0) = Image.FromFile("images/major.gif")
        'm_ImageArray(1) = Image.FromFile("images/medium.gif")
        'm_ImageArray(2) = Image.FromFile("images/minor.gif")

        m_DataGridWidth = GetDataGridWidth()

        'set up some adjustments to scale the output later
        If m_DataGrid.Width > m_PageWidthMinusMargins Then
            m_AdjColumnBy = m_DataGrid.Width - m_PageWidthMinusMargins
            m_DataGridWidth = m_DataGridWidth - m_AdjColumnBy
            m_IsTooWide = True
        Else
            m_AdjColumnBy = m_PageWidthMinusMargins - m_DataGrid.Width
            m_DataGridWidth = m_DataGridWidth + m_AdjColumnBy
            m_IsTooWide = False
        End If
    End Sub

    Public Sub New(ByVal dg As DataGrid, ByVal pd As PrintDocument, ByVal dt As DataTable, ByVal headString As String)
        m_sSelectString = headString

        m_DataGrid = dg
        m_DataTable = dt

        'set the document as landscape
        pd.DefaultPageSettings.Landscape = True

        'extract our width and height values
        m_PageHeight = pd.DefaultPageSettings.PaperSize.Width
        m_PageWidth = pd.DefaultPageSettings.PaperSize.Height
        m_PageWidthMinusMargins = m_PageWidth - (c_LeftMargin + c_RightMargin)

        'hard-coded images
        'm_ImageArray(0) = Image.FromFile("images/major.gif")
        'm_ImageArray(1) = Image.FromFile("images/medium.gif")
        'm_ImageArray(2) = Image.FromFile("images/minor.gif")

        m_DataGridWidth = GetDataGridWidth()

        'set up some adjustments to scale the output later
        If m_DataGrid.Width > m_PageWidthMinusMargins Then
            m_AdjColumnBy = m_DataGrid.Width - m_PageWidthMinusMargins
            m_DataGridWidth = m_DataGridWidth - m_AdjColumnBy
            m_IsTooWide = True
        Else
            m_AdjColumnBy = m_PageWidthMinusMargins - m_DataGrid.Width
            m_DataGridWidth = m_DataGridWidth + m_AdjColumnBy
            m_IsTooWide = False
        End If
    End Sub

    Public Function DrawDataGrid(ByVal g As Graphics) As Boolean
        Try
            DrawPageHeader(g)
            Return DrawPageRows(g)
        Catch ex As Exception
            '      MessageBox.Show(ex.Message.ToString())
            Return False
        End Try
    End Function

    Private Sub DrawPageHeader(ByVal g As Graphics)

        'use this format when drawing later
        Dim cellFormat As New StringFormat
        cellFormat.Trimming = StringTrimming.Word
        cellFormat.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.LineLimit

        If m_sSelectString.Length > 0 Then

            'temp width to draw this column
            Dim columnWidth As Integer = m_PageWidthMinusMargins - 10

            'create a layout rectangle to draw within.
            Dim cellBounds As New RectangleF(c_LeftMargin + 12, c_TopMargin - 9, columnWidth, m_DataGrid.PreferredRowHeight + c_VerticalCellLeeway - 3)

            g.DrawString(m_sSelectString, m_DataGrid.Font, New SolidBrush(m_DataGrid.HeaderForeColor), cellBounds, cellFormat)

            'create the header rectangle
            'Dim headerBounds As New RectangleF(c_LeftMargin, c_TopMargin, m_PageWidthMinusMargins, m_DataGrid.HeaderFont.SizeInPoints + c_VerticalCellLeeway)
            Dim headerBounds As New RectangleF(c_LeftMargin + 12, c_TopMargin - 3 - 9 - 9 + m_DataGrid.PreferredRowHeight + c_VerticalCellLeeway, m_PageWidthMinusMargins - 10, m_DataGrid.PreferredRowHeight + c_VerticalCellLeeway - 3)

            'draw the header rectangle
            g.FillRectangle(New SolidBrush(m_DataGrid.HeaderBackColor), headerBounds)
        Else
            'create the header rectangle
            'Dim headerBounds As New RectangleF(c_LeftMargin, c_TopMargin, m_PageWidthMinusMargins, m_DataGrid.HeaderFont.SizeInPoints + c_VerticalCellLeeway)
            Dim headerBounds As New RectangleF(c_LeftMargin + 12, c_TopMargin - 9, m_PageWidthMinusMargins - 10, m_DataGrid.PreferredRowHeight + c_VerticalCellLeeway - 3)

            'draw the header rectangle
            g.FillRectangle(New SolidBrush(m_DataGrid.HeaderBackColor), headerBounds)

        End If

        Dim xPosition As Single = c_LeftMargin + 12 ' +12 for some padding

        'find the column names from the tablestyle
        Dim cs As DataGridColumnStyle
        For Each cs In m_DataGrid.TableStyles(0).GridColumnStyles
            If cs.Width > 0 Then

                'temp width to draw this column
                Dim columnWidth As Integer = cs.Width

                'scale the summary column width
                'note: just a quick way to fit the text to the page width
                'this is not the best way to do this but it handles the most
                'common ui path for this demo app
                If cs.MappingName = "TaskSummary" And m_IsTooWide Then
                    columnWidth -= m_AdjColumnBy
                ElseIf cs.MappingName = "TaskSummary" Then
                    columnWidth += m_AdjColumnBy
                End If

                If m_sSelectString.Length > 0 Then
                    'create a layout rectangle to draw within.
                    Dim cellBounds As New RectangleF(xPosition, c_TopMargin - 9 + m_DataGrid.PreferredRowHeight + c_VerticalCellLeeway - 3, columnWidth, m_DataGrid.HeaderFont.SizeInPoints + c_VerticalCellLeeway)

                    'draw the column name
                    g.DrawString(cs.HeaderText, m_DataGrid.HeaderFont, New SolidBrush(m_DataGrid.HeaderForeColor), cellBounds, cellFormat)
                Else
                    'create a layout rectangle to draw within.
                    Dim cellBounds As New RectangleF(xPosition, c_TopMargin, columnWidth, m_DataGrid.HeaderFont.SizeInPoints + c_VerticalCellLeeway)

                    'draw the column name
                    g.DrawString(cs.HeaderText, m_DataGrid.HeaderFont, New SolidBrush(m_DataGrid.HeaderForeColor), cellBounds, cellFormat)
                End If

                'adjust the next X Pos
                xPosition += columnWidth
            End If
        Next

    End Sub

    Private Function DrawPageRows(ByVal g As Graphics) As Boolean

        'Dim yPosition As Single = c_TopMargin + m_DataGrid.HeaderFont.SizeInPoints + (c_VerticalCellLeeway * 2)
        Dim yPosition As Single = c_TopMargin + m_DataGrid.PreferredRowHeight
        If m_sSelectString.Length > 0 Then
            yPosition += m_DataGrid.PreferredRowHeight
        End If

        'use this format when drawing later
        Dim cellFormat As New StringFormat
        cellFormat.Trimming = StringTrimming.Word
        cellFormat.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.LineLimit

        Try
            If m_DataTable.DefaultView.Count <= 0 Then
                Dim xPosition As Single = c_LeftMargin + 12 ' +12 for some padding
                Dim cellBounds As New RectangleF(xPosition, yPosition, c_LeftMargin - c_RightMargin, m_DataGrid.PreferredRowHeight)
                g.DrawString("对不起,没有找到您需要打印的数据,请您确认程序主窗口中有您需要打印的数据!", m_DataGrid.Font, New SolidBrush(m_DataGrid.HeaderForeColor), cellBounds, cellFormat)
            End If
        Catch ex As Exception
            Dim xPosition As Single = c_LeftMargin + 12 ' +12 for some padding
            Dim cellBounds As New RectangleF(xPosition, yPosition, c_LeftMargin - c_RightMargin, m_DataGrid.PreferredRowHeight)
            g.DrawString("对不起,没有找到您需要打印的数据,请您确认程序主窗口中有您需要打印的数据!", m_DataGrid.Font, New SolidBrush(m_DataGrid.HeaderForeColor), cellBounds, cellFormat)
            Return False
        End Try

            'loop each visible row
            Dim i As Integer = 0
            For i = RowCount To (m_DataTable.DefaultView.Count - 1)

                Dim xPosition As Single = c_LeftMargin + 12 ' +12 for some padding
                'g.DrawLine(New Pen(m_DataGrid.GridLineColor, 1), xPosition - 3, yPosition - 3, xPosition - 3, yPosition + m_DataGrid.PreferredRowHeight + c_VerticalCellLeeway - 1)

                'loop the columns of this row, if the column is visible
                'then print the cell value
                Dim cs As DataGridColumnStyle
                For Each cs In m_DataGrid.TableStyles(0).GridColumnStyles
                    If cs.Width > 0 Then

                        'temp width to draw this column
                        Dim columnWidth As Integer = cs.Width

                        'scale the summary column width
                        'note: just a quick way to fit the text to the page width
                        'this is not the best way to do this but it handles the most
                        'common ui path for this demo app
                        If cs.MappingName = "TaskSummary" And m_IsTooWide Then
                            columnWidth -= m_AdjColumnBy
                        ElseIf cs.MappingName = "TaskSummary" Then
                            columnWidth += m_AdjColumnBy
                        End If

                        'create a layout rectangle to draw within.
                        'Dim cellBounds As New RectangleF(xPosition, yPosition, columnWidth, m_DataGrid.Font.SizeInPoints + c_VerticalCellLeeway)
                        Dim cellBounds As New RectangleF(xPosition, yPosition, columnWidth, m_DataGrid.PreferredRowHeight)

                        'draw the item value
                        If cs.MappingName = "商标图片" Then
                            Try
                                'draw image

                                Dim by As Byte() = CType(m_DataTable.DefaultView.Item(i).Item(cs.MappingName), Byte())

                                Dim imageToDraw As Image
                                Dim oMemoryStream As System.IO.MemoryStream
                                oMemoryStream = New System.IO.MemoryStream(by, 0, by.Length)
                                'oMemoryStream = New System.IO.MemoryStream((byte[])this.GetColumnValueAtRow(source, rowNum),  0, CType(m_DataTable.DefaultView.Item(i).Item(cs.MappingName), Byte))
                                'oMemoryStream = New System.IO.MemoryStream(CType(m_DataTable.DefaultView.Item(i).Item(cs.MappingName), [] Byte), 0, CType(m_DataTable.DefaultView.Item(i).Item(cs.MappingName), Byte))

                                'MemoryStream oMemoryStream=new MemoryStream((byte[])this.GetColumnValueAtRow(source, rowNum),0,((byte[])this.GetColumnValueAtRow(source, rowNum)).Length);

                                imageToDraw = System.Drawing.Image.FromStream(oMemoryStream)
                                g.DrawImage(imageToDraw, New Rectangle(xPosition, yPosition, 125, 36))
                                'Select Case m_DataTable.DefaultView.Item(i).Item("PriorityText")
                                '    Case "Major"
                                '        g.DrawImage(m_ImageArray(0), New Point(Convert.ToInt32(cellBounds.X) - 5, Convert.ToInt32(cellBounds.Y)))
                                '    Case "Medium"
                                '        g.DrawImage(m_ImageArray(1), New Point(Convert.ToInt32(cellBounds.X) - 5, Convert.ToInt32(cellBounds.Y)))
                                '    Case "Minor"
                                '        g.DrawImage(m_ImageArray(2), New Point(Convert.ToInt32(cellBounds.X) - 5, Convert.ToInt32(cellBounds.Y)))
                                'End Select

                            Catch ex As Exception
                            End Try
                        Else
                            'draw as short date format or regular string
                            'If m_DataTable.DefaultView.Item(i).Item(cs.MappingName).GetType() Is GetType(DateTime) Then
                            '    g.DrawString(String.Format("{0:d}", m_DataTable.DefaultView.Item(i).Item(cs.MappingName)), m_DataGrid.Font, New SolidBrush(m_DataGrid.HeaderForeColor), cellBounds, cellFormat)
                            'Else
                        cellBounds.Y = cellBounds.Y + 8
                        'cellBounds.Height = cellBounds.Height - 8
                            Try
                                g.DrawString(CType(m_DataTable.DefaultView.Item(i).Item(cs.MappingName), String), m_DataGrid.Font, New SolidBrush(m_DataGrid.HeaderForeColor), cellBounds, cellFormat)
                                'End If
                            Catch ex As Exception
                            End Try
                        cellBounds.Y = cellBounds.Y - 8
                        'cellBounds.Height = cellBounds.Height + 8
                        End If

                        g.DrawLine(New Pen(m_DataGrid.GridLineColor, 1), xPosition, yPosition - 4, xPosition, yPosition + m_DataGrid.PreferredRowHeight + c_VerticalCellLeeway - 1)

                        'adjust the next X Pos
                        xPosition += columnWidth

                    End If
                Next

                'set the rowcount (which is used for a possible next page)
                RowCount += 1

                g.DrawLine(New Pen(m_DataGrid.GridLineColor, 1), m_PageWidthMinusMargins + c_LeftMargin - 1, yPosition - 4, m_PageWidthMinusMargins + c_LeftMargin - 1, yPosition + m_DataGrid.PreferredRowHeight + c_VerticalCellLeeway - 1)

                'finished with this row, draw a nice line
                g.DrawLine(New Pen(m_DataGrid.GridLineColor, 1), c_LeftMargin + 12, yPosition + m_DataGrid.PreferredRowHeight + c_VerticalCellLeeway - 2, m_PageWidthMinusMargins + c_LeftMargin, yPosition + m_DataGrid.PreferredRowHeight + c_VerticalCellLeeway - 2)

                'adjust the next Y Pos
                'yPosition += m_DataGrid.HeaderFont.SizeInPoints + c_VerticalCellLeeway + 3
                yPosition += m_DataGrid.PreferredRowHeight + c_VerticalCellLeeway + 3

                'if at end of page exit out for next page
                If yPosition * PageNumber > PageNumber * (m_PageHeight - (c_BottomMargin + c_TopMargin)) Then
                    Return True
                End If
            Next

        Return False
    End Function

    Private Function GetDataGridWidth() As Integer
        Try
            Dim cs As DataGridColumnStyle
            Dim dgWidth As Integer = 0
            For Each cs In m_DataGrid.TableStyles(0).GridColumnStyles
                If cs.Width <> 0 Then
                    dgWidth = dgWidth + cs.Width
                End If
            Next
            Return dgWidth
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class

时间: 2024-12-29 23:00:23

Dotnet总结(3)--打印的相关文章

一个免费的WEB打印控件

问题描述 PAZU组件在国内我们提供授权给包括中国电信.移动和银行在内的超过300家大中型企业和IT企业应用于基于WEB的开发PAZU支持包括Java,DotNet,JSP,PHP,ASP,Perl等各种语言开发的WEB应用项目,PAZU专门针对IE浏览器提供高端用户所需的WEB打印控制功能PAZU是4FangLAPUTACSaaS平台下的核心产品之一,PAZU也支持任何的SaaS.AJAX架构PAZU是目前国内最为稳定.功能最为强大的一款WEB打印控件,鉴于早前版本的易用性不够强,PAZU于2

C#中怎样实现单据的打印

问题描述 我的问题是这样:我想实现凭证的打印,包括收衣单号.收衣时间.业务员.顾客编号.联系电话(这些信息都是填写在某些文本控件中),然后显示收衣列表,收衣列表是用listView显示的,包括收衣明细编,服装类型,颜色,品牌,服务项目.瑕疵.价格.折扣.折后价格,可能收取很多衣物,因此是一张二维表! 解决方案 解决方案二:我不知道你用的是什么程序设计语言,能说明白一些吗?1.如果你用的是VisualBasic6.0,你可以用自带的DataReport工具,这个是英文的,这个是中文的.2.如果你用

惠普(HP) LaserJet Pro M1136 MFP 黑白多功能激光一体机 (打印 复印 扫描)驱动安装记录

惠普(HP) LaserJet Pro M1136 MFP 黑白多功能激光一体机 (打印 复印 扫描) 新入手的打印机,如果没有安装经验,不要急着开箱组装! 先打开电脑,放入驱动光盘,运行安装向导,会有动画安装指南,一步一步的教你如何操作. 考虑买这款打印机的可以去产品官方商品页面看看. 如果没有驱动光盘,可以去官方产品支持页面下载. 下面是我的安装过程: 从官网上下载的驱动程序,由于下图中没有完全对应打印机正面显示的LaserJet M1136 MFP型号的驱动,纠结了半天到底应该安装哪一个型

基于DotNet构件技术的企业级敏捷软件开发平台 - AgileEAS.NET - 文章汇总及学习指南

一.AgileEAS.NET平台简介 AgileEAS.NET平台是一套应用系统快速开发平台,用于帮助中小软件开发商快速构建自己的企业信息管理类开发团队,以达到节省开发成本.缩短开发时间,快速适应市场变化的目的,AgileEAS.NET应用开发平台包含基础类库.资源管理平台.运行容器.开发辅助工具等四大部分,资源管理平台为敏捷并行开发提供了设计.实现.测试等开发过程的并行. AgileEAS.NET平台基于软件过程改进以及构件化快速开发两方面达到这方面的目标,在软件过程改进实践方面,提出了独有的

利用Visual Basic设计打印复杂报表

一.用VB创建外部EXCEL对象 大多数大型ActiveX-enabled应用程序和其它ActiveX部件,在它们的对象层次中都提供了一个顶层外部可创建对象.该对象提供了对该层次中其它对象的访问,并且还提供对整个应用程序起作用的方法和属性. 例如,每个MicrosoftOffice应用程序提供一个顶层Application对象.下面语句显示了对Microsoftoffice/9.shtml'target='_blank'class='article'>Excel的Application对象的引用

串口-如何通过android截取收银机的打印数据,组装后上传到后台服务器,然后把数据传到打印机打印小票?

问题描述 如何通过android截取收银机的打印数据,组装后上传到后台服务器,然后把数据传到打印机打印小票? 如题.举个例子,收银机连接打印机实现收银.打印小票,现在想在中间接个硬件Android系统,截取打印数据上传到后台服务器,不对现有收银系统改造,不能影响正常收银. 注:该硬件两端通过串口/并口/usb连接收银机与打印机 解决方案 用小票打印机打印 解决方案二: 这种需要抓包的,安卓不可能.... 解决方案三: 这个你需要截取他的网络包就行了.

IE浏览器中打印网页时网页版面太大或太宽打印不全

  方法一:修改打印格式的边界设定 这个方法比较常用,但一些超宽的网页可能无效. 1.打开IE 浏览器,在打开需要打印的网页.然后点击菜单栏的「文件」 ,在选择「页面设置」 项,如下图所示: 注意:如果找不到IE菜单栏,可以在IE中按下键盘上的 Alt 键即可显示出来. 2.在「页面设置」窗口中,将「纸张大小」设定为「A4」 ,然后将左右页边距设置为「5」 ,上下页边距设定为「10」 .最后点击「确定」 按钮.如下图所示: 注意:每个型号的打印机都有最小的可打印边界,上下左右边界必须设定在您的打

win7系统电脑打印文档后出现隐藏的网站链接怎么去掉?

  win7系统电脑打印文档后出现隐藏的网站链接怎么去掉?         方法一: 1.按ALT+F9可将文档中的域代码全部显示出来(特有字会被链接替换); 2.然后编辑--替换,在查找内容中输入除{ }符号的其它字符(如果将符号{}一并拷贝过来的话会自动换为特有字),在"替换为"中输入特有字,"全部替换"即可. 方法二: 1.工具--选项,在"打印"选项卡中找到"打印文档的附加信息",取消勾选"域代码"

巧用Win7系统添加打印目录提高打印速度

Windows7系统的强大性能和高效率深受用户们的喜爱,它的很多功能与技巧要是都能够很好的运用起来的话就可以提高工作效率.比如当用户打印很多文件时,比如需打印一个文件夹中的所有目录的时候,一个一个操作的话很是麻烦.其实在Windows7系统中只要添加打印机目录,就可让打印工作变得很轻松,下面小编就跟大家分享Win7系统中提高打印效率,快速打印文件夹文件添加打印目录的技巧. 操作方法: 1.在华硕笔记本Win7系统桌面上右击选择"新建--文本文档",将以下代码复制粘贴到新建的文本文档中: