How To Print a Form That Is Too Large for the Screen or Page

 The PrintForm method prints only the portion of the form that is displayed. This article demonstrates how to print a form that is either larger than the screen or is only partially displayed.

 

If you need to print a form that is larger than the screen, or is only partially displayed, then the controls on the form must be placed in a PictureBox. This prints the bitmap that is contained in the PictureBox, instead of a screen capture of the form itself (which is what PrintForm does). Because the PictureBox's bitmap is stored in a device context in memory, you have access to the entire bitmap regardless of what is displayed.

NOTE: For the purposes of this article, the height of the form is set to 11 inches. If another paper size is desired, such as 8.5- X 14- inches, set the sTall variable to an appropriate value, for example, 14. Also note that these assignments are made in twips because this is the default scalemode for forms. If you change the scalemode, you need to change these values to match as well.

Print a Form That Is Larger Than the Screen

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

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Add two PictureBoxes to Form1.

    Avoid drawing the second PictureBox inside the first, because doing so makes the second PictureBox a member of the first. Instead, place the origin point of the second PictureBox to the left of the origin point of the first PictureBox.
  3. Right-click Picture2 and choose Send to Back.
  4. Add two labels to Picture1, leaving Picture2 empty.
  5. Add the following code to the General Declarations section of Form1:
    Private Const twipFactor = 1440
    Private Const WM_PAINT = &HF
    Private Const WM_PRINT = &H317
    Private Const PRF_CLIENT = &H4&    ' Draw the window's client area.
    Private Const PRF_CHILDREN = &H10& ' Draw all visible child windows.
    Private Const PRF_OWNED = &H20&    ' Draw all owned windows.
    
    Private Declare Function SendMessage Lib "user32" Alias _
       "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
       ByVal wParam As Long, ByVal lParam As Long) As Long
       
    Private Sub Form_Load()
       Dim sWide As Single, sTall As Single
       Dim rv As Long
    
       Me.ScaleMode = vbTwips   ' default
       sWide = 8.5
       stall = 11   ' or 14, etc.
       Me.Width = twipFactor * sWide
       Me.Height = twipFactor * stall
       With Picture1
          .Top = 0
          .Left = 0
          .Width = twipFactor * sWide
          .Height = twipFactor * stall
       End With
       With Picture2
          .Top = 0
          .Left = 0
          .Width = twipFactor * sWide
          .Height = twipFactor * stall
       End With
       With Label1
          .Caption = "Top"
          .Left = Me.Width / 2
          .Top = 0
       End With
       With Label2
          .Caption = "Bottom"
          .Top = (twipFactor * stall) - .Height * 2
          .Left = Me.Width / 2
       End With
       Me.Visible = True
       DoEvents
    
       Picture1.SetFocus
       Picture2.AutoRedraw = True
       rv = SendMessage(Picture1.hwnd, WM_PAINT, Picture2.hDC, 0)
       rv = SendMessage(Picture1.hwnd, WM_PRINT, Picture2.hDC, _
       PRF_CHILDREN + PRF_CLIENT + PRF_OWNED)
       Picture2.Picture = Picture2.Image
       Picture2.AutoRedraw = False
    
       Printer.Print ""
       Printer.PaintPicture Picture2.Picture, 0, 0
       Printer.EndDoc
    End Sub
    					
  6. Run the project.
  7. The Top and Bottom labels should appear in their respective positions regardless of whether the form is completely displayed.

Print a Form on More Than One Page

<script type="text/javascript"></script> If the form you are printing is too large to fit on a page, you can use the same approach as in the preceding example with a minor addition. The PaintPicture method has a number of optional parameters. The clipping region parameters can be used to grab a portion of the bitmap image and print it.

To accomplish this, locate the following code in the previous example:

Printer.Print ""
Printer.PaintPicture Picture2.Picture, 0, 0
Printer.EndDoc
				

Replace those three lines of code with the following:

Printer.Print ""
Printer.PaintPicture Picture2.Picture, 0, 0, , , _
   0, 0, , Picture2.Height / 2
Printer.NewPage
Printer.PaintPicture Picture2.Picture, 0, 0, , , _
   0, Picture2.Height / 2, , Picture2.Height / 2
Printer.EndDoc
				

For ease of illustration, this example merely divides the form in half. You can adjust the clipping region appropriately for your needs.

NOTE: Visual Basic 6.0 Online Help states that PrintForm prints the entire form, whether the entire form is visible or not. This information is incorrect.

 

From:http://support.microsoft.com/kb/230502/EN-US/

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值