如何在 Windows NT 和 Windows 2000 中使用自定义页面大小打印(VB)

如何在 Windows NT 和 Windows 2000 中使用自定义页面大小打印

定义或选择页面大小的 DEVMODE 成员
打印机将其默认设置存储在名为 DEVMODE 的结构中。定义或选择页面大小时,可能会涉及此结构的四个成员:

  • dmPaperSize
  • dmPaperLength
  • dmPaperWidth
  • dmFormName
dmPaperLengthdmPaperWidth 成员在所有系统上都包含值,但只能用于在运行 Windows 95、Windows 98 或 Windows Me 的系统上选择或定义大小。 dmFormName 成员仅在运行 Windows NT 或 Windows 2000 的系统上有效。 dmPaperSize 成员可在任何 32 位 Windows 操作系统上使用,只要分配给它的值对应于该系统上定义的大小或页形。

因此,在运行 Windows 95、Windows 98 或 Windows Me 系统上,可以用高度(长度)和宽度或预定义的纸张大小来定义自定义页面大小。基于 Windows NT 或 Windows 2000 的系统仅使用定义的页形来指定页面大小,您可以通过以下两种方式来进行选择:设置 dmFormName ,或将一个“常量”值赋给 dmPaperSize

若要在 Windows NT 或 Windows 2000 系统上使用特定的纸张大小,则必须为其定义一个页形。Visual Basic 中的 Printer 对象具有 PageSizeHeightWidth 属性,对应于 DEVMODE 的 dmPaperSizedmPaperLengthdmPaperWidth 成员。但是, Printer 对象未提供按名称选择页形的方式,而且也未提供在运行时添加自定义页形的方法。我们必须使用 Win32 应用程序编程接口 (API) 来创建自定义页形,和/或按名称选择页形。
如何通过编程方式选择页形
本文使用 EnumForms 基本进程来列出当前打印机支持的所有页形,并检查每个页形的高度和宽度。如果找到了指定的大小,将选择该大小。如果未找到指定的大小,代码将添加该页形,然后选择它。

有三种方法可以通过编程方式选择页形:
  • 检索打印机的 DEVMODE 结构,设置dmFormName 成员,并使用ResetDC 函数将此页形设置为打印机设备上下文 (DC) 的当前页形。选择此页形作为 DC 的当前页形时,它仅对调用进程有效,而不会更改驱动程序的默认设置。这不需要任何特殊权限。对于需要更改打印机设置的应用程序,建议使用此方法。
  • 调用SetForm 函数更改打印机驱动程序的默认页形。使用此方法时,需要具有对该驱动程序的完全访问权限。更改了默认页形后,将影响所有使用该驱动程序进行打印的应用程序。对于网络打印机,多数用户都没有执行此调用所必需的权限。
  • 将自定义页形的系统定义值赋给Printer.PaperSize
本文中的示例代码仅演示了第一种方法,而未演示调用 SetForm 的方法,或将值赋给 Printer.PaperSize 的方法。示例代码通过返回新的自定义页形在 EnumForms 返回的列表中的位置顺序来获取其系统定义值。请注意,此方法仅适用于自定义页形。分配给预定义页形的常量值与它们在该列表中的位置顺序不对应。

使用 AddForm 函数添加自定义页形时,将为该页形分配它自己的编号(如果可用)。此编号对于使用该页形的所有打印机将一直保持一致。这样,就在当前系统上为该页形定义并分配了一个“常量”值。之后,即可通过将该值赋给 Printer.PaperSize 来选择该页形。但是,由于分配的编号只是简单地在添加该页形时列出的当前打印机页形数上加一,因此该编号可能会因某个预定义页形已经使用了该编号而不可用。因此,不建议使用此值来选择页形,所以本文中未演示使用此值的方法。如果使用分配的编号,但该值不可用,则可能会选择错误的页形或引发运行时错误 380:“Invalid Property Value.”
页形来源和函数
多数页形都由操作系统定义,而且对所有本地打印机都可用。但网络打印机的页形在打印机服务器上定义。某些页形可能是专为特定打印机驱动程序定义的,因此只出现在该驱动程序的列表中。页形的另一个来源是自定义或用户定义的页形。这些页形可以手动创建,或通过代码创建,并且对系统上的所有本地打印机都可用。特定于打印机的页形和用户定义的页形都存储在注册表中。

本文中的示例使用了几个与页形相关的函数调用。下表简要地列出了主要的函数:

