如何打印整个 VB 窗体和控制打印大小

 在 Windows PrintForm Visual Basic 方法提供了一种可以打印与客户区的窗体方法。 但是,重新实现不允许您控制大小或在打印的输出的比例,或打印该非客户端区域 (标题和边框) 窗体。

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

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

 

将 Windows API 函数 bitblt,StretchBlt、 CreateCompatibleDC、 DeleteDC、 SelectObject 和转义允许更好地的位置和大小打印出的表单控制比重新实现方法。 在由两部分组成的进程中整个窗体的图像由一个不可见的图片使用 bitblt 捕获,并转换永久位图使用 AutoRedraw 属性。 然后将图片打印使用打印图片控件 (其边框为通过查询 Microsoft 知识库中以下单词找到一单独文章) 的方法:

CreateCompatibleDC


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

下面此示例需要一个表单与不可见的图片控件。

示例

<script type="text/javascript"></script>

  1. 将以下代码添加到新的项目中窗体的 General Declarations 级别中:

    注意: 下面的所有声明语句必须都是在一行每个。

    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&amp;)
    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.)
    
    						
    可以将任何其他控件添加到窗体打印。 如果在运行时绘制图片控件,一定其 AutoRedraw 属性设置为 True,以便图形将进行传输的 Windows API 调用 bitblt 和最终打印 StretchBlt。
  3. 将以下代码添加到 Form 1 的 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 将其传送到该打印机将打印 bitblt 已传输的图像的 DC。

    (可选),可以使用 StretchBlt 打印之前将放文本或图片 (Form1.Picture2) 中的图形,或直接打印到使用 Printer.Print 或 printer.line 页。 如果您通过调整 StretchBlt 的第二个和第三个参数选择,后者的方法,您可跟窗体中的图像在同一页上已打印的内容。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用VB窗体进行数据写入模板打印的过程中,一般需要以下步骤: 1. 创建VB窗体应用:首先,我们需要创建一个VB窗体应用程序,用以实现数据写入和打印功能。可以使用VB的集成开发环境(IDE)来创建窗体应用。 2. 设计数据模板:在VB窗体应用中,我们需要设计一个数据模板,用于展示需要打印的数据。可以使用VB自带的控件(如文本框、标签等),根据需求进行布局和设计。 3. 绑定数据:在VB窗体中,我们需要将需要打印的数据与数据模板中的控件进行绑定。可以通过代码实现将数据插入到对应的控件中,例如文本框的Text属性。 4. 数据校验和处理:在执行打印操作之前,需要对数据进行校验和处理,确保数据的准确性和完整性。可以添加一些逻辑判断条件,以确保数据的有效性。 5. 打印数据:在VB窗体中,可以使用PrintDocument控件或者其他第三方插件来实现数据的打印。通过代码设置打印相关属性,如纸张大小打印机选择等。然后,调用相应的打印方法,将数据模板中的内容输出到打印页面。 6. 预览和调整:在打印前,可以进行打印预览,以确保打印效果符合要求。可以使用VB提供的PrintPreviewDialog控件来实现。 7. 打印设置:在VB窗体中,可以添加一些设置项,例如打印份数、打印方式等。通过代码实现这些设置选项,并在打印操作时应用这些设置。 8. 错误处理和异常捕获:在数据写入和打印的过程中,应该添加适当的错误处理和异常捕获机制,以处理可能出现的错误和异常情况,并给出相应的提示信息。 总之,通过VB窗体应用程序,我们可以方便地实现将数据写入模板并完成打印操作。通过合理的设计和代码实现,可以提高效率并保证打印结果的准确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值