TUTO Créer un aperçu avant impression

Informations pour bien débuter en PureBasic
Avatar de l’utilisateur
microdevweb
Messages : 1798
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

TUTO Créer un aperçu avant impression

Message 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...
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Avatar de l’utilisateur
microdevweb
Messages : 1798
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

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

Message 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
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Avatar de l’utilisateur
Micoute
Messages : 2522
Inscription : dim. 02/oct./2011 16:17
Localisation : 35520 La Mézière

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

Message par Micoute »

Merci microdevweb pour ce partage, du beau travail comme d'habitude.
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce RTX 3080 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 5.73 PB 6.00 LTS (x64)
Un homme doit être poli, mais il doit aussi être libre !
Répondre