收起该表格 展开该表格
函数说明
EnumForms返回当前打印机支持的所有页形的信息。
AddForm在系统中添加一个自定义页形。该页形上的数据存储在注册表中。因此,自定义页形对所有打印机都可用,而不是仅对添加该页形时的当前打印机可用。系统分配给该页形的数字值只是简单地在EnumForms 返回的当前打印机页形数上加一。必须具有对该驱动程序的完全访问权限才能调用此函数。如果不能手动添加页形,此函数也会失败。因此,通常不能在网络打印机上使用此函数。
DeleteForm删除一个自定义页形。但是,此函数不会删除操作系统定义的标准页形。如果尝试删除标准页形,将返回错误代码 87。此函数需要的权限与AddForm 函数相同。


虽然示例中未使用以下函数,但却包含这些函数的声明:

收起该表格 展开该表格
函数说明
GetForm检索有关特定页形的信息。
SetForm设置指定打印机的默认页形信息。此函数需要完全访问权限,就像更改任何打印机驱动程序设置一样。


必须强调这些函数中有一些需要对打印机的完全控制权限。因此,对于安装为“网络打印机”的打印机, SetFormAddFormDeleteForm 函数几乎根本不可用。这是因为网络打印机的驱动程序未安装在本地,而是安装在打印机服务器上,而您的用户帐户不太可能在打印机服务器上具有 Administrator 权限。但在 Windows 95、Windows 98 或 Windows Me 系统上情况有所不同。在这些系统上,所有打印机驱动程序(甚至网络打印机)始终都安装在本地。然而,可以在 Windows NT 或 Windows 2000 上将网络打印机安装为本地打印机,并将“端口”设置为打印机队列。这样,您就可以使用这些函数来进行更改,而不会影响共享该打印机的其他用户。
为网络打印机添加本地打印机驱动程序的步骤
  1. 开始菜单上,指向设置,然后单击打印机。双击添加打印机 ,启动“添加打印机向导”。
  2. 向导提示时,一定要单击本地打印机 -或- 我的电脑 ,而不要选择网络打印机。虽然将连接到一台网络打印机,但要使用“我的电脑”上的驱动程序。单击下一步以继续。
  3. 在 Windows NT 上,单击添加端口。在 Windows 2000 上,单击创建新端口
  4. 在下拉列表框中,单击本地端口。在 Windows NT 上,单击新端口。在 Windows 2000 上,单击下一步,然后向导将提示您输入端口名
  5. 键入该打印机在网络上的位置。例如:
    \\printserver\printername
    (请用指向该打印机的实际路径名替换)。
  6. 单击确定,完成该向导。
如果您更改了活动打印机的打印机属性,仍会影响所有使用该打印机的应用程序,但仅限于本地系统上。

