打印本文 打印本文 关闭窗口 关闭窗口
完美支持98、2000系统自定义打印纸张的VB代码
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2800  更新时间:2009/4/23 15:04:10  文章录入:mintao  责任编辑:mintao
ndle 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 FormName
    Case "广东省发票"
      With FormSize   '''' Desired page size
        .cx = 186000
        .cy = 102000
      End With
      PaperWidth = 10544 ''''15.6cm
      PaperHeight = 5784 ''''10.2cm
    Case "80列报表"
      With FormSize   '''' Desired page size
        .cx = 210000
        .cy = 280000
      End With
      PaperWidth = 11904 ''''21cm
      PaperHeight = 15872 ''''28cm
    Case "40列报表"
      With FormSize   '''' Desired page size
        .cx = 210000
        .cy = 140000
      End With
      PaperWidth = 11904 ''''21cm
      PaperHeight = 7936 ''''14cm
    Case "20列报表"
      With FormSize   '''' Desired page size
        .cx = 210000
        .cy = 70000
      End With
      PaperWidth = 11904 ''''21cm
      PaperHeight = 4008 ''''7cm
  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

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
       &nb

上一页  [1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口