Author Topic: 儲存及載入 PrinterSetup.setupstring 的方法  (Read 3886 times)

admin

  • Administrator
  • *****
  • Posts: 0
    • View Profile
儲存及載入 PrinterSetup.setupstring 的方法
« on: August 09, 2012, 12:17:38 AM »
筆者曾經說過一個RealBasic 在Windows平檯下非常嚴重問題, 就是關於列印時選擇印表機, 在Realbasic 開發的軟件下, 每次列印必須開啟PageSetupDialog 設定相關列印設定, 才能生成Graphics 物件, 如需省略選擇印表機的部份, 必須預先儲存之前已設定好的SetupString, 然後留給以後直接列印時使用, 在Mac 的環境下, 儲存這個SetupString 並沒有什麼大問題, 但在Windows 環境下可能因為內部編碼的分別, 當直接載入已儲存的SetupString 便會立即當掉Realbasic 的軟件, 問了很多網友及Realbasic 的支援人員也沒有答覆, 花了兩年時間找答案, 到了最後還是自己解決問題 !

http://forums.realsoftware.com/viewtopic.php?f=1&t=35483

儲存已設定好的頁面設定,印表機選項等相關選項, 留待下次列印時, 不用重新設定
Code: [Select]
  Dim f as FolderItem
  Dim stream as BinaryStream
  Dim ps As New PrinterSetup
  dim enc as TextEncoding
  dim base, varnt, form,strLength as int64
  dim g as graphics
  dim tmpSetupString as  string
 
  If ps.PageSetupDialog then
    tmpSetupString=ps.SetupString
    enc=tmpSetupString.Encoding
    if enc = nil then
      enc = GetTextEncoding(0, 0, 0)
    end if
   
    base = enc.base
    varnt = enc.variant
    form = enc.format
    tmpSetupString=EncodeBase64(tmpSetupString,0)
    strLength = lenB(tmpSetupString)
   
    f=GetFolderItem("d:\printersetup.dat")
   
    If f<> Nil Then
      stream=BinaryStream.Create(f,True)
      stream.WriteInt64(base)
      stream.WriteInt64(varnt)
      stream.WriteInt64(form) 
      stream.WriteInt64(strLength)
      stream.Write tmpSetupString
      stream.Close
    End if
   
  End If

載入已儲存的 PrinterSetup.setupstring 資料
Code: [Select]
  Dim f as FolderItem
  Dim stream as BinaryStream
  Dim ps As New PrinterSetup
  dim enc as TextEncoding
  dim base, varnt, form,strLength as int64
  dim g as graphics
  dim tmpSetupString as  string
  f=GetFolderItem("d:\printersetup.dat")
 
  If f<> Nil Then
    stream=BinaryStream.Open(f, False)
    base = stream.ReadInt64
    varnt = stream.ReadInt64
    form = stream.ReadInt64
    strLength = stream.ReadInt64
   
    tmpSetupString = stream.read(strLength, encodings.UTF8)
    stream.close
    if tmpSetupString <> "" then
      enc = GetTextEncoding(base, varnt, form)
      tmpSetupString = DecodeBase64(tmpSetupString, enc)
    end if
   
    ps.SetupString=tmpSetupString
   
    g = OpenPrinter(ps)
   
    if g <> nil then
      // if the report runs successfully
     
      g.textSize = 72
     
      g.drawLine  0,0,g.width-1,0
      g.drawLine 0,0,0,g.height-1
      g.drawLine g.width-1,g.height-1,0,g.height-1
      g.drawLine g.width-1,g.height-1,g.width-1,0
     
      g.foreColor = rgb(150,150,150) //watermark-ish
     
      g.drawString "1",(g.width-g.stringWidth("1"))/2,(g.height/2)+(g.textHeight/2)
     
      //send this last page to the printer, move to the next
      g.nextPage
     
      //start our drawing routines again
      g.foreColor=rgb(0,0,0)//black again
     
      g.drawLine 0,0,g.width-1,0
      g.drawLine 0,0,0,g.height-1
      g.drawLine g.width-1,g.height-1,0,g.height-1
      g.drawLine g.width-1,g.height-1,g.width-1,0
     
      g.foreColor = rgb(150,150,150) //watermark-ish
      g.drawString "2",(g.width-g.stringWidth("2"))/2,(g.height/2)+(g.textHeight/2)
    End If
  End If
« Last Edit: August 09, 2012, 12:40:13 AM by Roy Chan »