如何使用Form的PrintForm方法

EGW 2002-04-10 04:20:29
我用此方法打印出的图形总是和屏幕显示的大小不相符,如何调整打印出的图形大小?
...全文
395 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
EGW 2002-04-11
  • 打赏
  • 举报
回复
还是不行
gaoqi5037 2002-04-11
  • 打赏
  • 举报
回复
doevents
a = StretchBlt(Printer.hdc, 100, 100, Screen.Width, Screen.Height, frmprintline.hdc, 0, 0, frmprintline.Width / Screen.TwipsPerPixelX, frmprintline.Height / Screen.TwipsPerPixelY, vbSrcCopy)
Printer.EndDoc '开始打印
应该可以
EGW 2002-04-11
  • 打赏
  • 举报
回复
gaoqi:
我在程序中的语句
a = StretchBlt(Printer.hdc, 100, 100, Screen.Width, Screen.Height, frmprintline.hdc, 0, 0, frmprintline.Width / Screen.TwipsPerPixelX, frmprintline.Height / Screen.TwipsPerPixelY, vbSrcCopy)
Printer.EndDoc '开始打印
a的值为1但打印机没有打印

lindqp 2002-04-11
  • 打赏
  • 举报
回复
sdf
jett 2002-04-10
  • 打赏
  • 举报
回复
HOWTO: Use a PictureBox to Control Orientation Printing a Form
Last reviewed: December 16, 1997
Article ID: Q178076
The information in this article applies to:
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0


SUMMARY
When using the PrintForm method to print a Form, you have no control over the orientation. This article describes a way to use a PictureBox to print the client area of a Form in either orientation.



MORE INFORMATION
The PrintForm method creates its own Device Context and therefore ignores any Printer object settings. It will just use the default orientation of the current default printer. Because you cannot change how PrintForm behaves, you must print another way. Your could change printer settings with APIs, print the Form, then change them back, but a simpler approach is to print from a PictureBox. The drawback to this method of using PictureBoxes is that you are printing a Bitmap graphic, so the quality is less than the PrintForm methods. The process described here uses two PictureBoxes, each one covering the Form. The second PictureBox is to avoid a shadowing effect caused by a small offset of the redraw on a single PictureBox. This same technique can be used to print any group of controls.



Example of Using a PictureBox to Print a Form in Any Orientation

Create a new Standard EXE Project. Form1 is created by default.

Add two PictureBoxes to Form1 so that each nearly covers the Form.
NOTE: Avoid drawing the second PictureBox within the first. Doing so makes the second PictureBox a member of the first. A simple way to avoid the problem is to place the origin point of the second PictureBox to the left of the origin point of the first PictureBox. Once the second box is drawn, you can resize it so that it is the same size as the first PictureBox.


Right-mouse click on Picture2 and choose "Send to Back."

Place Picture1 on top of Picture2, covering Picture2.

Add a CommandButton and some other controls to Picture1, leaving Picture2 empty.

Place this code in the Declarations Section of the module of the Form:


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 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 Sub Command1_Click()
Dim rv As Long
Picture1.SetFocus ' So that the button doesn't look pressed
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.Orientation = vbPRORLandscape ' 2
Printer.Print ""
Printer.PaintPicture Picture2.Picture, 0, 0
Printer.EndDoc
Command1.SetFocus ' Return Focus
End Sub

Private Sub Form_Load()
Me.Show
Command1.Caption = "Print Form"
End Sub



Run the Project and click on the "Print Form" button. Note that when you click on Command1, "Print Form", it will print the PictureBox and the controls it contains in Landscape orientation.

REFERENCES
If you need to print more than just the client area of a Form or if you need to scale the printout, please see the following article in the Microsoft Knowledge Base:


ARTICLE-ID: Q161299
TITLE : HOWTO: Capture and Print the Screen, a Form, or any Window




--------------------------------------------------------------------------------

Additional query words: vb5all portrait
Keywords : vb5all vb5howto
Version : WINDOWS:5.0
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.


gaoqi5037 2002-04-10
  • 打赏
  • 举报
回复
刚才那个有点毛病

Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long


StretchBlt Printer.hdc, PrintLeft, PrintTop, PrintWidth, PrintHeight, Me.hdc, 0, 0, Me.Width / Screen.TwipsPerPixelX, Me.Height / Screen.TwipsPerPixelY, vbSrcCopy

以下单位为象素
Printer可以用 PrinterWidth/Printer.TwipsPerPixelX转换

PrintLeft 为在打印纸上图像的Left
PrintTop 为在打印纸上图像的Top
PrintWidth 为在打印纸上图像的Width
PrintHeight为在打印纸上图像的Height

gaoqi5037 2002-04-10
  • 打赏
  • 举报
回复
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

BitBlt Printer.hDC, PrintLeft, PrintTop, PrintWidth, PrintHeight, Me.hDC, 0, 0, vbSrcCopy
Printer.EndDoc

PrintLeft 为在打印纸上图像的Left
PrintTop 为在打印纸上图像的Top
PrintWidth 为在打印纸上图像的Width
PrintHeight为在打印纸上图像的Height
gaoqi5037 2002-04-10
  • 打赏
  • 举报
回复
大小不相符?
好象是自动调整大小的
gump2000 2002-04-10
  • 打赏
  • 举报
回复
Private Sub Form_Click ()
Dim Msg ' Declare variable.
On Error GoTo ErrorHandler ' Set up error handler.
PrintForm ' Print form.
Exit Sub
ErrorHandler:
Msg = "The form can't be printed."
MsgBox Msg ' Display message.
Resume Next
End Sub

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