目标框架framework不同对项目的影响

 相逢情便深,恨不相逢早:


        背景:

        开始做中谷项目时我还不知道怎么用DLL。后来看别人用才试着去学习使用。虽然现在已经会用了,但DLL的强大之处我了解的还远远不够。下面先简单说下DLL。


        DLL是神马? 

        Windows操作系统是非常依赖于动态链接库(DLL)中的函数和数据的,实际上操作系统中几乎所有的内容都由DLL以一种或另外一种形式代表着,例如显示的字体和图标存储在GDI DLL中、显示windows桌面和处理用户的输入所需要的代码被存储在一个User DLL中、Windows编程所需要的大量API函数也呗包含在Kernel DLL中。


       使用DLL优点多多:

       最主要的一点是多个应用程序、甚至不同语言编写的应用程序可以共享一个DLL文件,实现资源“共享”,这样就大大缩小了应用程序的执行代码,有效利用内存;另一个优点是DLL文件作为一个单独的程序模块,封装性、独立性好,在软件需要升级的时候,开发人员只需要修改相应的DLl文件就可以了,而且,当DLL中的函数改变后,只要不是参数的改变,程序代码并不需要重新编译,这在编程时大大提高了软件开发和维护的效率。


 纸上得来终觉浅:


         打印:

        很多窗体都需要打印功能,为了调用方便,我把打印写成一个类封装成DLL,然后在程序里添加引用。Print类代码也附到这里,不是重点。

 

Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms

