Imprimer avec richtextbox

Résolu
danmor Messages postés 31 Statut Membre -  
arion320 Messages postés 219 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour, Tout le monde

Bon début d'année

Moi je commence bien avec cette question

est-ce que quellequ'un a une bonne facon d'imprimer avec vb 2005 j'en ai essayer beaucoup mais rien a faire.
y a toujours une erreur quellque par que je trouve pas.
en avez vous une bonne facon qui fonctionne bien si ont peut maider ca serait gentil.

a+
Configuration: Windows XP
Internet Explorer 7.0

2 réponses

  1. Nico# Messages postés 328 Date d'inscription   Statut Membre Dernière intervention   102
     
    Salut,

    1.

    Créez une classe RichTextBoxPrintCtrl qui hérite de RichTextBox.
    2.

    Utilisez la classe RichTextBoxPrintCtrl dans votre projet mis à niveau.

    Pour créer une classe qui hérite de la classe RichTextBox

    1.

    Utilisez Visual Basic 2005 pour créer un nouveau projet Bibliothèque de classes nommé RichTextBoxPrintCtrl.

    Par défaut, Class1.vb est créé.
    2.

    Modifiez le nom du fichier Class1.vb pour l'appeler RichTextBoxPrintCtrl.vb.
    3.

    Dans l'Explorateur de solutions, cliquez avec le bouton droit sur Références, puis cliquez sur Ajouter une référence.
    4.

    Dans la boîte de dialogue Ajouter une référence, double-cliquez sur System.Drawing.dll, puis sur System.Windows.Forms.dll.
    5.

    Pour ajouter les références, cliquez sur OK.
    6.

    Supprimez le code existant dans RichTextBoxPrintCtrl.vb.
    7.

    Ajoutez le code suivant à RichTextBoxPrintCtrl.vb :

    Option Explicit On

    Imports System
    Imports System.Windows.Forms
    Imports System.Drawing
    Imports System.Runtime.InteropServices
    Imports System.Drawing.Printing

    Namespace RichTextBoxPrintCtrl
    Public Class RichTextBoxPrintCtrl
    Inherits RichTextBox

    ' Convert the unit that is used by the .NET framework
    ' (1/100 inch) and the unit that is used by Win32 API calls
    ' (twips 1/1440 inch)
    Private Const AnInch As Double = 14.4

    Private WithEvents m_PrintDocument As Printing.PrintDocument
    Private intCharactersToPrint As Integer
    Private intCurrentPosition As Integer

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure RECT
    Public Left As Integer
    Public Top As Integer
    Public Right As Integer
    Public Bottom As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure CHARRANGE
    ' First character of range (0 for start of doc)
    Public cpMin As Integer
    ' Last character of range (-1 for end of doc)
    Public cpMax As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure FORMATRANGE
    ' Actual DC to draw on
    Public hdc As IntPtr
    ' Target DC for determining text formatting
    Public hdcTarget As IntPtr
    ' Region of the DC to draw to (in twips)
    Public rc As Rect
    ' Region of the whole DC (page size) (in twips)
    Public rcPage As Rect
    ' Range of text to draw (see above declaration)
    Public chrg As CHARRANGE
    End Structure

    Private Const WM_USER As Integer = &H400
    Private Const EM_FORMATRANGE As Integer = WM_USER + 57

    Private Declare Function SendMessage Lib "USER32" Alias _
    "SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
    ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr

    Public Sub SelPrint()

    'print only the selected text if any is selected
    If Me.SelectionLength > 0 Then
    intCharactersToPrint = Me.SelectionStart + Me.SelectionLength
    intCurrentPosition = Me.SelectionStart
    Else
    'otherwise print the entire document
    intCharactersToPrint = Me.TextLength
    intCurrentPosition = 0
    End If

    m_PrintDocument.Print()

    End Sub
    ' Render the contents of the RichTextBox for printing
    ' Return the last character printed + 1 (printing start from
    ' this point for next page)
    Private Function Print(ByVal charFrom As Integer, _
    ByVal charTo As Integer, ByVal e As PrintPageEventArgs) As Integer

    ' Mark starting and ending character
    Dim cRange As CHARRANGE
    cRange.cpMin = charFrom
    cRange.cpMax = charTo

    ' Calculate the area to render and print
    Dim rectToPrint As RECT
    rectToPrint.Top = e.MarginBounds.Top * AnInch
    rectToPrint.Bottom = e.MarginBounds.Bottom * AnInch
    rectToPrint.Left = e.MarginBounds.Left * AnInch
    rectToPrint.Right = e.MarginBounds.Right * AnInch

    ' Calculate the size of the page
    Dim rectPage As RECT
    rectPage.Top = e.PageBounds.Top * AnInch
    rectPage.Bottom = e.PageBounds.Bottom * AnInch
    rectPage.Left = e.PageBounds.Left * AnInch
    rectPage.Right = e.PageBounds.Right * AnInch

    Dim hdc As IntPtr = e.Graphics.GetHdc()

    Dim fmtRange As FORMATRANGE
    ' Indicate character from to character to
    fmtRange.chrg = cRange
    ' Use the same DC for measuring and rendering
    fmtRange.hdc = hdc
    ' Point at printer hDC
    fmtRange.hdcTarget = hdc
    ' Indicate the area on page to print
    fmtRange.rc = rectToPrint
    ' Indicate whole size of page
    fmtRange.rcPage = rectPage

    Dim res As IntPtr = IntPtr.Zero

    Dim wparam As IntPtr = IntPtr.Zero
    wparam = New IntPtr(1)

    ' Move the pointer to the FORMATRANGE structure in
    ' memory
    Dim lparam As IntPtr = IntPtr.Zero
    lparam = _
    Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
    Marshal.StructureToPtr(fmtRange, lparam, False)

    ' Send the rendered data for printing
    res = _
    SendMessage(Handle, EM_FORMATRANGE, wparam, lparam)

    ' Free the block of memory allocated
    Marshal.FreeCoTaskMem(lparam)

    ' Release the device context handle obtained by a
    ' previous call
    e.Graphics.ReleaseHdc(hdc)

    'return the last + 1 character printed
    Return res.ToInt32

    End Function
    Public ReadOnly Property PrintDocument() As Printing.PrintDocument
    Get
    If m_PrintDocument Is Nothing Then
    m_PrintDocument = New Printing.PrintDocument
    End If

    Return m_PrintDocument
    End Get
    End Property

    Private Sub m_PrintDocument_PrintPage(ByVal sender As _
    Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
    Handles m_PrintDocument.PrintPage
    ' Print the content of the RichTextBox.
    ' Store the last character printed.

    intCurrentPosition = Me.Print(intCurrentPosition, _
    intCharactersToPrint, e)

    ' Look for more pages by checking
    If intCurrentPosition < intCharactersToPrint Then
    e.HasMorePages = True
    Else
    e.HasMorePages = False
    End If

    End Sub
    End Class
    End Namespace

    8.

    Pour créer RichTextBoxPrintCtrl.dll, cliquez sur Générer la solution dans le menu Générer.

    Pour utiliser la classe RichTextBoxPrintCtrl dans votre projet mis à niveau

    1.

    Ouvrez votre projet d'application Windows mis à niveau dans Visual Basic 2005.
    2.

    À partir de la boîte à outils, faites glisser un composant PrintDialog sur le formulaire contenant le contrôle RichTextBox.
    3.

    Dans le menu Outils, cliquez sur Personnaliser la boîte à outils.
    4.

    Cliquez sur Composants .NET Framework, sur Parcourir, sélectionnez RichTextBoxPrintCtrl.dll, puis cliquez sur OK.
    5.

    À partir de la Boîte à outils, faites glisser un contrôle RichTextBoxPrintCtrl dans le formulaire.
    6.

    Définissez les propriétés de RichTextBoxPrintCtrl afin qu'elles correspondent aux propriétés de votre contrôle RichTextBox d'origine.
    7.

    Supprimez le contrôle RichTextBox, puis renommez le contrôle RichTextBoxPrintCtrl pour lui attribuer le nom de votre contrôle RichTextBox d'origine.
    8.

    Double-cliquez sur le formulaire pour ouvrir l'éditeur de code.
    9.

    Ajoutez le code suivant au gestionnaire d'événements Button_Click (ou partout où vous avez appelé SelPrint dans le code d'origine) :

    ' NOTE: Replace "Button1" with the name of your actual Print button
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    'associate the correct printdocument to the printdialog
    PrintDialog1.Document = Me.RichTextBoxPrintCtrl1.PrintDocument

    If PrintDialog1.ShowDialog() = DialogResult.OK Then
    Me.RichTextBoxPrintCtrl1.SelPrint()
    End If
    End Sub
    0
  2. danmor Messages postés 31 Statut Membre
     
    Merci mais la je recoit directement une erreur de violation d'imprimante

    jai deja ce cette dll effective dans un autre de mes projet et c est le meme probleme

    Merci pour ton aides mais je vais devoir verifier tout mes programme qui utilise une imprimant et voir c est quoi le prob et merci encore
    0
    1. chiffre
       
      Merci pour ces détails pour imprimer..

      Ceci fonctionne tres bien il me reste quelques code a ajouter...

      Merci
      chiffre
      0