转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
完美支持98、2000系统自定义打印纸张的VB代码         ★★★★

完美支持98、2000系统自定义打印纸张的VB代码

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2801 更新时间:2009/4/23 15:04:10
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]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台