This is the first part of my Print Tutorial ! OK will help you begin =)
In this small tutor I 'll show you some basics of printing with VB 2005, I made and tested my self.
Ok may be the first thing to think on is to find printers on the computer.
So take a look @ this code
Ok add a new combo box and all should works well !
Dim InstalledPrinters As String
' Find all printers installed
For Each InstalledPrinters In _
PrinterSettings.InstalledPrinters
combobox1.Items.Add(InstalledPrinters)
Next InstalledPrinters
' Set the combo to the first printer in the list
If combobox1.Items.Count > 0 Then
combobox1.SelectedIndex = 0
End If
Now let's learn some basics of preview and print. So let's think on how to preview a document.
First let's make a design to our preview (some limits) we need then to use VB Pen
Take a look, the following code will help you to show a pageview !
This is the whole code to more help you !
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnPreview As System.Windows.Forms.Button
Friend WithEvents pdocTest As System.Drawing.Printing.PrintDocument
Friend WithEvents ppdTest As System.Windows.Forms.PrintPreviewDialog
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.btnPreview = New System.Windows.Forms.Button
Me.pdocTest = New System.Drawing.Printing.PrintDocument
Me.ppdTest = New System.Windows.Forms.PrintPreviewDialog
Me.SuspendLayout()
'
'btnPreview
'
Me.btnPreview.Location = New System.Drawing.Point(79, 43)
Me.btnPreview.Name = "btnPreview"
Me.btnPreview.TabIndex = 0
Me.btnPreview.Text = "Preview"
'
'pdocTest
'
'
'ppdTest
'
Me.ppdTest.AutoScrollMargin = New System.Drawing.Size(0, 0)
Me.ppdTest.AutoScrollMinSize = New System.Drawing.Size(0, 0)
Me.ppdTest.ClientSize = New System.Drawing.Size(400, 300)
Me.ppdTest.Document = Me.pdocTest
Me.ppdTest.Enabled = True
Me.ppdTest.Icon = CType(resources.GetObject("ppdTest.Icon"), System.Drawing.Icon)
Me.ppdTest.Location = New System.Drawing.Point(144, 18)
Me.ppdTest.MinimumSize = New System.Drawing.Size(375, 250)
Me.ppdTest.Name = "ppdTest"
Me.ppdTest.TransparencyKey = System.Drawing.Color.Empty
Me.ppdTest.Visible = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(232, 109)
Me.Controls.Add(Me.btnPreview)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
' Convert the dialog into a Form.
Dim dlg As Form = DirectCast(ppdTest, Form)
' Set a "restore" size.
dlg.Width = 600
dlg.Height = 400
' Start the dialog maximized.
dlg.WindowState = FormWindowState.Maximized
' Show the dialog.
ppdTest.ShowDialog()
End Sub
Private Sub pdocTest_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdocTest.PrintPage
Dim the_pen As New Pen(Color.Blue, 10)
e.Graphics.DrawRectangle(the_pen, e.MarginBounds)
the_pen.Dispose()
e.HasMorePages = False
End Sub
End Class
That's quite good for today come some days later coz me only I don't know much and I'm learning ;)
0 comments:
Post a Comment