zoukankan      html  css  js  c++  java
  • 如何打印整个 VB 窗体和控制打印大小

    概要:

    Visual Basic 的 Windows PrintForm 方法提供了一种打印窗体的工作区的方法。但是,PrintForm 不允许您控制大小或在打印输出的比例,或打印窗体在非工作区 (标题和边框)。 

    下面的代码示例将使用 Windows API 函数来打印整个窗体,并提供了一种方法来控制输出的大小。 此方法还可用于打印仅客户端区域特定的大小,并控制允许文本或其他图形作为窗体的图像在同一页上打印在打印出的表单的位置。该方法也是适用于打印项目中的所有窗体。 

    注: 本示例将无法正常工作的 PostScript 打印机。 正常工作例如打印机必须使用标准的非 PostScript 激光打印机配置 (如 PCL/HP)。

    More:
    组合用 Windows API 函数 BitBlt、 StretchBlt、 CreateCompatibleDC、 DeleteDC、 SelectObject,和转义符允许更好地控制位置和打印窗体的大小比 PrintForm 方法。在两个部分组成的过程中的整个窗体图像捕获到一个不可见的图片使用 BitBlt 和变成永久位图使用 AutoRedraw 属性。然后使用该方法的打印图片控件 (通过以下单词 Microsoft 知识库中相应的查询来找到一个单独文章中做了概述) 打印图片: 

    CreateCompatibleDC


    在最大化窗体,以及任何较小的窗体时,此方法有效。 使用 GetSystemMetrics 允许以处理不同的窗口的边框样式传递给它的查询窗口以像素为单位) 的标准边框大小的视频驱动程序的通用过程。 

    下面的示例需要与不可见的图片控件在单个窗体。 

    示例

    1. 将下面的代码添加到新项目在窗体的一般声明级别: 

      注: 下面的所有声明语句都必须在同一行中。 

      DefInt A-Z
      Declare Function BitBlt Lib "gdi" (ByVal hDestDC, ByVal X, ByVal Y,
               ByVal nWidth, ByVal nHeight, ByVal hSrcDC, ByVal XSrc,
               ByVal YSrc, ByVal dwRop&)
      Declare Function CreateCompatibleDC Lib "GDI" (ByVal hDC)
      Declare Function SelectObject Lib "GDI" (ByVal hDC, ByVal hObject)
      Declare Function StretchBlt Lib "GDI" (ByVal hDC, ByVal X, ByVal Y,
               ByVal nWidth, ByVal nHeight, ByVal hSrcDC, ByVal XSrc,
               ByVal YSrc, ByVal nSrcWidth, ByVal nSrcHeight, ByVal dwRop&)
      Declare Function DeleteDC Lib "GDI" (ByVal hDC)
      Declare Function Escape Lib "GDI" (ByVal hDC, ByVal nEscape,
               ByVal nCount, lplnData As Any, lpOutData As Any)
      Declare Function GetSystemMetrics Lib "User" (ByVal nIndex)
      
      Const SM_CYCAPTION = 4
      Const SM_CXBORDER = 5
      Const SM_CYBORDER = 6
      Const SM_CXDLGFRAME = 7
      Const SM_CYDLGFRAME = 8
      Const SM_CXFRAME = 32
      Const SM_CYFRAME = 33
      
      Const TWIPS = 1
      Const PIXEL = 3
      Const NILL = 0&
      Const SRCCOPY = &HCC0020
      Const NEWFRAME = 1
      
      Dim ModeRatio, XOffset, YOffset As Integer
      

      2. 在设计时设置下列属性:

       Control         Property      Setting
         -------         --------      -------
         Form1           Name          Form1 (default)
         Form1.Picture1  Name          Picture1 (default)
         Form1.Picture2  Name          Picture2 (default)
         Form1.File1     Name          File1 (default)
        
         (In Visual Basic version 1.0 for Windows, set the CtlName/FormName
          Property for the above objects instead of the Name property.)
      

        您可以添加到该窗体,用于打印的任何其他控件。如果在运行时绘制图片控件,请务必设置 StretchBlt 通过其 AutoRedraw 属性为 True,以便将图形传输由 Windows API 调用 BitBlt 并最终打印。

      3.将下面的代码添加到 Form1 的 Form_Load 过程中:

      Sub Form_Load ()
      
      ' Size the form explicitly to match parameters of StretchBlt.
      ' Or use design time size to set coordinates.
              Form1.Move 1095, 1200, 8070, 5280
      
      ' Size two example controls.
              File1.Move 4080, 120, 2775, 2535
              Picture1.Move 240, 120, 2775, 2535
      
      ' Put up a caption to indicate how to print the form.
          Form1.Caption = "Double Click to Print Form And Text"
      
      ' The following *optional* code illustrates creating a persistent
      ' bitmap that will successfully StretchBlt to the printer.
          Picture1.AutoRedraw = -1  ' Create persistent bitmap of picture
                                    ' contents.
          Picture1.Line (0, 0)-(Picture1.ScaleWidth / 2,
          Picture1.ScaleHeight / 2), , BF
          Picture1.AutoRedraw = 0   ' Toggle off.
      
      ' Make sure the temporary workspace picture is invisible.
          Picture2.visible = 0
      End Sub
      

      4.将下面的代码添加到窗体的一般过程级别: 

      Sub FormPrint (localname As Form)
      
      ' Display cross.
          screen.MousePointer = 2
      ' Calculate ratio between ScaleMode twips and ScaleMode pixel.
          localname.ScaleMode = PIXEL
          ModeRatio = localname.height \ localname.ScaleHeight
          localname.ScaleMode = TWIPS
      
      XOffset = (localname.width - localname.ScaleWidth) \ ModeRatio
      YOffset = (localname.height - localname.ScaleHeight) \ ModeRatio
      CapSize% = GetSystemMetrics(SM_CYCAPTION) ' The height of the caption.
      
        ' The size of the fixed single border:
      FudgeFactor% = GetSystemMetrics(SM_CYBORDER)
      ' The fudgefactor is due to inevitable mapping errors when converting
      ' logical pixels to screen pixels. This example is coded for 640X480
      ' screen resolution. For 800X600, remove the fudgefactor.
      ' For other resolutions, tweak for perfection!
      
      Select Case localname.BorderStyle
      Case 0      ' None.
              XOffset = 0
              YOffset = 0
      
      Case 1      ' Fixed Single.
              XOffset = GetSystemMetrics(SM_CXBORDER)
              YOffset = GetSystemMetrics(SM_CYBORDER) + CapSize% - FudgeFactor%
      
      Case 2      ' Sizeable.
              XOffset = GetSystemMetrics(SM_CXFRAME)
              YOffset = GetSystemMetrics(SM_CYFRAME) + CapSize% - FudgeFactor%
      
      Case 3      ' Fixed Double.
              XOffset = GetSystemMetrics(SM_CXDLGFRAME) + FudgeFactor%
              YOffset = GetSystemMetrics(SM_CYDLGFRAME) + CapSize%
      
      End Select
      
      ' Size the picture to the size of the form's non-client (complete)
      ' area.
      
         Picture2.Move 0, 0, localname.Width, localname.Height
      
      ' NOTE: Bitblt requires coordinates in pixels.
         Picture2.ScaleMode = PIXEL
      ' Clear Picture property of any previous BitBlt image.
         Picture2.Picture = LoadPicture("")
      ' -1 equals true: Must Have This!!!
         Picture2.AutoRedraw = -1
      ' Assign information of the destination bitmap.
         hDestDC% = Picture2.hDC
              X% = 0: Y% = 0
              nWidth% = Picture2.ScaleWidth
              nHeight% = Picture2.ScaleHeight
      
      ' Assign information of the source bitmap.
      ' Source is entire client area of form (plus non-client area)
      ' XOffset and YOffset settings depend on the BorderStyle chosen for
      ' the form.
              hSrcDC% = localname.hDC
              XSrc% = -XOffset: YSrc% = -YOffset
      ' Show transition to BitBlt by changing MousePointer.
         Screen.MousePointer = 4
      ' Assign the SRCCOPY constant to the Raster operation.
         dwRop& = SRCCOPY
         ' The following statement must appear on one line.
         Suc% = BitBlt(hDestDC%, X%, Y%, nWidth%, nHeight%, hSrcDC%, XSrc%,
                      YSrc%, dwRop&)
      ' Start the StretchBlt process now.
      ' Assign persistent bitmap to Picture property:
         Picture2.Picture = Picture2.Image
      ' StretchBlt requires pixel coordinates.
             Picture2.ScaleMode = PIXEL
             Printer.ScaleMode = PIXEL
      ' * The following is an example of mixing text with StretchBlt.
             Printer.Print "This is a test of adding text and bitmaps "
             Printer.Print "This is a test of adding text and bitmaps "
             Printer.Print "This is a test of adding text and bitmaps "
      ' * If no text is printed in this procedure,
      ' * then you must add minimum: Printer.Print " "
      ' * to initialize Printer.hDC.
      
      ' Now display hour glass for the StretchBlt to printer.
         screen.MousePointer = 11
      
         hMemoryDC% = CreateCompatibleDC(Picture2.hDC)
         hOldBitMap% = SelectObject(hMemoryDC%, Picture2.Picture)
      
      ' You adjust the vertical stretch factor of the form in the
      ' argument "Printer.ScaleHeight - 1000":
         ApiError% = StretchBlt(Printer.hDC, 0, 192,
                     Printer.ScaleWidth - 300, Printer.ScaleHeight - 1000,
                     hMemoryDC%, 0, 0, Picture2.ScaleWidth,
                     Picture2.ScaleHeight, SRCCOPY)  ' concatenate above
      ' The second parameter above allows for text already printed: modify
      ' accordingly.
         hOldBitMap% = SelectObject(hMemoryDC%, hOldBitMap%)
         ApiError% = DeleteDC(hMemoryDC%)
      ' * The following is an example of mixing text with StretchBlt.
      ' Set the printer currentY to allow for the size of the StretchBlt
      ' image. (This is relative to size of form and stretch factors chosen)
             Printer.currentY = 2392 ' In Twips.
             Printer.Print "This is for text after the StretchBlt"
             Printer.Print "This is for text after the StretchBlt"
             Printer.Print "This is for text after the StretchBlt"
         Printer.EndDoc
         ApiError% = Escape(Printer.hDC, NEWFRAME, 0, NILL, NILL)
      
      ' Reset MousePointer to default.
         Screen.MousePointer = 1
      
      End Sub
      

      5..将下面的代码添加到 Double_Click 事件: 

      Sub Form_DblClick ()
         FormPrint Form1
      End Sub
      		
      

      6.后保存该项目运行该示例。 

      双击该窗体以调用 FormPrint 过程。将打印作为参数传递给 FormPrint 任何窗体。BitBlt 将转移到图片控件的图像,然后 StretchBlt 将其传送到打印机 DC,将打印已传输的 BitBlt 的图像。 

      (可选),您可以打印与 StretchBlt 前放置图片 (Form1.Picture2) 中的文本或图形或直接打印到使用 Printer.Print 或 Printer.Line 页。如果调整 StretchBlt 的第二个和第三个参数选择后, 一种方法可以使已打印的内容能跟在同一页上的窗体的图像。\

      参考:http://support.microsoft.com/kb/84066

  • 相关阅读:
    int string java 呼转
    E:Encountered a section with no Package: header, E:Problem with MergeList /var/lib/apt/lists/******
    东南大学课程表爬虫
    文件传输器
    springmvc-项目启动初始化类
    分享一段线程队列--生产者和消费者代码
    maven-assembly-plugin 插件打包一个bug手动解决方式
    dubbo Main独立运行,脱离web容器
    Logstash5.6.1-Kafka插件配置
    kafka 设置开机启动
  • 原文地址:https://www.cnblogs.com/laojiefang/p/2294846.html
Copyright © 2011-2022 走看看