【问题标题】:Printer Page Size Problem打印机页面尺寸问题
【发布时间】:2011-01-15 06:10:53
【问题描述】:

我正在尝试通过以下方式设置自定义纸张尺寸:

Printer.Height = 2160
Printer.Width = 11900

但它似乎没有任何效果。设置完成后,我要求提供该值并返回默认值。还有这个:

Printer.PaperSize = 256

返回错误...

有什么想法吗??

【问题讨论】:

    标签: vb6 size printing


    【解决方案1】:

    您的打印机不允许设置这些属性,或者您超出了它们的最大允许值。来自Visual Basic Reference

    如果您设置高度和宽度 打印机驱动程序的属性 不允许这些属性 设置,没有错误发生和大小 纸张保持原样。如果你 设置打印机的高度和宽度 仅允许某些值的驱动程序 要指定,不会发生错误,并且 该属性设置为 司机允许。例如,您可以 将高度设置为 150,驱动程序将 将其设置为 144。

    我不知道为什么将 Papersize 属性设置为 256 时会出现错误。它适用于我。此外,the documentation 指出,“设置打印机的高度或宽度属性会自动将 PaperSize 设置为 vbPRPSUser。”,等于 256。

    【讨论】:

      【解决方案2】:

      我实际上也遇到了同样的问题,但我只是碰巧找到了突破口。 首先,您需要创建一个自定义表单来定义您的自定义纸张尺寸。然后,你需要 请参阅 Windows API 以检查您刚刚创建的表单名称。你会得到 for 名字 从函数返回的数组中,并使用找到表单名称的数组索引。 最后用它作为printer.papersize的值

      示例如下:

      Public Type PRINTER_DEFAULTS
         pDatatype            As Long
         pDevMode             As Long
         DesiredAccess        As Long
      End Type
      
      Public Type FORM_INFO_1
              Flags As Long
              pName As Long   ' String
              Size As SIZEL
              ImageableArea As RECTL
      End Type
      
      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 Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _
         (pDest As Any, pSource As Any, ByVal cbLength As Long)
      Public Declare Sub Sleep Lib "KERNEL32" (ByVal dwMilliseconds As Long)
      
      Public Declare Function OpenPrinter Lib "winspool.drv" Alias _
         "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
         pDefault As PRINTER_DEFAULTS) As Long
      
      Public Declare Function ClosePrinter Lib "winspool.drv" _
         (ByVal hPrinter As Long) As Long
      
      Public Declare Function lstrcpy Lib "KERNEL32" Alias "lstrcpyA" _
          (ByVal lpString1 As String, ByRef lpString2 As Long) As Long
      
      'UDF
      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 IsFormExist(ByVal DeviceName As String, ByVal isFormName As String, ByVal PrinterHandle As Long) As Long
      Dim NumForms As Long, i As Long
      Dim FI1 As FORM_INFO_1
      Dim pd As PRINTER_DEFAULTS
      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
      
      On Error GoTo cleanup
      
      FormIndex = 0
      ReDim aFI1(1)
      ' First call retrieves the BytesNeeded.
      
      RetVal = OpenPrinter(DeviceName, PrinterHandle, pd)
        If (RetVal = 0) Or (PrinterHandle = 0) Then
        'Can't access current printer. Bail out doing nothing
        Exit Function
      End If
      
      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 isFormName = PtrCtoVbString(.pName) Then
                 ' Found the desired form
                  FormIndex = i + 1
                  Exit For
              End If
          End With
      Next i
      IsFormExist = FormIndex ' Returns the number when form is found.
      
      cleanup:
         'Release the printer handle
         If (PrinterHandle <> 0) Then Call ClosePrinter(PrinterHandle)
      End Function
      
      
      'Here We Go
      
      dim papercode as long, printername as string, formname as string
      printername=printer.Devicename 
      formname = "myform"   
      
      papercode=IsFormExist(printername, formname, Printer.hdc)
      
      if papercode<>0 then
        printer.papersize=papercode
      end if
      

      试试看,祝你好运

      【讨论】:

        【解决方案3】:

        您确定错误与打印机本身的最大打印宽度无关吗?许多打印机的最大打印宽度为 8.25" (11880),以便在 8.5" 宽的纸张的两侧留出 1/4" 的边距。

        最快的检查方法是将打印宽度设置为 11880 或更低,看看它是否有效。

        另一种可能性是对打印机的权限。如果它是共享网络资源,它可能会被锁定。

        【讨论】:

          【解决方案4】:

          解决办法是用windows 98。win2k不行,winXP也不行。相同的代码,相同的打印机。

          问候。

          【讨论】:

            【解决方案5】:

            我正在测试此代码,但在 Windows XP Professional SP3 的控制面板中看不到我使用打印机和扫描仪创建的自定义表单。 注意:我可以在 regedit 中检查该表单是否存在,其 ID 为 512 的字符串值,它包含在打印机控制面板中创建的表单的名称。

            为什么这个函数不返回我的自定义表单,我使用的是 HP Laserjet 1020。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-08-23
              • 2010-10-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多