我的自定义纸张设定不知道哪里错了,帮忙看看.

woolnil 2008-05-24 12:43:29
第一个过程是修改主过程,第二个过程是设置纸张的过程.
运行时纸张设置不正确.
"如何在 Windows NT 和 Windows 2000 中使用自定义页面大小打印"这篇文章也看了,好像不太适合这的这种方法.
如果这两个函数无法理解的话是否可以留下邮箱我把整个工程发过去.很小的一个工程.拖很久了,麻烦帮帮忙..

Private Sub PrintSell(ByVal blnPrevious As Boolean)

Dim tmpRpt As New rptSellOut
Dim bk

' '>*>增加:保存当前打印机纸张<
Dim iCnt As Long ' 控件数组下标索引
Dim tmpPrintinf As PrintInf
Dim SizeNeeded As Long, buffer() As Long
Dim pDef As PRINTER_DEFAULTS
Dim X As DEVMODE
Dim lret As Long
Dim mhPrinter As Long
Dim intTempOri As Integer
Dim intTemp As Integer

pDef.DesiredAccess = 0
lret = OpenPrinter(Printer.DeviceName, mhPrinter, pDef)
ReDim Preserve buffer(0 To 0)
'获取SizeNeeded所需的大小
lret = GetPrinter(mhPrinter, 2, buffer(0), 0, SizeNeeded)
ReDim Preserve buffer(0 To (SizeNeeded / 4) + 3) As Long
'得到当前打印机信息
lret = GetPrinter(mhPrinter, 2, buffer(0), UBound(buffer) * 4, SizeNeeded)
'自定义纸张
Call CopyMemory(X, ByVal buffer(7), Len(X))
ClosePrinter mhPrinter

intTemp = intPap
For iCnt = 1 To UBound(PapInf)
If PapInf(iCnt).PapSize = X.dmPaperSize Then
intPap = iCnt
Exit For
End If
Next iCnt

tmpPrintinf.PapWid = PapInf(intPap).PapWid
tmpPrintinf.PapHei = PapInf(intPap).PapHei
tmpPrintinf.PapSize = X.dmPaperSize
tmpPrintinf.PapName = PapInf(intPap).PapName
intTempOri = X.dmOrientation
intPap = intTemp
' >*>增加:保存当前打印机纸张<
'
' >*>增加:设置报表的纸张类型<

For iCnt = 1 To UBound(PapInf)
If PapInf(iCnt).PapSize = gintPaperSize Then Exit For
Next iCnt
If iCnt <= UBound(PapInf) Then
Call SetPrinterPaper(PapInf(iCnt).PapName, gintPrintOrientation, gintPaperSize)
Else
MousePointer = vbDefault
MsgBox "没有该报表所需的纸张类型,请添加!", vbOKOnly, "消息!"
Exit Sub
End If

iCnt = SelectForm("海舜", Me.hwnd)

'>*>增加:设置报表的纸张类型<

With tmpRpt
.TopMargin = 500
.LeftMargin = 1005
.RightMargin = 0
.BottomMargin = 500

Set .DataSource = rsSub

With .Sections("secDetail")
.Controls("商品编号").DataField = "商品编号"
.Controls("商品名称").DataField = "商品名称"
.Controls("商品品牌").DataField = "商品品牌"
.Controls("单位").DataField = "单位"
.Controls("数量").DataField = "数量"
.Controls("单价").DataField = "单价"
.Controls("单价").Alignment = rptJustifyRight
.Controls("金额").DataField = "金额"
.Controls("金额").Alignment = rptJustifyRight
.Controls("备注").DataField = "备注"
End With

