vb.net PrintForm 适合一页(横向)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15763074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PrintForm to fit to one page (Landscape)
提问by user1662306
I have a form which uses the PrintForm method that has been placed on a button.
我有一个表单,它使用已放置在按钮上的 PrintForm 方法。
The following is the code which ensures the form is printed landscape, however it cuts a chunk off the left side.
以下是确保表单横向打印的代码,但它从左侧切掉了一块。
Me.PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = True
PrintForm1.Print()
I was wondering if there was a simple way to fit to one page?
我想知道是否有一种简单的方法可以适应一页?
回答by Kasnady
Check this msdn link
检查此msdn 链接
and here
与此
Also, i more suggest to use print preview too, cause it can adjust the margin. Here is the linkabout print preview. But between these link, i would most suggest this code..
另外,我更建议使用打印预览,因为它可以调整边距。这是有关打印预览的链接。但是在这些链接之间,我最建议使用此代码..
Print a Form That Is Larger Than the Screen
打印大于屏幕的表格
- Start a new Standard EXE project in Visual Basic. Form1 is created by default.
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.
- Right-click Picture2and choose Send to Back.
- Add two labels to Picture1, leaving Picture2 empty.
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 SubRun the project.
- The Topand Bottomlabels should appear in their respective positions regardless of whether the form is completely displayed.
- 在 Visual Basic 中启动一个新的标准 EXE 项目。默认情况下创建 Form1。
将两个图片框添加到 Form1。
避免在第一个图片框内绘制第二个图片框,因为这样做会使第二个图片框成为第一个图片框的成员。相反,将第二个图片框的原点放置在第一个图片框原点的左侧。
- 右键单击Picture2并选择Send to Back。
- 将两个标签添加到图片 1,将图片 2 留空。
将以下代码添加到 Form1 的 General Declarations 部分:
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运行项目。
- 无论表单是否完全显示,顶部和底部标签都应出现在各自的位置。
This code can let us adjust the width and height of the Form snapshot, so later we wanted to print it, it would just settle its own to the way we had setting.
这段代码可以让我们调整Form快照的宽度和高度,所以稍后我们想要打印它,它会按照我们设置的方式自行解决。