重要说明 使用自定义页面大小时,在激光或喷墨式打印机和点阵或撞击式打印机上的现象有所不同。很明显,没有打印机会接受比它实际能够容纳的纸张大小还要宽的页面大小。但使用连续送纸机制的打印机对页面大小的限制很灵活,因为这种打印机每次打印一行。而激光和喷墨打印机每次却打印一页。因此,在打印到点阵打印机(这种打印机使用连续送纸机制)时,很容易就可以在换页时看到页面长度,因为打印机会将纸张向前送动它认为下一页的顶端开始的位置,而不管所用纸张的实际大小有多大。在激光和喷墨打印机上,换页时通常会弹出一整页打印纸,而不管它所识别的页面大小有多大。此外,在多数激光打印机上,让打印机在用户定义的页面大小上打印时,显示屏上将显示“Load Custom(装入自定义大小纸张)”,或类似提示。
分步示例
  1. 将一台本地打印机设置为默认打印机。为此,请按照下列步骤操作:
    1. 开始 菜单上,指向设置,然后单击打印机
    2. 右键单击一台本地打印机的图标,然后单击设为默认值
  2. 在 Visual Basic 中启动一个新的标准 EXE 工程。默认情况下将创建 Form1。
  3. 在 Form1 上添加三个命令按钮和一个列表框。s
  4. 将以下代码粘贴到 Form1 的模块中:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim FormName As String
    
    FormName = "MyCustomForm"   ' Use special, user-defined form.
    UseForm FormName
    End Sub
    
    Private Sub Command2_Click()
    Dim FormName As String
    
    ' Get FormName from the ListBox.
    On Error GoTo ListBoxERR    ' Trap for no selection.
    FormName = Mid(List1.Text, 1, InStr(1, List1.Text, " -") - 1)
    On Error GoTo 0             ' Turn off Error trap.
    
    UseForm FormName
    
    Exit Sub
    ListBoxERR:
    MsgBox "Select a printer from the ListBox before using this option.", _
       vbExclamation
    End Sub
    
    Private Sub Command3_Click()
    Dim RetVal As Long
    Dim PrinterHandle As Long   ' Handle to printer
    Dim PrinterName As String
    Dim FormName As String
    Dim Continue As Long
    
    ' Delete form that is selected in ListBox.
    PrinterName = Printer.DeviceName  ' Current printer
    If OpenPrinter(PrinterName, PrinterHandle, 0&) Then
    
        On Error GoTo ListBoxERR    ' Trap for no selection.
        FormName = Mid(List1.Text, 1, InStr(1, List1.Text, " -") - 1)
        On Error GoTo 0             ' Turn off Error trap.
        
        Continue = MsgBox("Are you sure you want to permanently remove " & _
                   FormName & " from " & PrinterName & "?", vbYesNo)
        If Continue = vbYes Then
            RetVal = DeleteForm(PrinterHandle, FormName & Chr(0))
            If RetVal <> 0 Then ' DeleteForm succeeded.
                List1.Clear     ' Reflect the deletion in the ListBox.
                Form_Load       ' Rebuild the list.
                MsgBox FormName & " deleted!", vbInformation, "Success!"
            Else
                MsgBox FormName & " not deleted!" & vbCrLf & vbCrLf & _
                   "Error code: " & Err.LastDllError, vbInformation, "Failure!"
            End If
        End If
        ClosePrinter (PrinterHandle)
    End If
    
    Exit Sub
    ListBoxERR:
    MsgBox "Select a printer from the ListBox before using this option.", _
       vbExclamation
    ClosePrinter (PrinterHandle)
    End Sub
    
    Private Sub Form_Load()
    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 BytesNeeded As Long
    Dim PrinterName As String           ' Current printer
    Dim PrinterHandle As Long           ' Handle to printer
    Dim FormItem As String              ' For ListBox
    Dim RetVal As Long
    Dim FormSize As SIZEL               ' Size of desired form
    
    PrinterName = Printer.DeviceName    ' Current printer
    If OpenPrinter(PrinterName, PrinterHandle, 0&) Then
        With FormSize   ' Desired page size
            .cx = 214000
            .cy = 216000
        End With
        ReDim aFI1(1)
        RetVal = EnumForms(PrinterHandle, 1, aFI1(0), 0&, BytesNeeded, _
                 NumForms)
        ReDim Temp(BytesNeeded)
        ReDim aFI1(BytesNeeded / Len(FI1))
        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)
                ' List name and size including the count (index).
                FormItem = PtrCtoVbString(.pName) & " - " & .Size.cx / 1000 & _
                   " mm X " & .Size.cy / 1000 & " mm   (" & I + 1 & ")"
                List1.AddItem FormItem
            End With
        Next I
        ClosePrinter (PrinterHandle)
    End If
    End Sub
    
    Private Sub UseForm(FormName As String)
    Dim RetVal As Integer
    
    RetVal = SelectForm(FormName, Me.hwnd)
    Select Case RetVal
        Case FORM_NOT_SELECTED   ' 0
            ' Selection failed!
            MsgBox "Unable to retrieve From name", vbExclamation, _
               "Operation halted!"
        Case FORM_SELECTED   ' 1
            ' Selection succeeded!
            PrintTest     ' Comment this line to avoid printing
        Case FORM_ADDED   ' 2
            ' Form added and selected.
            List1.Clear   ' Reflect the addition in the ListBox
            Form_Load     ' by rebuilding the list.
    End Select
    End Sub
    					
  5. 工程 菜单上,添加一个新模块,Module1。
  6. 将下面的代码粘贴到 Module1 中:
    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
    
    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"
        End If
        AddNewForm = "none"
    Else
        AddNewForm = FI1.pName
    End If
    End Function
    
    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 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 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.
        If FormName = "MyCustomForm" Then
            ' Use form "MyCustomForm", adding it if necessary.
            ' Set the desired size of the form needed.
            With FormSize   ' Given in thousandths of millimeters
                .cx = 214000   ' width
                .cy = 216000   ' height
            End With
            If GetFormName(PrinterHandle, FormSize, FormName) = 0 Then
                ' Form not found - Either of the next 2 lines will work.
                'FormName = AddNewForm(PrinterHandle, FormSize, "MyCustomForm")
                AddNewForm PrinterHandle, FormSize, "MyCustomForm"
                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
        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
    
    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
    					
  7. 运行该工程。列表框中将显示当前打印机支持的所有页形。
  8. 单击Command1。这将在该列表的末尾添加“MyCustomForm - 214 mm X 216 mm (xxx)”。其中,“xxx”是分配给这个新页形的编号。
  9. 单击列表框中的一个页形,然后单击Command2。这将使用所选页形在当前打印机上打印一张测试页。
  10. 单击列表框中的新自定义页形,然后单击Command3。程序将提示您是否确定要删除该页形。如果单击,程序将删除该自定义页形。如果尝试删除预定义的页形,将引发错误 87,因为只有自定义页形可以删除。

转载于:https://www.cnblogs.com/baiefjg/archive/2009/06/14/1502830.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值