With .Sections("secPageTitle")
.Controls("送货单号").Caption = IIf(IsNull(rsMain.Fields("送货单号").Value), "", rsMain.Fields("送货单号").Value)
.Controls("送货日期").Caption = IIf(IsNull(rsMain.Fields("送货日期").Value), "", rsMain.Fields("送货日期").Value)
.Controls("客户名称").Caption = IIf(IsNull(rsMain.Fields("客户名称").Value), "", rsMain.Fields("客户名称").Value)
.Controls("地址").Caption = IIf(IsNull(rsMain.Fields("地址").Value), "", rsMain.Fields("地址").Value)
.Controls("联系人").Caption = IIf(IsNull(rsMain.Fields("联系人").Value), "", rsMain.Fields("联系人").Value)
.Controls("联系电话").Caption = IIf(IsNull(rsMain.Fields("联系电话").Value), "", rsMain.Fields("联系电话").Value)
End With

With .Sections("secFoot")
' .Controls("发票类型").Caption = IIf(IsNull(rsMain.Fields("发票类型").Value), "", rsMain.Fields("发票类型").Value)
.Controls("销售金额大写").Caption = rsMain.Fields("销售金额大写").Value
.Controls("销售金额").Caption = "¥" & Format(rsMain.Fields("销售金额").Value, "0.00")
.Controls("销售金额").Alignment = rptJustifyRight
.Controls("制单人").Caption = rsMain.Fields("制单人").Value
.Controls("主表备注").Caption = IIf(IsNull(rsMain.Fields("备注").Value), "", rsMain.Fields("备注").Value)
End With

If blnPrevious Then
.Show
Else
.Refresh
.PrintReport
Unload tmpRpt
End If

bk = rsMain.Bookmark
rsMain.Requery
Call SetgrdMain
rsMain.UpdateBatch
rsMain.Bookmark = bk
Set tmpRpt = Nothing
End With

'>*>增加:还原打印机纸张类型<
With tmpPrintinf
If Not (Printer.PaperSize = .PapSize And Printer.Orientation = intTempOri) Then Call SetPrinterPaper(.PapName, intTempOri, .PapSize)
End With
'>*>增加:还原打印机纸张类型<

End Sub

Public Sub SetPrinterPaper(ByVal strPaperName As String, ByVal newOrientation As OrientationConstants, ByVal newPaperSize As Long)
Dim bDevMode() As Byte, bPrinterInfo2() As Long
Dim dm As DEVMODE, pd As PRINTER_DEFAULTS
Dim nSize As Long, hPrinter As Long, lResult As Long
Dim pi2 As PRINTER_INFO_2

Call OpenPrinter(Printer.DeviceName, hPrinter, pd)
pd.DesiredAccess = (&HF0000 Or &H4& Or &H8&)
' 得到所需缓存大小
ReDim bPrinterInfo2(0 To 0) As Long
Call GetPrinter(hPrinter, 2, bPrinterInfo2(0), 0&, nSize)
ReDim bPrinterInfo2(0 To (nSize / 4) + 3) As Long
' 返回指定打印机的信息
lResult = GetPrinter(hPrinter, 2, bPrinterInfo2(0), UBound(bPrinterInfo2) * 4, nSize)
DoEvents
Call CopyMemory(dm, ByVal bPrinterInfo2(7), Len(dm))
With dm
.dmFormName = strPaperName & vbNullChar
.dmOrientation = newOrientation
.dmPaperSize = newPaperSize
.dmFields = &H10000 Or 2
End With
Call CopyMemory(ByVal bPrinterInfo2(7), dm, Len(dm))
' 修改打印机初始化信息
lResult = SetPrinter(hPrinter, 2, bPrinterInfo2(0), 0&)
ClosePrinter (hPrinter)

End Sub
...全文
148 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
饮水需思源 2008-05-24
  • 打赏
  • 举报
回复
把上面三段代码保存到一个模块中,在打印语句中加上下面的设置:

'*******************
Dim FormName As String, RetValue As Integer
'自定义打印纸张
FormName = "NBUF_DTV"
RetValue = UseForm(FormName, MyhWnd, 打印页面宽度值,打印面值高度值)
If RetValue = 0 Then
MsgBox "设置自定义打印格式错误,请检查自定义打印格式!", vbCritical, "提示"
Exit Sub
End If
'********************
饮水需思源 2008-05-24
  • 打赏
  • 举报
