IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

[C#] Quelle méthode pour imprimer un form?


Sujet :

Windows Forms

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre très actif
    Profil pro
    Développeur Java
    Inscrit en
    Avril 2006
    Messages
    130
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Avril 2006
    Messages : 130
    Par défaut [C#] Quelle méthode pour imprimer un form?
    Bonjour,

    J'ai fais une recherche dans le forum mais je vois vraiment pas comment imprimer une fenetre de winform.. est-ce qu'il faut utiliser une dll, le namespace Printing, émuler la touche "Impr écran" ...?
    J'ai regardé sur les liens donnés dans les topics existants mais à chaque fois c'est pour imprimer un contrôle particulier du form, mais je voudrais imprimer la fenetre en entière.

    Merci d'avance (de me rediriger vers un autre topic au cas où) pour votre aide.

    Ou est-ce que qqun peut traduire ça en C# ? :p
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    Private Declare Function BitBlt Lib "gdi32.dll" Alias "BitBlt" (ByVal _
       hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As _
       Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal _
       hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, _
       ByVal dwRop As System.Int32) As Long
    Dim memoryImage As Bitmap
    Private Sub CaptureScreen()
       Dim mygraphics As Graphics = Me.CreateGraphics()
       Dim s As Size = Me.Size
       memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
       Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
       Dim dc1 As IntPtr = mygraphics.GetHdc
       Dim dc2 As IntPtr = memoryGraphics.GetHdc
       BitBlt(dc2, 0, 0, Me.ClientRectangle.Width, _
          Me.ClientRectangle.Height, dc1, 0, 0, 13369376)
       mygraphics.ReleaseHdc(dc1)
       memoryGraphics.ReleaseHdc(dc2)
    End Sub
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
       ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
       PrintDocument1.PrintPage
       e.Graphics.DrawImage(memoryImage, 0, 0)
    End Sub
    Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As _
       System.EventArgs) Handles PrintButton.Click
       CaptureScreen()
       PrintDocument1.Print()
    End Sub

  2. #2
    Membre très actif
    Profil pro
    Développeur Java
    Inscrit en
    Avril 2006
    Messages
    130
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Avril 2006
    Messages : 130
    Par défaut
    Finalement j'ai trouvé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
     
    using System.Runtime.InteropServices;
     
    ...
     
    private Image _memImage;
    ...
     
    #region gdi322.dll
            [DllImport("gdi32.dll")]
            private static extern bool BitBlt(
                IntPtr hdcDest, // handle to destination DC
                int xDest, // x coord of the dest upper left corner
                int yDest, // y coord of the dest upper left corner
                int width, // width of the destination rectangle
                int height, // height of the destination rectangle
                IntPtr hdcSrc, // handle to source DC
                int xSrc, // x coord of the source upper left corner
                int ySrc, // y coord of the source upper left corner
                Int32 dwRop // raster operation code
                );
            #endregion
     
    ...
     
            #region main menu imprimer
            private void toolStripMenuItem1_Click(object sender, EventArgs e)
            {
                // Rafraichit le Form pour ne pas capturer l'animation du menu
                this.Refresh();
                Graphics g = this.CreateGraphics();
                Size s = this.Size;
                _memImage = new Bitmap(s.Width, s.Height, g);
                Graphics memGraphic = Graphics.FromImage(_memImage);
                IntPtr dc1 = g.GetHdc();
                IntPtr dc2 = memGraphic.GetHdc();
                BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
                g.ReleaseHdc();
                memGraphic.ReleaseHdc();
     
                // Show the print dialog
                PrintDialog pd = new PrintDialog();
                pd.Document = printDocument1;
                DialogResult dr = pd.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    printDocument1.Print();
                }
            }
            #endregion
     
            #region Event Print Page
            private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                e.Graphics.DrawImage(_memImage, 0, 0);
            }
            #endregion
    Bonne journée !

  3. #3
    Membre très actif
    Profil pro
    Développeur Java
    Inscrit en
    Avril 2006
    Messages
    130
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Avril 2006
    Messages : 130
    Par défaut
    Allez, déjà pour ceux que ça intéresse, les explications du code précédent (je les ai trouvé après) : http://www.dotnet-tech.com/tutoriels...ptureEcran.pdf
    Idéal pour le framework .NET 1.1

    Pour ceux qui codent avec .NET 2.0 :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Size s = this.Size;
    Bitmap bmp = new Bitmap(s.Width, s.Height);
    this.DrawToBitmap(bmp, new Rectangle(0, 0, s.Width, s.Height));
    Il suffit de remplacer this par le contrôle qui vous intéresse si vous ne voulez pas le form en entier

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Lazarus] Quelle méthode pour dupliquer des Forms
    Par Invité dans le forum Lazarus
    Réponses: 3
    Dernier message: 20/07/2012, 09h07
  2. Réponses: 3
    Dernier message: 14/08/2007, 22h58
  3. Réponses: 7
    Dernier message: 04/04/2007, 13h37
  4. Réponses: 16
    Dernier message: 09/10/2006, 18h25
  5. [C#] Quelle méthode pour savoir l'état d'un fichier ?
    Par freesly dans le forum Windows Forms
    Réponses: 15
    Dernier message: 25/04/2006, 14h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo