Page 1 sur 1

Comment puis-je imprimer horizontalement (Landscape)

Publié : dim. 11/févr./2007 20:14
par RV
Je voudrais imprimer en landscape sans avoir a passer par le PrintRequester.

PureBasic ne le gère pas mais peut-etre qu'un code API peut le faire.

Toute personne pouvant m'aider sera le bien venu.

Merci d'avance!

Publié : lun. 12/févr./2007 19:45
par Gillou
Salut,

Refonte de mon poste d'hier soir, connaissant pas ton niveau, j'ai completé les parties manquantes ;)

Sinon, un code génial de Xombie sur le forum anglais : http://www.purebasic.fr/english/viewtopic.php?t=22409
Structure Imprimante
    Nom.s
    Pilote.s
    Port.s
EndStructure

Global NewList Imprimante.Imprimante()

Procedure PrinterList()
    *Buffer= AllocateMemory (4096):Vide$= Space (1024)
     GetProfileString_ ( "devices" ,0, "" ,*Buffer,4096)
    LongChaine=0
     ClearList (Imprimante())
     Repeat
        Chaine$= PeekS (*Buffer+LongChaine)
        LongChaine=LongChaine+ Len (Chaine$) +1
         If Chaine$ <> ""
             AddElement (Imprimante())
            Imprimante()\Nom = Chaine$
             GetPrivateProfileString_ ( "devices" ,Chaine$, "" ,Vide$,1024, "Win.Ini" )
            Imprimante()\Pilote = StringField (Vide$,1, "," )
            Imprimante()\Port = StringField (Vide$,2, "," )
         EndIf
     Until Chaine$ = ""
     FreeMemory (*Buffer)
EndProcedure