回复
'---承上页

Public Function GetFormName(ByVal PrinterHandle As Long, _
FormSize As SIZEL, FormName As String) As Integer
Dim NumForms As Long, i As Long
Dim FI1 As FORM_INFO_1
Dim aFI1() As FORM_INFO_1 ' Working FI1 array
Dim temp() As Byte ' Temp FI1 array
Dim FormIndex As Integer
Dim BytesNeeded As Long
Dim RetVal As Long

FormName = vbNullString
FormIndex = 0
ReDim aFI1(1)
' First call retrieves the BytesNeeded.
RetVal = EnumForms(PrinterHandle, 1, aFI1(0), 0&, BytesNeeded, NumForms)
ReDim temp(BytesNeeded)
ReDim aFI1(BytesNeeded / Len(FI1))
' Second call actually enumerates the supported forms.
RetVal = EnumForms(PrinterHandle, 1, temp(0), BytesNeeded, BytesNeeded, _
NumForms)
Call CopyMemory(aFI1(0), temp(0), BytesNeeded)
For i = 0 To NumForms - 1
With aFI1(i)
If .Size.cx = FormSize.cx And .Size.cy = FormSize.cy Then
' Found the desired form
FormName = PtrCtoVbString(.pName)
FormIndex = i + 1
Exit For
End If
End With
Next i
GetFormName = FormIndex ' Returns non-zero when form is found.
End Function

Public Function AddNewForm(PrinterHandle As Long, FormSize As SIZEL, _
FormName As String) As String
Dim FI1 As sFORM_INFO_1
Dim aFI1() As Byte
Dim RetVal As Long

With FI1
.flags = 0
.pName = FormName
With .Size
.cx = FormSize.cx
.cy = FormSize.cy
End With
With .ImageableArea
.Left = 0
.Top = 0
.Right = FI1.Size.cx
.Bottom = FI1.Size.cy
End With
End With
ReDim aFI1(Len(FI1))
Call CopyMemory(aFI1(0), FI1, Len(FI1))
RetVal = AddForm(PrinterHandle, 1, aFI1(0))
If RetVal = 0 Then
If Err.LastDllError = 5 Then
MsgBox "You do not have permissions to add a form to " & _
Printer.DeviceName, vbExclamation, "Access Denied!"
Else
MsgBox "Error: " & Err.LastDllError, , "Error Adding Form"
'MsgBox "Error: " & Err.LastDllError, "Error Adding Form"
End If
AddNewForm = "none"
Else
AddNewForm = FI1.pName
End If
End Function

Public Sub PrintTest()
' Print two test pages to confirm the page size.
Printer.Print "Top of Page 1."
Printer.NewPage
' Spacing between lines should reflect the chosen page height.
Printer.Print "Top of Page 2. - Check the page Height (Length.)"
Printer.EndDoc
MsgBox "Check Printer " & Printer.DeviceName, vbInformation, "Done!"
End Sub
Public Function PtrCtoVbString(ByVal Add As Long) As String
Dim sTemp As String * 512, x As Long

x = lstrcpy(sTemp, ByVal Add)
If (InStr(1, sTemp, Chr(0)) = 0) Then
PtrCtoVbString = ""
Else
PtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr(0)) - 1)
End If
End Function
Public Function IsNtOs() As Boolean
Dim ver As OSVERSIONINFO

ver.dwOSVersionInfoSize = 148
GetVersionEx ver
With ver
Select Case .dwPlatformId
Case 0, 1
IsNtOs = False
Case 2
IsNtOs = True
End Select
End With
End Function
Public Function UseForm(FormName As String, sHwnd As Long, dblPageWidth As Double, dblPageHeight As Double) As Integer
Dim RetVal As Integer
Dim FormSize As SIZEL ' Size of desired form

RetVal = SelectForm(FormName, sHwnd, dblPageWidth, dblPageHeight) '选择纸张类型
UseForm = RetVal
Select Case RetVal
Case FORM_NOT_SELECTED ' 0
' 选择纸张失败!
'MsgBox "Unable to retrieve From name", vbExclamation, _
'"Operation halted!"
Case FORM_SELECTED ' 1
' 选择成功!
'PrintTest ' Comment this line to avoid printing
Case FORM_ADDED ' 2
' 添加并选择成功.
'PrintTest ' Comment this line to avoid printing
End Select
End Function

'*************************************************************************
'**函 数 名:DelForm
'**输 入:FormName(String) - 格式名称
'**输 出:(Long) - 0 删除成功 1 删除失败
'**功能描述:
'**全局变量:
'**调用模块:
'**作 者:
'**日 期:
'**修 改 人:
'**日 期:
'**版 本:
'*************************************************************************
Public Function DelForm(FormName As String) As Long
Dim RetVal As Long
Dim PrinterHandle As Long
Dim PrinterName As String
Dim Continue As Long

'当前打印机
PrinterName = Printer.DeviceName
DelForm = 1
If OpenPrinter(PrinterName, PrinterHandle, 0&) Then
RetVal = DeleteForm(PrinterHandle, FormName & Chr(0))
If RetVal <> 0 Then
DelForm = 0 '删除成功
Else
DelForm = 1 '删除失败
End If
ClosePrinter (PrinterHandle)
End If
End Function
饮水需思源 2008-05-24
  • 打赏
  • 举报
回复
'---承上页

Public Function SelectForm(FormName As String, ByVal MyhWnd As Long, dblPageWidth As Double, dblPageHeight As Double) _
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
Dim tmpFormName As String
Dim isNT As Boolean
Dim PaperWidth As Long, PaperHeight As Long


'在这里统一添加自定义纸张
Select Case Trim(FormName)
Case "NBUF_DTV"
With FormSize ' Desired page size
.cx = dblPageWidth * 1000
.cy = dblPageHeight * 1000
'.cx = 420
'.cy = 97
'.cx = 210000
'.cy = 249000
End With
'PaperWidth = 11907 '21cm
'PaperHeight = 14175 '25cm
PaperWidth = 17577 '31cm
PaperHeight = 53298 '9.4cm
End Select

'不是NT直接给Printer赋值得了
If Not IsNtOs Then
Printer.Width = PaperWidth
Printer.Height = PaperHeight
SelectForm = FORM_SELECTED
Exit Function
End If


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 Public (predefined) portion of the DEVMODE.
Call CopyMemory(pDevMode, aDevMode(1), Len(pDevMode))

' If FormName is "MyCustomForm", we must make sure it exists
' before using it. Otherwise, it came from our EnumForms list,
' and we do not need to check first. Note that we could have
' passed in a Flag instead of checking for a literal name.

tmpFormName = FormName

If GetFormName(PrinterHandle, FormSize, tmpFormName) = 0 Then
' Form not found - Either of the next 2 lines will work.
'FormName = AddNewForm(PrinterHandle, FormSize, "MyCustomForm")
AddNewForm PrinterHandle, FormSize, FormName
If GetFormName(PrinterHandle, FormSize, FormName) = 0 Then
ClosePrinter (PrinterHandle)
SelectForm = FORM_NOT_SELECTED ' Selection Failed!
Exit Function
Else
SelectForm = FORM_ADDED ' Form Added, Selection succeeded!
End If
End If

' 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


'---接下页
饮水需思源 2008-05-24
  • 打赏
  • 举报
回复
Option Explicit

Public Declare Function EnumForms Lib "winspool.drv" Alias "EnumFormsA" _
(ByVal hPrinter As Long, ByVal Level As Long, ByRef pForm As Any, _
ByVal cbBuf As Long, ByRef pcbNeeded As Long, _
ByRef pcReturned As Long) As Long