Public Class Class1

    Private PrintFont As New Font("宋体", 10)
    Private PrintLines As Integer = 50
    Private PrintRecordNumber As Integer = 45
    Private DataGridSource As DataGridView

    Private ev As PrintPageEventArgs
    Private PrintDataGrid As PrintDocument
    Private PrintPriview As PrintPreviewDialog
    Private PageSetup As PageSetupDialog
    Private PrintScale As Double = 1
    Private DataGridColumn As DataColumn
    Private DataGridRow As DataRow
    Private DataGridTable As DataTable
    Private Cols As Integer
    Private Rows As Integer = 1
    Private ColsCount As Integer
    Private PrintingLineNumber As Integer = 0
    Private PageRecordNumber As Integer
    Dim X_unit As Integer
    Dim Y_unit As Integer
    Dim _strPrint As String
    Private PrintingPageNumber As Integer = 0
    Private PageNumber As Integer
    Private PrintRecordLeave As Integer
    Private PrintRecordComplete As Integer = 0

    ‘‘‘ <summary>
    ‘‘‘ 设置打印字体
    ‘‘‘ </summary>
    ‘‘‘ <value></value>
    ‘‘‘ <remarks></remarks>
    Public WriteOnly Property setPrintFont() As System.Drawing.Font
        Set(ByVal Value As System.Drawing.Font)
            PrintFont = Value
        End Set
    End Property


    Public WriteOnly Property setPrintRecordNumber() As Integer
        Set(ByVal Value As Integer)
            PrintRecordNumber = Value
        End Set
    End Property

    ‘‘‘ <summary>
    ‘‘‘ 设置表头
    ‘‘‘ </summary>
    ‘‘‘ <value></value>
    ‘‘‘ <returns></returns>
    ‘‘‘ <remarks></remarks>
    Public Property setstrPrint() As String
        Set(ByVal value As String)
            _strPrint = value
        End Set
        Get
            Return _strPrint
        End Get
    End Property

    Sub New(ByVal TableSource As DataGridView)
        DataGridSource = TableSource
        DataGridTable = New DataTable
        DataGridTable = DataGridSource.DataSource()
        ColsCount = DataGridTable.Columns.Count
    End Sub

    Public Sub Print()
        Try
            PrintDataGrid = New System.Drawing.Printing.PrintDocument
            AddHandler PrintDataGrid.PrintPage, AddressOf Me.PrintDataGrid_PrintPage
            ‘* 
            PageSetup = New PageSetupDialog
            PageSetup.PageSettings = PrintDataGrid.DefaultPageSettings
            If PageSetup.ShowDialog() = DialogResult.Cancel Then
                Exit Sub
            End If
            ‘* 
            If PrintDataGrid.DefaultPageSettings.Landscape = False Then
                PrintLines = PrintDataGrid.DefaultPageSettings.PaperSize.Height / (PrintFont.Height + 5)
            Else
                PrintLines = PrintDataGrid.DefaultPageSettings.PaperSize.Width / (PrintFont.Height + 5)
            End If
            ‘* 
            If PrintDataGrid.DefaultPageSettings.PaperSize.PaperName.ToString = "custom" Then

            End If
            ‘* 
            PrintPriview = New PrintPreviewDialog
            PrintPriview.Document = PrintDataGrid
            PrintPriview.ShowDialog()

        Catch ex As Exception
            MessageBox.Show("错误:" & ex.ToString)
        End Try
    End Sub

    Private Sub PrintDataGrid_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
        Dim strPrint As String = Nothing
        Dim DrawBrush As New SolidBrush(System.Drawing.Color.Blue)
        Dim X As Integer
        Dim Y As Integer
        Dim DrawPoint As New PointF(X, Y)
        ‘Dim row_count As Integer
        PrintRecordLeave = DataGridTable.Rows.Count - PrintRecordComplete
        ‘* 
        PageNumber = PrintRecordLeave / PrintRecordNumber
        PrintingPageNumber = 0
        ‘* 
        If PrintDataGrid.DefaultPageSettings.Landscape = True Then
            X_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Height / (DataGridTable.Columns.Count + 2)
            Y_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Width / PrintLines
        Else
            X_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Width / (DataGridTable.Columns.Count + 2)
            Y_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Height / PrintLines
        End If
        ‘* 
        If DataGridTable.Rows.Count - PrintingPageNumber * PrintRecordNumber >= PrintRecordNumber Then
            PageRecordNumber = PrintRecordNumber
        Else
            PageRecordNumber = (DataGridTable.Rows.Count - PrintingPageNumber * PrintRecordNumber) Mod PrintRecordNumber
        End If

        While PrintingPageNumber <= PageNumber
            ‘* 
            ‘strPrint = DataGridSource.CaptionText
            DrawPoint = New PointF(X_unit, Y_unit)
            ev.Graphics.DrawString(strPrint, PrintFont, DrawBrush, DrawPoint)
            ‘* 
            Dim ColumnText(DataGridTable.Columns.Count) As String
            Dim Cols As Integer
            ‘Dim Table As Integer
            For Cols = 0 To DataGridTable.Columns.Count - 1
                ColumnText(Cols) = DataGridTable.Columns(Cols).ToString
                ‘* 
                DrawPoint = New PointF(X_unit * (Cols + 1), Y_unit * 2)
                ev.Graphics.DrawString(ColumnText(Cols), PrintFont, DrawBrush, DrawPoint)

            Next
            DrawPoint = New PointF(X_unit, Y_unit * 2)
            Call DrawLine(DrawPoint, ev)
            ‘* 
            Dim printingline As Integer = 0
            ‘* 
            Dim strNonce As String = ""
            Dim strUpData As String = ""
            While printingline < PageRecordNumber
                DataGridRow = DataGridTable.Rows(PrintRecordComplete)
                ‘* 
                For Cols = 0 To DataGridTable.Columns.Count - 1
                    DrawPoint.X = X_unit * (Cols + 1)
                    DrawPoint.Y = Y_unit * (printingline + 1 + 2)
                    If Cols = 0 Then
                        If strUpData <> "" And strNonce <> "" Then
                            If strUpData <> DataGridRow(ColumnText(0)) Then
                                ev.HasMorePages = True
                                Exit Sub
                            End If
                        End If
                    End If
                    ev.Graphics.DrawString(Convert.ToString(DataGridRow(ColumnText(Cols))), PrintFont, DrawBrush, DrawPoint)
                    strUpData = DataGridRow(ColumnText(0))
                Next
                DrawPoint.X = X_unit * 1
                DrawPoint.Y = Y_unit * (printingline + 1 + 2)
                Call DrawLine(DrawPoint, ev)
                printingline += 1
                PrintRecordComplete += 1
                ‘* 
                If PrintRecordComplete >= DataGridTable.Rows.Count Then
                    ev.HasMorePages = False
                    Exit Sub
                End If
            End While
            PrintingPageNumber += 1
            If PrintingPageNumber > PageNumber Then
                ev.HasMorePages = False
            Else
                ev.HasMorePages = True
                Exit While
            End If
        End While

    End Sub
    Private Sub DrawLine(ByVal point As PointF, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
        Dim blackPen As New Pen(System.Drawing.Color.Black, 1)
        ev.Graphics.DrawLine(blackPen, point.X, point.Y + PrintFont.Height, point.X * (ColsCount + 1), point.Y + PrintFont.Height)
    End Sub

End Class

在事件中调用时代码如下:

 

 Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click

        If dgvWindWorkLog.DataSource Is Nothing Then
            MsgBox("没有可打印的内容,请重新操作!", vbOKOnly, "系统提示")
            Exit Sub
        End If
        ‘加载dll文件,利用反射
        Dim assembly As Assembly = assembly.LoadFrom(Application.StartupPath & "\\Print.dll")
        Dim Print As New Print.Class1(Me.dgvWindWorkLog) ‘实例化打印类
        Print.setstrPrint = "工作记录卡" ‘设置打印标题
        Print.Print()
       
    End Sub



 几报加载浑错


        以上操作均没有错,如果在同一个Demo中测试,一切正常,打印效果老好了。然后将dll文件拷贝到中谷项目目录下,在事件中调用,提示错误。


目标框架framework不同对项目的影响


   测试了好多次都觉得不应该有错,我觉得哪里都一样就是到了项目中就运行不起来。还有下面这个问题,这两个问题我整整解决了一天。唉,只怪咱水平有限、经验有限啊!

 

       报表:

         不知道这叫不叫无独有偶,竟然同一天遇到两个一样的问题。


目标框架framework不同对项目的影响

     这也是做好的一个报表.grf,添加到程序中,结果每次运行到

Report = NewgrproLib.GridppReport

Report.LoadFromFile(Application.StartupPath& "\\报表.grf")时就提示上面的错误。

 


拨开云雾见青天:


     我测试Demo的目标框架是.NET Framework 4.5,而中谷项目是.NETFramework 3.5

     于是……

 


宝剑锋从磨砺出:


         No.1 经历了一次下次就知道如何解决和预防此类问题,自己做测试一定要细心。

       其实报表中最大的问题,是我开发用的Grid++Report5.6,给同组人的是5.0,(因为我很久前在机房合作开发时装了Grid++,安装文件找不到了,就跟当时给我传文件的那个同学又要了一份,结果两次的文件并不是同一版本)。这下热闹了,我们各种调试、各种注册、各种。。。终于决定删除重做。

          No.2 合作开发一定要统一版本,不管是开发环境(包括比如VS版本、framework)、SVN客户端、调试工具以及开发过程中用到的控件等。因为稍不注意就可能是推翻重来,那可是血的代价啊!

 


目标框架framework不同对项目的影响

上一篇:程序 打印 输出 调试 信息


下一篇:ECS七天训练营——DAY3