Page 1 sur 1

TUTO Créer un aperçu avant impression

Publié : mer. 12/juil./2017 9:15
par microdevweb
L'impression avec PureBasic est devenue très simple depuis la librairie vector, et la création d'un aperçu avant impression l'est tout autant.

Image

Voici le code pour l'apperçu (de l'exemple complet sur la page suivante)

Code : Tout sélectionner

Procedure Draw()
  ; Choix de l'unité en mm
  StartVectorDrawing(CanvasVectorOutput(#Canvas,#PB_Unit_Millimeter))
  ; Gestion du zoom
  ScaleCoordinates(ZoomFactor,ZoomFactor,#PB_Coordinate_User)
  ; Efface le canvas
  VectorSourceColor($FFFFFFFF)
  FillVectorOutput()
  DrawMyAdresse()
  DrawCustomerAdresse()
  DrawTop()
  StopVectorDrawing()
EndProcedure
Et le code d'impression

Code : Tout sélectionner

Procedure EventPrint()
  If PrintRequester()
    StartPrinting("Impression")
    StartVectorDrawing(PrinterVectorOutput(#PB_Unit_Millimeter))
    DrawMyAdresse()
    DrawCustomerAdresse()
    DrawTop()
    StopVectorDrawing()
    StopPrinting()
  EndIf
EndProcedure
Vous voyer qu'il n'y pas de grandes différences. Sur le post suivant vous trouverer le code complet avec redimensionnement du canvas en fonction du facteur de zoom etc...

Re: TUTO Créer un aperçu avant impression

Publié : mer. 12/juil./2017 9:16
par microdevweb
code complet

Code : Tout sélectionner

; *********************************************************************************
; Nom     : TUTO aperçu avant impression
; Autheur : MicrodevWeb
; *********************************************************************************
EnableExplicit

Global ZoomFactor.f=1,PageWidth=210,PageHeight=297

; Chargement de police pour l'impression
LoadFont(0,"Arial",11,#PB_Font_HighQuality)
Runtime Enumeration 
  #Form
  #Area
  #CbZoom
  #Canvas
  #BtPrint
  #BtExit
EndEnumeration
Procedure Exit()
  CloseWindow(EventWindow())
  End
EndProcedure
Procedure DrawMyAdresse()
  Protected text.s="MicrodevWeb"+Chr(10)+
                 "Rue Sainry 140"+Chr(10)+
                 "4870 Trooz"+Chr(10)+
                 "(BELGIQUE)"+Chr(10)
  Protected x=10,y=10
  VectorSourceColor($FF000000)
  VectorFont(FontID(0))
  MovePathCursor(x,y)
  DrawVectorParagraph(text,50,30)
EndProcedure
Procedure DrawTop()
  Protected X=10,Y=80,w=190,h=10,yt,xt
  Protected Text.s=" Date : 10-07-217      Facture N°: 20170058"
  VectorSourceColor($FF000000)
  VectorFont(FontID(0))
  yt=y+((h/2)-(VectorTextHeight(Text)/2))
  MovePathCursor(x,yt)
  DrawVectorText(Text)
  AddPathBox(x,y,w,h)
  StrokePath(0.4)
EndProcedure
Procedure DrawCustomerAdresse()
  Protected text.s="Mr Dupond"+Chr(10)+
                 "Rue de la Fourche 200"+Chr(10)+
                 "75000 Paris"+Chr(10)+
                 "(France)"+Chr(10)
  Protected w=50,h=30,x=210-w,y=40
  VectorSourceColor($FF000000)
  VectorFont(FontID(0))
  MovePathCursor(x,y)
  DrawVectorParagraph(text,w,h)
EndProcedure
Procedure Draw()
  ; Choix de l'unité en mm
  StartVectorDrawing(CanvasVectorOutput(#Canvas,#PB_Unit_Millimeter))
  ; Gestion du zoom
  ScaleCoordinates(ZoomFactor,ZoomFactor,#PB_Coordinate_User)
  ; Efface le canvas
  VectorSourceColor($FFFFFFFF)
  FillVectorOutput()
  DrawMyAdresse()
  DrawCustomerAdresse()
  DrawTop()
  StopVectorDrawing()
EndProcedure
Procedure EventPrint()
  If PrintRequester()
    StartPrinting("Impression")
    StartVectorDrawing(PrinterVectorOutput(#PB_Unit_Millimeter))
    DrawMyAdresse()
    DrawCustomerAdresse()
    DrawTop()
    StopVectorDrawing()
    StopPrinting()
  EndIf
EndProcedure
Procedure FillCbZoom()
  Protected i,z=25
  For i=0 To 11
    AddGadgetItem(#CbZoom,-1,Str(z)+" %")
    SetGadgetItemData(#CbZoom,i,z)
    z+25
  Next
  SetGadgetState(#CbZoom,3)
EndProcedure
Procedure ResizeCanvas()
    Protected W,H,X,Y,cw,ch
    ; Calcul de la taille en pixel du canvas
    ; selon les dimention A4 210 x 297
    StartVectorDrawing(CanvasVectorOutput(#Canvas,#PB_Unit_Millimeter))
    ScaleCoordinates(ZoomFactor,ZoomFactor,#PB_Coordinate_User)
    cW=ConvertCoordinateX(PageWidth,0,#PB_Coordinate_User,#PB_Coordinate_Device)
    cH=ConvertCoordinateY(0,PageHeight,#PB_Coordinate_User,#PB_Coordinate_Device)
    StopVectorDrawing()
    ; Centre horizontalement le canvas et la règle si plus petit que la zone de dessin
    If (cw)<GadgetWidth(#Area)-50
      SetGadgetAttribute(#Area,#PB_ScrollArea_InnerWidth,GadgetWidth(#Area)-50)
      x=(GadgetWidth(#Area)/2)-(cw/2)
    Else
      SetGadgetAttribute(#Area,#PB_ScrollArea_InnerWidth,cw+50)
      x=0
    EndIf
    ; Centre verticalement le canvas et la règle si plus petit que la zone de dessin
    If ch<GadgetHeight(#Area)-50
      SetGadgetAttribute(#Area,#PB_ScrollArea_InnerHeight,GadgetHeight(#Area)-50)
      Y=(GadgetHeight(#Area)/2)-(ch/2)
    Else
      SetGadgetAttribute(#Area,#PB_ScrollArea_InnerHeight,ch+50)
      Y=0
    EndIf
    ; Repositionne et redimentionne le canvas de dessin
    ResizeGadget(#Canvas,X,Y,cW,cH)
    Draw()
  EndProcedure
  Procedure EventZomm()
    Protected z=GetGadgetItemData(#CbZoom,GetGadgetState(#CbZoom))
    ZoomFactor=z/100
    ResizeCanvas()
  EndProcedure
Procedure OpenForm()
  Protected xml.s
  ; Xml pour l'ouverture de la fenêtre
  xml="<window name='main' id='#Form' "+
      " width='800'"+
      " height='600'"+
      " text='Aperçu avant impression'"+
      " flags='#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget|#PB_Window_MaximizeGadget'>"+
      "<vbox expand='item:2'>"+
      "   <hbox expand='no'>"+
      "     <frame text='Zoom' width='80'>"+
      "       <combobox id='#CbZoom'/>"+
      "     </frame>"+
      "     <button id='#BtPrint' text='Imprimer' width='100'/>"+
      "     <button id='#BtExit' text='Quitter' width='100'/>"+
      "   </hbox>"+
      "   <scrollarea id='#Area'>"+
      "   </scrollarea>"+
      "</vbox>"+
      "</window>"
  ; Chargement du xml
  CatchXML(0,@xml,StringByteLength(xml),0,#PB_UTF8)
  ; Création du dialog
  CreateDialog(0)
  ; Ouverture de la fenêtre
  OpenXMLDialog(0,0,"main")
  ; ajout du canvas n'est pas ajouté directement dans le xml parceque cela ne fonctionnerait pas bien
  OpenGadgetList(#Area)
  CanvasGadget(#Canvas,0,0,100,100)
  CloseGadgetList()
  ; Redimentionnement du canvas en fonction de la taille et du zoom
  ResizeCanvas()
  ; Remplissage du comboibox zoom
  FillCbZoom()
  ; Mise en place des callback
  BindEvent(#PB_Event_CloseWindow,@Exit(),#Form)
  BindGadgetEvent(#BtExit,@Exit())
  BindGadgetEvent(#CbZoom,@EventZomm())
  BindGadgetEvent(#BtPrint,@EventPrint())
  BindEvent(#PB_Event_SizeWindow,@ResizeCanvas())
EndProcedure

OpenForm()

Repeat 
  WaitWindowEvent()
ForEver

Re: TUTO Créer un aperçu avant impression

Publié : mer. 12/juil./2017 14:05
par Micoute
Merci microdevweb pour ce partage, du beau travail comme d'habitude.