Public Declare Function AddForm Lib "winspool.drv" Alias "AddFormA" _
(ByVal hPrinter As Long, ByVal Level As Long, pForm As Byte) As Long

Public Declare Function DeleteForm Lib "winspool.drv" Alias "DeleteFormA" _
(ByVal hPrinter As Long, ByVal pFormName As String) As Long

Public Declare Function OpenPrinter Lib "winspool.drv" _
Alias "OpenPrinterA" (ByVal pPrinterName As String, _
phPrinter As Long, ByVal pDefault As Long) As Long

Public Declare Function ClosePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long

Public Declare Function DocumentProperties Lib "winspool.drv" _
Alias "DocumentPropertiesA" (ByVal hwnd As Long, _
ByVal hPrinter As Long, ByVal pDeviceName As String, _
pDevModeOutput As Any, pDevModeInput As Any, ByVal fMode As Long) _
As Long

Public Declare Function ResetDC Lib "gdi32" Alias "ResetDCA" _
(ByVal hDC As Long, lpInitData As Any) As Long

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
(ByVal lpString1 As String, ByRef lpString2 As Long) As Long

' Optional functions not used in this sample, but may be useful.
Public Declare Function GetForm Lib "winspool.drv" Alias "GetFormA" _
(ByVal hPrinter As Long, ByVal pFormName As String, _
ByVal Level As Long, pForm As Byte, ByVal cbBuf As Long, _
pcbNeeded As Long) As Long

Public Declare Function SetForm Lib "winspool.drv" Alias "SetFormA" _
(ByVal hPrinter As Long, ByVal pFormName As String, _
ByVal Level As Long, pForm As Byte) As Long

' Constants for DEVMODE
Public Const CCHFORMNAME = 32
Public Const CCHDEVICENAME = 32
Public Const DM_FORMNAME As Long = &H10000
Public Const DM_ORIENTATION = &H1&

' Constants for PRINTER_DEFAULTS.DesiredAccess
Public Const PRINTER_ACCESS_ADMINISTER = &H4
Public Const PRINTER_ACCESS_USE = &H8
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)

' Constants for DocumentProperties() call
Public Const DM_MODIFY = 8
Public Const DM_IN_BUFFER = DM_MODIFY
Public Const DM_COPY = 2
Public Const DM_OUT_BUFFER = DM_COPY

' Custom constants for this sample's SelectForm function
Public Const FORM_NOT_SELECTED = 0
Public Const FORM_SELECTED = 1
Public Const FORM_ADDED = 2

Public Type RECTL
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Public Type SIZEL
cx As Long
cy As Long
End Type

Public Type SECURITY_DESCRIPTOR
Revision As Byte
Sbz1 As Byte
Control As Long
Owner As Long
Group As Long
Sacl As Long ' ACL
Dacl As Long ' ACL
End Type

' The two definitions for FORM_INFO_1 make the coding easier.
Public Type FORM_INFO_1
flags As Long
pName As Long ' String
Size As SIZEL
ImageableArea As RECTL
End Type

Public Type sFORM_INFO_1
flags As Long
pName As String
Size As SIZEL
ImageableArea As RECTL
End Type

Public Type DEVMODE
dmDeviceName As String * CCHDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCHFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Long
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type

Public Type PRINTER_DEFAULTS
pDatatype As String
pDevMode As Long ' DEVMODE
DesiredAccess As Long
End Type

Public Type PRINTER_INFO_2
pServerName As String
pPrinterName As String
pShareName As String
pPortName As String
pDriverName As String
pComment As String
pLocation As String
pDevMode As DEVMODE
pSepFile As String
pPrintProcessor As String
pDatatype As String
pParameters As String
pSecurityDescriptor As SECURITY_DESCRIPTOR
Attributes As Long
Priority As Long
DefaultPriority As Long
StartTime As Long
UntilTime As Long
Status As Long
cJobs As Long
AveragePPM As Long
End Type
'判断系统是否为NT系统
Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long

Public Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance string for PSS usage
End Type


'---接下页

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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