我找到一个打印当前窗体的函数,用起来很不错的,感觉也很好。
但是由于窗体打印只可以打印可见窗体,所以我通过循环多页的
方式打印全部数据,但是这个函数一进行第二次调用循环就报错,
说我当前的打印机不支持位图打印,但是在第一次调用的时候就
没问题,我把调用函数和打印函数都贴出来,大家看看。Sub PrintMe()
'取消背景图片
Me.Picture = LoadPicture("")
'设置纸型
Call SelectForm("syt", Me.hwnd) 'syt是自定义纸张
'打印
Me.PrintForm
'卸载窗体
If printnumber>1 Then
printnumber=printnumber-1
PrintMe
Else
Unload Me
End If
End SubPublic Function SelectForm(FormName As String, ByVal MyhWnd As Long) As Integer
Dim nSize As Long ' Size of DEVMODE
Dim pDevMode As DEVMODE
Dim PrinterHandle As Long ' Handle to printer
Dim hPrtDC As Long ' Handle to Printer DC
Dim PrinterName As String
Dim aDevMode() As Byte ' Working DEVMODE
Dim FormSize As SIZEL
PrinterName = Printer.DeviceName ' Current printer
hPrtDC = Printer.hdc ' hDC for current Printer
SelectForm = FORM_NOT_SELECTED ' Set for failure unless reset in code.
' Get a handle to the printer.
If OpenPrinter(PrinterName, PrinterHandle, 0&) Then
' Retrieve the size of the DEVMODE.
nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, 0&, 0&, 0&)
' Reserve memory for the actual size of the DEVMODE.
ReDim aDevMode(1 To nSize)
' Fill the DEVMODE from the printer.
nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, _
aDevMode(1), 0&, DM_OUT_BUFFER)
' Copy the private (predefined) portion of the DEVMODE.
Call CopyMemory(pDevMode, aDevMode(1), Len(pDevMode))
' Change the appropriate member in the DevMode.
' In this case, you want to change the form name.
pDevMode.dmFormName = FormName & Chr(0) ' Must be NULL terminated!
' Set the dmFields bit flag to indicate what you are changing.
pDevMode.dmFields = DM_FORMNAME
' Copy your changes back, then update DEVMODE.
Call CopyMemory(aDevMode(1), pDevMode, Len(pDevMode))
nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, _
aDevMode(1), aDevMode(1), DM_IN_BUFFER Or DM_OUT_BUFFER)
nSize = ResetDC(hPrtDC, aDevMode(1)) ' Reset the DEVMODE for the DC.
' Close the handle when you are finished with it.
ClosePrinter (PrinterHandle)
' Selection Succeeded! But was Form Added?
If SelectForm <> FORM_ADDED Then SelectForm = FORM_SELECTED
Else
SelectForm = FORM_NOT_SELECTED ' Selection Failed!
End If
End Function
但是由于窗体打印只可以打印可见窗体,所以我通过循环多页的
方式打印全部数据,但是这个函数一进行第二次调用循环就报错,
说我当前的打印机不支持位图打印,但是在第一次调用的时候就
没问题,我把调用函数和打印函数都贴出来,大家看看。Sub PrintMe()
'取消背景图片
Me.Picture = LoadPicture("")
'设置纸型
Call SelectForm("syt", Me.hwnd) 'syt是自定义纸张
'打印
Me.PrintForm
'卸载窗体
If printnumber>1 Then
printnumber=printnumber-1
PrintMe
Else
Unload Me
End If
End SubPublic Function SelectForm(FormName As String, ByVal MyhWnd As Long) As Integer
Dim nSize As Long ' Size of DEVMODE
Dim pDevMode As DEVMODE
Dim PrinterHandle As Long ' Handle to printer
Dim hPrtDC As Long ' Handle to Printer DC
Dim PrinterName As String
Dim aDevMode() As Byte ' Working DEVMODE
Dim FormSize As SIZEL
PrinterName = Printer.DeviceName ' Current printer
hPrtDC = Printer.hdc ' hDC for current Printer
SelectForm = FORM_NOT_SELECTED ' Set for failure unless reset in code.
' Get a handle to the printer.
If OpenPrinter(PrinterName, PrinterHandle, 0&) Then
' Retrieve the size of the DEVMODE.
nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, 0&, 0&, 0&)
' Reserve memory for the actual size of the DEVMODE.
ReDim aDevMode(1 To nSize)
' Fill the DEVMODE from the printer.
nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, _
aDevMode(1), 0&, DM_OUT_BUFFER)
' Copy the private (predefined) portion of the DEVMODE.
Call CopyMemory(pDevMode, aDevMode(1), Len(pDevMode))
' Change the appropriate member in the DevMode.
' In this case, you want to change the form name.
pDevMode.dmFormName = FormName & Chr(0) ' Must be NULL terminated!
' Set the dmFields bit flag to indicate what you are changing.
pDevMode.dmFields = DM_FORMNAME
' Copy your changes back, then update DEVMODE.
Call CopyMemory(aDevMode(1), pDevMode, Len(pDevMode))
nSize = DocumentProperties(MyhWnd, PrinterHandle, PrinterName, _
aDevMode(1), aDevMode(1), DM_IN_BUFFER Or DM_OUT_BUFFER)
nSize = ResetDC(hPrtDC, aDevMode(1)) ' Reset the DEVMODE for the DC.
' Close the handle when you are finished with it.
ClosePrinter (PrinterHandle)
' Selection Succeeded! But was Form Added?
If SelectForm <> FORM_ADDED Then SelectForm = FORM_SELECTED
Else
SelectForm = FORM_NOT_SELECTED ' Selection Failed!
End If
End Function
解决方案 »
- 谁吧把我的回复删除了?是个男人说一下。
- VB查询SQL中的数据
- VBA录制宏的代码自动在模块中,可执行;但放入SHEET里的按钮代码,提示错误
- 请问有没有用VB6做的控件,可以查询多条件的(即可以对不同字段进行组合后再查询对应的报表,数据库是SQL)?那里有得下载?谢谢
- 请问哪位大侠有Spread6.0以上的版本,(含补丁),在线等(急)
- 请教在VB中TreeView控件如何使用
- 我要实现这样一个功能,应该如何实现?
- 我刚接触数据库,有很多问题
- 我想问一下!!如果在一个窗体中打开另一个窗体!!
- 如何在程序中输入文件夹共享密码?
- 我想实现这样的数据库查询要怎么实现?
- vsPrinter怎么实现Header的自由设置(可以设置sections的字体)
应该用for eachDim frm As Form
For Each frm In Forms
Call SelectForm("syt", frm.hwnd) 'syt是自定义纸张
'打印
frm.PrintForm
'卸载窗体
If printnumber>1 Then
printnumber=printnumber-1
PrintMe
Else
Unload frm
End If