![]() |
![]() |
| Paypal | FTP | CCD | Buscar | Trucos | Trabajo | Foros |
|
|||||||
| Registrarse | FAQ | Miembros | Calendario | Guía de estilo | Temas de Hoy |
![]() |
|
|
Herramientas | Buscar en Tema | Desplegado |
|
|
|
#1
|
|||
|
|||
|
Problema clase PrintDocument
Hola a todos necesito ayuda urgente, me estoy volviendo loca con este tema. Estoy utilizando la clase PrintDocument para imprimir un archivo q tengo en memoria en un MemoryStream, pero el ejemplo viene en visual basic mis preguntas son:
1. No entiendo cómo relaciona el PrintDocument con el StreamReader. 2. Esta linea de codigo AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage cómo se traduce a delphi. 3. Cómo se asocia el parametro ev As PrintPageEventArgs del método pd_PrintPage Por favor, agradecería que alguien me pudiera ayudar. Muchas gracias por adelantado. El código completo lo adjunto a continuacion. CODIGO COMPLETO Código:
Public Class PrintingExample
Inherits System.Windows.Forms.Form
Private components As System.ComponentModel.Container
Private printButton As System.Windows.Forms.Button
Private printFont As Font
Private streamToPrint As StreamReader
Public Sub New()
' The Windows Forms Designer requires the following call.
InitializeComponent()
End Sub
' The Click event is raised when the user clicks the Print button.
Private Sub printButton_Click(sender As Object, e As EventArgs)
Try
streamToPrint = New StreamReader("C:\My Documents\MyFile.txt")
Try
printFont = New Font("Arial", 10)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
' The PrintPage event is raised for each page to be printed.
Private Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
' Print each line of the file.
While count < linesPerPage
line = streamToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
count += 1
End While
' If more lines exist, print another page.
If Not (line Is Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
Última edición por __hector fecha: 29-03-2007 a las 20:10:58. |
|
#2
|
||||
|
||||
|
Hola,
Nunca he usado la clase PrintDocument, pero para aclarar tus dudas (aunque genere otras): - El AddressOf es la forma en la que VB maneja los delegados. En tu caso particular, al parecer la clase PrintDocument, al llamar el metodo Print(), dispara el evento PrintPage (pd.PrintPage). Lo que hace AddressOf es decir: cuando se dispare el evento PrintPage del objeto pd de tipo PrintDocument, yo quiero que tu me ejecutes el metodo pd_PrintPage que yo tengo definido en mi clase. Este metodo (pd_PrintPage) tiene que cumplir con el "signature" (tipo y numero de parametros) definido para ese evento, en tu caso, recibir un object y PrintPageEventArgs, en sender y ev, como parametros del metodo. Eso lo puedes ver con cualquier otro componente que arrastres al formulario, especialmente en c#, ya que vb oculta (lamentablemente) esto del programador, y lo maneja con un ... methodname() Handles control.Evento. Son cosas del lenguaje. Sobre lo segundo, la asociacion no se da entre el stream y el printdocument, sino en: Código:
line = streamToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
__________________
Héctor Geraldino Software Engineer |
![]() |
|
|
Temas Similares
|
||||
| Tema | Autor | Foro | Respuestas | Último mensaje |
| Instanciar Clase | buildero_d | OOP | 2 | 05-10-2006 06:12:56 |
| Variables de clase | supermilloriver | OOP | 11 | 28-08-2006 23:03:40 |
| Problema con Clase | pelaorb68 | OOP | 7 | 27-07-2006 04:57:57 |
| ...la clase... | Jure | Humor | 0 | 27-07-2004 20:00:47 |
| Clase de Catellano | Investment | Humor | 0 | 30-09-2003 13:59:22 |
|