Procedure PrintImage(Image, PrinterDC)
     ImageID = ImageID (Image)
    hdc = CreateCompatibleDC_ (0)
     If hdc
        bmi.BITMAPINFO
        bm.BITMAP
         GetObject_ ( ImageID (Image), SizeOf (BITMAP), @bm.BITMAP)
        bmi\bmiHeader\biSize = SizeOf (BITMAPINFOHEADER)
        bmi\bmiHeader\biWidth = bm\bmWidth
        bmi\bmiHeader\biHeight = bm\bmHeight
        bmi\bmiHeader\biPlanes = 1
        bmi\bmiHeader\biBitCount = 32
        bmi\bmiHeader\biCompression = #BI_RGB
        W = GetDeviceCaps_ (PrinterDC, #HORZRES )
        H = GetDeviceCaps_ (PrinterDC, #VERTRES )
        HList = AllocateMemory (bm\bmWidth * bm\bmHeight * 4)
         GetDIBits_ (hdc, ImageID (Image), 0, bm\bmHeight, HList, bmi, #DIB_RGB_COLORS )
         StretchDIBits_ (PrinterDC, 0, 0, W, H, 0, 0, bm\bmWidth-1, bm\bmHeight-1, HList, bmi, #DIB_RGB_COLORS , #SRCCOPY )
         FreeMemory (HList)
         ReleaseDC_ (0, hdc)
     EndIf
EndProcedure

PrinterList() ; De quoi remplir une listicon pour choisir l'imprimante : D
FirstElement (Imprimante())
PrintersName.s = Imprimante()\Nom
If OpenPrinter_ (PrintersName, @PrinterHandle.l, 0) ; Retourne 0 si non OK
    Buffersize.l = DocumentProperties_ (0, PrinterHandle, PrintersName, 0, 0, 0)
    *DevBufferIn = AllocateMemory (Buffersize)
    *DevBufferOut = AllocateMemory (Buffersize)
     DocumentProperties_ (0, PrinterHandle, PrintersName, *DevBufferIn, *DevBufferOut, #DM_OUT_BUFFER | #DM_IN_BUFFER )
    *PrinterParameters.DEVMODE = *DevBufferIn
     ClosePrinter_ (PrinterHandle)
    *PrinterParameters\dmOrientation = 2 ; Paysage
    lpszDriver.s = Imprimante()\Pilote
    PrinterDC.l = CreateDC_ (@lpszDriver, PrintersName, 0, *PrinterParameters)
     FreeMemory (*DevBufferIn)
     FreeMemory (*DevBufferOut)
     If PrinterDC <> 0 ; On peut imprimer
        
        W = GetDeviceCaps_ (PrinterDC, #HORZRES )
        H = GetDeviceCaps_ (PrinterDC, #VERTRES )
        
        Size = 2000 ; Taille max de l'image vu que PureBasic ne supporte pas les grandes images
        
         If W/H > H/W
            WImg = Size
            HImg = H*Size/W
         Else
            HImg = Size
            WImg = W*Size/H
         EndIf
        
        DocInf.DOCINFO
        DocInf\cbSize = SizeOf (DOCINFO)
        DocInf\lpszDocName = @ "Impression en paysage"
        DocInf\lpszOutput = #Null
        
        Image = CreateImage ( #PB_Any , WImg, HImg)
         If StartDoc_ (PrinterDC,@DocInf) > 0
             If StartPage_ (PrinterDC) > 0 ; PAGE 1
                     StartDrawing ( ImageOutput (Image)) ; on dessine ce que l'on a envie
                         Box (0, 0, WImg, HImg, $0000FF)
                         DrawText (20, 20, "PAGE 1" , 0, $0000FF)
                     StopDrawing ()
                    PrintImage(Image, PrinterDC)
                 EndPage_ (PrinterDC)
                 StartPage_ (PrinterDC) ; PAGE 2
                     StartDrawing ( ImageOutput (Image))
                         Box (0, 0, WImg, HImg, $FFFFFF)
                         Box (WImg/4, HImg/4, WImg/2, HImg/2, $0000FF)
                         Circle (500, 500, 400, $00FF00)
                         DrawText (20, 20, "PAGE 2" , $FF0000, $FFFFFF)
                     StopDrawing ()
                    PrintImage(Image, PrinterDC)
                 EndPage_ (PrinterDC)
             EndIf
             FreeImage (Image)
             EndDoc_ (PrinterDC)
             ReleaseDC_ (0, DC)
         EndIf
     EndIf
EndIf

Publié : sam. 17/févr./2007 13:57
par RV
Salut et merci Gillou pour avoir repondu.

J'ai essayé ton code mais lorsque je lance celui-ci, il m'ouvre l'assistant de télécopie et me demande de saisir un destinataire.

Pour info, j'ai bien un fax dans ma liste d'imprimantes mais il est dommage que ton code n'ouvre pas directement l'imprimante par défaut.

De plus pour corser un peu le probleme, je veux imprimer alors que je suis en OpenScreen() et donc je ne veux passer par aucun assistant d'impression pour eviter de revenir sous Windows car je trouve que le passage de l'un à l'autre est instable.

Publié : dim. 18/févr./2007 22:18
par Gillou
Voila le nouveau code qui utilise l'imprimante par défaut,

Pour ta seconde demande, si tu utilises une imprimante autre qu'un fax ou un générateur de fichier pdf, il ne devrait pas y avoir de fenêtre de dialogue lors de l'impression
Procedure.s GetDefaultPrinter()
    Name.s = Space ( #MAX_PATH )
     GetProfileString_ ( "Windows" , "Device" , "" , @Name, 255)
     ProcedureReturn Name
EndProcedure

Procedure PrintImage(Image, PrinterDC)
     ImageID = ImageID (Image)
    hdc = CreateCompatibleDC_ (0)
     If hdc
        bmi.BITMAPINFO
        bm.BITMAP
         GetObject_ ( ImageID (Image), SizeOf (BITMAP), @bm.BITMAP)
        bmi\bmiHeader\biSize = SizeOf (BITMAPINFOHEADER)
        bmi\bmiHeader\biWidth = bm\bmWidth
        bmi\bmiHeader\biHeight = bm\bmHeight
        bmi\bmiHeader\biPlanes = 1
        bmi\bmiHeader\biBitCount = 32
        bmi\bmiHeader\biCompression = #BI_RGB
        w = GetDeviceCaps_ (PrinterDC, #HORZRES )
        h = GetDeviceCaps_ (PrinterDC, #VERTRES )
        HList = AllocateMemory (bm\bmWidth * bm\bmHeight * 4)
         GetDIBits_ (hdc, ImageID (Image), 0, bm\bmHeight, HList, bmi, #DIB_RGB_COLORS )
         StretchDIBits_ (PrinterDC, 0, 0, w, h, 0, 0, bm\bmWidth-1, bm\bmHeight-1, HList, bmi, #DIB_RGB_COLORS , #SRCCOPY )
         FreeMemory (HList)
         ReleaseDC_ (0, hdc)
     EndIf
EndProcedure

Printer.s = GetDefaultPrinter()
PrintersName.s = StringField (Printer,1, "," )
If OpenPrinter_ (PrintersName, @PrinterHandle.l, 0) ; Retourne 0 si non OK
    BufferSize.l = DocumentProperties_ (0, PrinterHandle, PrintersName, 0, 0, 0)
    *DevBufferIn = AllocateMemory (BufferSize)
    *DevBufferOut = AllocateMemory (BufferSize)
     DocumentProperties_ (0, PrinterHandle, PrintersName, *DevBufferIn, *DevBufferOut, #DM_OUT_BUFFER | #DM_IN_BUFFER )
    *PrinterParameters.DEVMODE = *DevBufferIn
     ClosePrinter_ (PrinterHandle)
    *PrinterParameters\dmOrientation = 2 ; Paysage
    lpszPrinter.s = StringField (Printer,2, "," )
    PrinterDC.l = CreateDC_ (@lpszPrinter, PrintersName, 0, *PrinterParameters)
     FreeMemory (*DevBufferIn)
     FreeMemory (*DevBufferOut)
     If PrinterDC <> 0 ; On peut imprimer
        
        w = GetDeviceCaps_ (PrinterDC, #HORZRES )
        h = GetDeviceCaps_ (PrinterDC, #VERTRES )
        
        Size = 2000 ; Taille max de l'image vu que PureBasic ne supporte pas les grandes images
        
         If w/h > h/w
            WImg = Size
            HImg = h*Size/w
         Else
            HImg = Size
            WImg = w*Size/h
         EndIf
        
        DocInf.DOCINFO
        DocInf\cbSize = SizeOf (DOCINFO)
        DocInf\lpszDocName = @ "Impression en paysage"
        DocInf\lpszOutput = #Null
        
        Image = CreateImage ( #PB_Any , WImg, HImg)
         If StartDoc_ (PrinterDC,@DocInf) > 0
             If StartPage_ (PrinterDC) > 0 ; PAGE 1
                 StartDrawing ( ImageOutput (Image)) ; on dessine ce que l'on a envie
                 Box (0, 0, WImg, HImg, $0000FF)
                 DrawText (20, 20, "PAGE 1" , 0, $0000FF)
                 StopDrawing ()
                PrintImage(Image, PrinterDC)
                 EndPage_ (PrinterDC)
                 StartPage_ (PrinterDC) ; PAGE 2
                 StartDrawing ( ImageOutput (Image))
                 Box (0, 0, WImg, HImg, $FFFFFF)
                 Box (WImg/4, HImg/4, WImg/2, HImg/2, $0000FF)
                 Circle (500, 500, 400, $00FF00)
                 DrawText (20, 20, "PAGE 2" , $FF0000, $FFFFFF)
                 StopDrawing ()
                PrintImage(Image, PrinterDC)
                 EndPage_ (PrinterDC)
             EndIf
             FreeImage (Image)
             EndDoc_ (PrinterDC)
             ReleaseDC_ (0, DC)
         EndIf
     EndIf
EndIf

Publié : lun. 19/févr./2007 12:57
par RV
Salut Gillou,

Merci pour ton code car c'est exactement ce que je voulais.

A+