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

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

2.在 Visual Basic 中启动一个新的标准 EXE 工程。默认情况下将创建 Form1。3.在 Form1 上添加三个命令按钮和一个列表框。s4.将以下代码粘贴到 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 Decla

本文关键:如何在 Windows NT 和 Windows 2000 中使用自定义页面大小打印
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top