| |

VB.NET - Ein- und UmsteigerDer Code hat doch gepasst | |  | Autor: lighty | Datum: 05.09.06 12:13 |
| Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Drawing.Printing
Namespace Printing
Public Class PrintFromRichTextBox
Private RTB As RichTextBox
Private WithEvents PrintDoc As PrintDocument
Private CharsToPrint As Integer
Private Pos As Integer
#Region "Properties"
Public ReadOnly Property PrintDocument() As PrintDocument
Get
If PrintDoc Is Nothing Then
PrintDoc = New PrintDocument
End If
Return PrintDoc
End Get
End Property
#End Region
#Region "Const"
Private Const AnInch As Double = 14.4
Private Const WM_User As Integer = &H400
Private Const EM_Formatrange As Integer = WM_User + 57
#End Region
#Region "Structur"
<StructLayout(LayoutKind.Sequential)> _
Private Structure Rect
Public Left
Public Top
Public Right
Public Bottom
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure CharRange
Public FirstChar As Integer
Public LastChar As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure FormatRange
Public hdc As IntPtr
Public hdcTarget As IntPtr
Public Rectangle As Rect
Public RectanglePage As Rect
Public ChRange As CharRange
End Structure
#End Region
#Region "Function"
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
Private Function Print(ByVal CharFrom As Integer, ByVal CharTo As Integer, ByVal e As PrintPageEventArgs) As Integer
Dim CRange As CharRange
CRange.FirstChar = CharFrom
CRange.LastChar = CharTo
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
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
fmtRange.ChRange = CRange
fmtRange.hdc = hdc
fmtRange.hdcTarget = hdc
fmtRange.Rectangle = RectToPrint
fmtRange.RectanglePage = rectPage
Dim res As IntPtr = IntPtr.Zero
Dim wparam As IntPtr = IntPtr.Zero
wparam = New IntPtr(1)
Dim lparam As IntPtr = IntPtr.Zero
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
Marshal.StructureToPtr(fmtRange, lparam, False)
res = SendMessage(RTB.Handle, EM_Formatrange, wparam, lparam)
Marshal.FreeCoTaskMem(lparam)
e.Graphics.ReleaseHdc(hdc)
Return res.ToInt32
End Function
#End Region
#Region "Sub"
Public Sub New(ByVal RichTextBox As RichTextBox)
RTB = RichTextBox
End Sub
Public Sub SelPrint()
If RTB.SelectionLength > 0 Then
CharsToPrint = RTB.SelectionStart + RTB.SelectionLength
Pos = RTB.SelectionStart
Else
CharsToPrint = RTB.TextLength
Pos = 0
End If
PrintDoc.Print()
End Sub
Private Sub PrintDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
Pos = Print(Pos, CharsToPrint, e)
If Pos < CharsToPrint Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
#End Region
End Class
End Namespace
Mfg, lighty
---------------------------------------------------
Schaut mal hier:
http://www.reset.ch/index.html
(die Links sind ungef?hrlich )
Ps.: F?r jeden, der wie ich, zu oft vorm Rechner sitzt |  |
 | Sie sind nicht angemeldet! Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.
Einloggen | Neu registrieren |
  |
|
Neu! sevEingabe 3.0 
Einfach stark!
Ein einziges Eingabe-Control für alle benötigten Eingabetypen und -formate, inkl. Kalender-, Taschenrechner und Floskelfunktion, mehrspaltige ComboBox mit DB-Anbindung, ImageComboBox u.v.m. Weitere InfosTipp des Monats Oktober 2025 Matthias KozlowskiUmlaute konvertierenErsetzt die Umlaute in einer Zeichenkette durch die entsprechenden Doppelbuchstaben (aus ä wird ae, usw.) Access-Tools Vol.1 
Über 400 MByte Inhalt
Mehr als 250 Access-Beispiele, 25 Add-Ins und ActiveX-Komponenten, 16 VB-Projekt inkl. Source, mehr als 320 Tipps & Tricks für Access und VB
Nur 24,95 EURWeitere Infos
|
|
|
Copyright ©2000-2025 vb@rchiv Dieter Otter Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.
Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel
|
|