打印样例

MDI下

一个子窗体上控件很多,所以在1280*1024分辨率下做的,
然后在1024*768下,控件不能全部显示出来,也不能全部打印出来。:(

考虑过用滚动条,但用printform也只能打印出当前屏幕显示的东西

怎么解决啊?不会让我用printer吧,几百个控件会写s人地

________________________________________________________________

给你一些参考吧

[名称]           打印预览

[语言种类]       Visual Basic

[类别一]         打印

[类别二]         空

[类别三]         空

[数据来源]       未知

[来源时间]       未知

[保存时间]       2002-01-10

[关键字一]       打印

[关键字二]       预览

[关键字三]       空

[文件列表]       空

[内容简介]       空

[心得体会]       空

[源代码内容]

      Option Explicit

      Private Sub Form_Load()
        CommonDialog1.CancelError = True
        Command1.Caption = "Load Picture"
        Command2.Caption = "Print Preview"
        Command3.Caption = "Print"
      End Sub

      Private Sub Command1_Click()
        Dim sFileFilter As String

        On Error GoTo ErrHandler

        sFileFilter = "Bitmap Files (*.bmp)|*.bmp|"
        sFileFilter = sFileFilter & "GIF Files (*.gif)|*.gif|"
        sFileFilter = sFileFilter & "Icon Files (*.ico)|*.ico|"
        sFileFilter = sFileFilter & "JPEG Files (*.jpg)|*.jpg|"
        sFileFilter = sFileFilter & "Windows MetaFiles (*.wmf)|.wmf"
        With CommonDialog1
            .Filter = sFileFilter
            .ShowOpen
            If .FileName <> " " Then
              Picture2.Picture = LoadPicture(.FileName)
            End If
        End With

ErrHandler:
            Exit Sub
      End Sub

      Private Sub Command2_Click()
        Dim dRatio As Double
        dRatio = ScalePicPreviewToPrinterInches(Picture1)
        PrintRoutine Picture1, dRatio
      End Sub

      Private Sub Command3_Click()
        Printer.ScaleMode = vbInches
        PrintRoutine Printer
        Printer.EndDoc
      End Sub

      Private Function ScalePicPreviewToPrinterInches _
        (picPreview As PictureBox) As Double

        Dim Ratio As Double ' Ratio between Printer and Picture
        Dim LRGap As Double, TBGap As Double
        Dim HeightRatio As Double, WidthRatio As Double
        Dim PgWidth As Double, PgHeight As Double
        Dim smtemp As Long

        ' Get the physical page size in Inches:
        PgWidth = Printer.Width / 1440
        PgHeight = Printer.Height / 1440

        ' Find the size of the non-printable area on the printer to
        ' use to offset coordinates. These formulas assume the
        ' printable area is centered on the page:
        smtemp = Printer.ScaleMode
        Printer.ScaleMode = vbInches
        LRGap = (PgWidth - Printer.ScaleWidth) / 2
        TBGap = (PgHeight - Printer.ScaleHeight) / 2
        Printer.ScaleMode = smtemp

        ' Scale PictureBox to Printer's printable area in Inches:
        picPreview.ScaleMode = vbInches

        ' Compare the height and with ratios to determine the
        ' Ratio to use and how to size the picture box:
        HeightRatio = picPreview.ScaleHeight / PgHeight
        WidthRatio = picPreview.ScaleWidth / PgWidth
        If HeightRatio < WidthRatio Then
            Ratio = HeightRatio
            smtemp = picPreview.Container.ScaleMode
            picPreview.Container.ScaleMode = vbInches
            picPreview.Width = PgWidth * Ratio
            picPreview.Container.ScaleMode = smtemp
        Else
            Ratio = WidthRatio
            smtemp = picPreview.Container.ScaleMode
            picPreview.Container.ScaleMode = vbInches
            picPreview.Height = PgHeight * Ratio
            picPreview.Container.ScaleMode = smtemp
        End If

        ' Set default properties of picture box to match printer
        ' There are many that you could add here:
        picPreview.Scale (0, 0)-(PgWidth, PgHeight)
        picPreview.Font.Name = Printer.Font.Name
        picPreview.FontSize = Printer.FontSize * Ratio
        picPreview.ForeColor = Printer.ForeColor
        picPreview.Cls

        ScalePicPreviewToPrinterInches = Ratio
      End Function

      Private Sub PrintRoutine(objPrint As Object, _
                              Optional Ratio As Double = 1)
        ' All dimensions in inches:

        ' Print some graphics to the control object
        objPrint.Line (1, 1)-(1 + 6.5, 1 + 9), , B
        objPrint.Line (1.1, 2)-(1.1, 2)
        objPrint.PaintPicture Picture2, 1.1, 1.1, 0.8, 0.8
        objPrint.Line (2.1, 1.2)-(2.1 + 5.2, 1.2 + 0.7), _
                        RGB(200, 200, 200), BF

        ' Print a title
        With objPrint
            .Font.Name = "Arial"
            .CurrentX = 2.3
            .CurrentY = 1.3
            .FontSize = 35 * Ratio
            objPrint.Print "Visual Basic Printing"
        End With

        ' Print some circles
        Dim x As Single
        For x = 3 To 5.5 Step 0.2
            objPrint.Circle (x, 3.5), 0.75
        Next

        ' Print some text
        With objPrint
            .Font.Name = "Courier New"
            .FontSize = 30 * Ratio
            .CurrentX = 1.5
            .CurrentY = 5
            objPrint.Print "It is possible to do"

            .FontSize = 24 * Ratio
            .CurrentX = 1.5
            .CurrentY = 6.5
            objPrint.Print "It is possible to do print"

            .FontSize = 18 * Ratio
            .CurrentX = 1.5
            .CurrentY = 8
            objPrint.Print "It is possible to do print preview"
        End With
      End Sub

[名称]           用printer对象打印表格

[语言种类]       Visual Basic

[类别一]         打印

[类别二]         报表

[类别三]         空

[数据来源]       未知

[来源时间]       未知

[保存时间]       2002-08-11

[关键字一]       printer

[关键字二]       打印

[关键字三]       表格

[文件列表]       空

[内容简介]       空

[心得体会]       空

[源代码内容]

   用msflexgrid控件显示的表格,要将它打印出来,最简单的方法是用printform方法,然而这只适
合于数据正好能被屏幕显示的,即数据量少的,而且这种打印效果很差。而用printer对象进行打印编
程,虽然麻烦点,但效果却是相当不错的,你可以自定义打印格式,打印页数,表格的粗细,字体大
小等。实际上用printer对象进行打印编程是比较简单的。

下面我就用一实例来说明:

打印的内容是一张数据表,这里就只有两列数据,包括标题,副标题。(用A4纸打印)

假设数据处在C_DataArray(),和R_DataArray()中C_Name与R_Name分别为两数据项的字段名

Public Sub Printtable()
'初始化

Dim printer1 as Printer

Dim pageheader
Dim pagefooter
Dim pageleft
Dim pageright
Dim usewidth
Dim useheight
Dim i, j, k As Integer
Dim word As String
Dim startx
Dim starty
Dim startyline             ‘ 用来纪录打印竖线的起点

Dim endyline              ’ 用来纪录打印竖线的末点

设置页面参数

pageheader = 25
pagefooter = 25
pageleft = 20
pageright = 20


 With printer1
    .PaperSize = 9
    .ScaleMode = 6
    .FontBold = True
    .ScaleLeft = -20
    .ScaleTop = -25
    .ScaleWidth = 210                                        '设置为A4纸
    .ScaleHeight = 297
    usewidth = .ScaleWidth - 40
    useheight = .ScaleHeight - 50
    .CurrentX = 0
    .CurrentY = 0
    .DrawWidth = 5
End With
'打印标题
With printer1
    .FontSize = 20
    .CurrentX = (usewidth - .TextWidth(DataTitle)) / 2
    .CurrentY = pageheader + .ScaleTop
End With
    printer1.Print DataTitle
  
 '打印副标题
   printer1.FontSize = 15
    word = DataTitle2
    printer1.CurrentX = usewidth - printer1.TextWidth(word)
    printer1.Print word
   
 '打印第一条线  Line方法不能用在with ....end with里

printer1.CurrentX = pageleft + printer1.ScaleLeft
startyline = printer1.CurrentY
'线宽

printer1.Line -((printer1.ScaleLeft + printer1.ScaleWidth - pageleft), printer1.CurrentY)
printer1.FontSize = 10
'printer1.Print vbLf
printer1.CurrentY = printer1.CurrentY + 1
'打印第一个字段名
starty = printer1.CurrentY
printer1.CurrentX = ((printer1.ScaleWidth - 40) / 2 - printer1.TextWidth(C_Name)) / 2

printer1.Print C_Name


'打印第二个字段名
printer1.CurrentX = usewidth / 2 + ((usewidth / 2 - printer1.TextWidth(R_Name)) / 2)
printer1.CurrentY = starty
printer1.Print R_Name
printer1.CurrentY = printer1.CurrentY + 1


'打印数据和横线,rownum为数据行数

For i = 1 To rownum
'判断是否该页已打满
    If printer1.CurrentY >= useheight Then

    '打印横线
    printer1.CurrentX = printer1.ScaleLeft + pageleft
    printer1.Line -((printer1.ScaleLeft + printer1.ScaleWidth - pageleft), printer1.CurrentY)
    printer1.CurrentY = printer1.CurrentY + 1
   
    
        '打印三条竖线
        endyline = printer1.CurrentY
        printer1.Line (0, startyline)-(0, endyline)
        printer1.Line (usewidth / 2, startyline)-(usewidth / 2, endyline)
        printer1.Line (usewidth, startyline)-(usewidth, endyline)
       
       '打印页号
       With printer1
        .CurrentX = (.ScaleWidth - .TextWidth(.Page)) / 2 - pageleft
        .CurrentY = useheight + 3
       End With
        printer1.Print printer1.Page
        printer1.NewPage
       With printer1
            .CurrentX = pageleft + .ScaleLeft
            .CurrentY = pageheader + .ScaleTop
            startyline = .CurrentY
       End With
       
    End If
   
    '打印一行数据
    printer1.CurrentX = ((printer1.ScaleWidth - 40) / 2 - printer1.TextWidth(C_DataArray(i))) / 2
    starty = printer1.CurrentY
    printer1.Print C_DataArray(i)
    printer1.CurrentX = (printer1.ScaleWidth - 40) / 2 + ((printer1.ScaleWidth - 40) / 2 - printer1.TextWidth(R_DataArray(i) )) / 2
    printer1.CurrentY = starty
    printer1.Print R_DataArray(i)
    printer1.CurrentY = printer1.CurrentY + 1   
Next i
       '打印最后一条横线
    printer1.CurrentX = printer1.ScaleLeft + pageleft
    printer1.Line -((printer1.ScaleLeft + printer1.ScaleWidth - pageleft), printer1.CurrentY)
    endyline = printer1.CurrentY
'打印三条竖线
        printer1.Line (0, startyline)-(0, endyline)
        printer1.Line (usewidth / 2, startyline)-(usewidth / 2, endyline)
        printer1.Line (usewidth, startyline)-(usewidth, endyline)
  
 
        '打印页号
       With printer1
        .CurrentX = (.ScaleWidth - .TextWidth(.Page)) / 2 - pageleft
        .CurrentY = useheight + 3
       End With
        printer1.Print printer1.Page
      
printer1.EndDoc
end sub

[名称]           谈谈VB应用程序的几种打印方法

[语言种类]       Visual Basic

[类别一]         打印

[类别二]         空

[类别三]         空

[数据来源]       未知

[来源时间]       未知

[保存时间]       2002-02-27

[关键字一]       打印方法

[关键字二]       应用程序

[关键字三]       空

[文件列表]       空

[内容简介]       空

[心得体会]       空

[源代码内容]

谈谈Visual Basic应用程序的几种打印方法
浏览: 475 次
打印是编制应用程序过程中最复杂的事情之一,不同的打印机提供了各种不同的功能。编写能充分利用打印机性能的子程序与编写应用程序的其它部分一样都很困难。庆幸的微软的Windows平台使打印工作变得相对容易了

  最近,笔者编制出一套综合试题库管理系统,完成试题的编辑、自动组卷及打印试卷等工作,在编程时,也碰到了打印问题,笔者找到了几种打印方法,现将它们整理出来,以飧读者,希望能给您的工作提供一点方便。

  一、采用Visual Basic提供简单的打印函数PrintForm方法

  应用程序窗体的PrintForm方法时,Visual Basic把窗体的位图送到当前打印机。该方法的优点在于它几乎不需要任何编程,但也有很大缺陷。最为突出的是当低分辨率图形在高分辨率打印机上打印时,其结果令人无法满意,会产生锯齿。下面代码将在打印机上打印窗体。

Private Sub Command1_Click() '用PrintForm打印
     Me.PrintForm '打印窗体的可见区域
End Sub

  二、用Printer对象
  用Printer对象可以进行高分辨率输出,但要想产生复杂的打印输出,编程较为繁琐。Printer对象代表系统确省的打印机。Printer对象支持许多由窗体和图形框所支持的属性和方法,三种对象都有画线和画方框。应用程序可用以下列代码在Printer对象上画出一平方英寸的方框。它离左上角二英寸。注意:打印机以twips来测量距离。每英寸有1440个twips。

  Printer.Line(2*1440,2*1440)-Step(1440,1440), ,B

  打印机、从窗体和图形框都有Circle、PaintPicture、Print、Pset、TextHeight、TextWidth方法。使用这些方法,应用程序可以为打印机生成高分辨率输出。
  打印文本直接用Print方法,见下列代码:

  Printer.Print “Hello,China ComputerWorld!” ‘打印字符串

  Printer对象还有一些窗体和图形框都没有方法:

  NewPage告诉打印机,程序对当前输出页的发送已经结束。Printer对象应开始新的一页。

  EndDoc告诉VISUAL BASIC,程序创建文档结束。VISUAL BASIC应将它发送到物理打印机上打印。

  KillDoc取消当前打印作业。应用程序应该终止由EndDoc和KillDoc所设定的每个打印作业。

  Zoom属性用于定义打印输出的缩放因子。

  Copies属性用于定义打印的副本数目。

  三、采用直接将数据传送打印机的方法进行打印输出

  该方法也不太实用。有两种方法将数据送往打印机。第一种是用Print # 方法,就象将数据写入一个文件一样写数据。另一种方法写端口,但不是送文本,而是送特定的PCL语言,PCL表示打印控制语言(Print Control Language)。它是一种特殊语言,用转义代码来控制打印机的具体动作。因为此方法太繁琐,本文不做太多介绍,见谅。

  四、使用RichTextBox控件的SelPrint方法

  如果你在编程时用到了RichTextBox控制,那么你可以使用该控件的SelPrint 方法来打印,使用非常简单。下面一段代码即用RichTextBox控件的SelPrint 方法来完成打印。

Private Sub Command3_Click() 'SelPrint方法
CommonDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums
If RTF1.SelLength = 0 Then  'RTF1为窗体的RichTextBox控制
CommonDialog1.Flags = CommonDialog1.Flags + cdlPDAllPages
    Else
CommonDialog1.Flags = CommonDialog1.Flags + cdlPDSelection
    End If
    CommonDialog1.CancelError = True
    On Error Resume Next
    CommonDialog1.ShowPrinter
    If Err.Number = cdlCancel Then Exit Sub
    If Err.Number < > 0 Then
      Beep
      MsgBox "Error printing file.
" & vbCrLf + Err.Description, vbOKOnly + vbExclamation,
"Printing Error!"
    Exit Sub
    End If
    Printer.Print ""
RTF1.SelPrint CommonDialog1.hDC '
打印RTF1控件的可见区域
End Sub

  上面代码先进行打进设置,再进行打印。如果不需要设置,采用下面代码更为简单。
  RTF1.SelPrint Printer.hDC '打印RTF1控件的可见区域

  五、可以在VB中调用Word 97提供的OLE自动化服务

  利用Word 97强大的打印功能来完成VISUAL BASIC打印,笔者认为这是最令人满意的方法。下面代码说明VB如何与Word集成。

Private Sub Command4_Click() '调用Word打印
    Dim objWord As Object
    Const CLASSOBJECT = "Word.Application"
       On Error GoTo objError
      Set objWord = CreateObject(CLASSOBJECT)
    objWord.Visible = True
    objWord.Documents.Add
With objWord
.ActiveDocument.Paragraphs.Last.Range.Bold = False
.ActiveDocument.Paragraphs.Last.Range.Font.Size = 20
.ActiveDocument.Paragraphs.Last.Range.Font.Name = "黑体"
.ActiveDocument.Paragraphs.Last.Range.Font.ColorIndex = 4
.ActiveDocument.Paragraphs.Last.Range.Text =
"我是计算机世界读者!"
  End With
     Clipboard.Clear
Clipboard.SetText "通过剪切板向WORD传送数据!"
   objWord.Selection.Paste
   objWord.PrintPreview = True '预览方式
   'objWord.PrintOut'执行打印
   'objWord.Quit'退出Word
Exit Sub
objError:
If Err < > 429 Then
     MsgBox Str$(Err) & Error$
Set objWord = Nothing '不能创建Word对象则退出
    Exit Sub
Else
    Resume Next
End If
End Sub

  六、用VC编制DLL模块完成打印
  在VISUAL BASIC中调用该模块的混合编程的方法进行打印输出。因涉及VC编程比较繁琐,因篇幅问题这里不再讨论,但可以肯定用VC编制的打印将更具特色。

  笔者的试题库采用的是第五和第六两种方案,如果机器上没有Word ,就调用DLL打印模块。打印是编程工作中重要的一部分,总的来说,微软已经使打印变得极为简便,但还要我们做一些必须的工作。希望本文能够拓展你编程的思路,编出更好的应用程序。



阅读更多
个人分类: VB
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