Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

19
Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007

Transcript of Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Page 1: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Printer & Dialog boxes

Engr. Faisal ur RehmanCE-105T Spring 2007

Page 2: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Printer

• In GDI+, we have the namespace of System.Drawing.Printing for printing.

• For printing, we need:– PrintDocument Class– Print method– Graphics Property– StreamReader– DrawString– PrintController– PreviewPrintController– PrintPreviewControl

Page 3: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Printer

• Following dialog boxes are also used:– PrintDialog– PageSetupDialog– PrintPreviewDialog

Page 4: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Explanation• PrintDocument Class: Defines a reusable object that sends output to

a printer.

• Print method: This method is called by PrintDocument to print document

• Graphics Property: Specify the output to print.

• StreamReader reads one line at a time from the stream

• DrawString method is used to draw the line in the graphics object.

• PrintController controls how a PrintDocument is printed. It has following methods:– OnStartPrint, OnEndPrint, OnStartPage, and OnEndPage

• PreviewPrintController and PrintPreviewControl controls the preview of Document

Page 5: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Printing Un-Formated Text

• Use PrintDocument1 Control and Pass its Print Method

• For facilitation, add PrintDialog1 and attach its Document to PrintDocument1

• PrintPreviewDialog1 will add preview while PageSetupDialog1 will add page setup functionality

Page 6: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Printing Un-Formated Text

• During a print, an event of pdoc_PrintPage is fired, which is attached to PrintDocument1

• During this event, following setting is done:

1. Font

2. DefaultPageSettings

3. Format for print, which is rectangular

4. And Check for more pages to Print

Page 7: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Printing Graphics

• Following namespaces are used:– System.ComponentModel– System.Collections– System.Drawing– System.Drawing.Printing– System.Resources– System.Windows.Forms

Page 8: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Printing Graphics

• Declare PrintDocument and add EventHandler of PrintPage which will pass during Print method of PrintDocument

• In event picture from resource is printed by DrawImage method

Page 9: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Q & A

• Define the following:– PrintDocument Class– Print method– Graphics Property– StreamReader– DrawString– PrintController– PreviewPrintController– PrintPreviewControl

• Write the procedure for printing text files (unformatted)• Write the procedure for printing graphics

Page 10: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Dialog Box

• Secondary window opened from main window to display or get information from user is called a dialog box

• In simple terms, a dialog box is a form with its FormBorderStyle enumeration property set to FixedDialog.

• Dialog boxes are used to interact with the user and retrieve information.

• There are two categories: – Modal– Modeless.

Page 11: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Dialog Box

• Modal dialog box commonly prevents users from activating other windows while it remains open.

• A modeless dialog box, on the other hand, does not prevent users from activating other windows while it is open.

Page 12: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Types

• Example of dialog boxes falls in to three categories:– Message Boxes – Common Dialog Boxes – Custom Dialog Boxes

Page 13: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Types

• Message box is a simple dialog box that can be used to both display information and allow users to make a decision.

• Common dialog boxes are:– OpenFileDialog– SaveFileDialog– PrintDialog

• Custom Dialog box is more complex than a message box and is not supported by the common dialog boxes, you need to create your own.

Page 14: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Example of Open Dialog Box Private Sub open() Dim myStream As IO.Stream Dim openFileDialog1 As New OpenFileDialog()

openFileDialog1.InitialDirectory = "c:\" openFileDialog1.Filter = "MPL Input Files|*.txt" openFileDialog1.FilterIndex = 2 openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then myStream = openFileDialog1.OpenFile() If Not (myStream Is Nothing) Then ' Insert code to read the stream here. Dim path As String = "" Dim text As String = "" path = openFileDialog1.FileName text = My.Computer.FileSystem.ReadAllText(path) sInputFile = text myStream.Close() End If End If End Sub

Page 15: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Example of Open Dialog Box Friend Sub save()

Dim saveFileDialog1 As New SaveFileDialog() saveFileDialog1.Filter = "Text Files|*.txt" saveFileDialog1.Title = "Save a Text File" saveFileDialog1.ShowDialog()

If saveFileDialog1.FileName <> "" Then Dim Path As String = "" Dim Text As String = ""

Path = saveFileDialog1.FileName Text = sInputFile My.Computer.FileSystem.WriteAllText(Path, Text, True) End If

End Sub

Page 16: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Example of Font Dialog Private Sub button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.Click

FontDialog1.ShowColor = True FontDialog1.Font = TextBox1.Font FontDialog1.Color = TextBox1.ForeColor If FontDialog1.ShowDialog() <> _ Windows.Forms.DialogResult.Cancel Then TextBox1.Font = FontDialog1.Font TextBox1.ForeColor = FontDialog1.Color End If End Sub

Page 17: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Example of Color Dialog Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Dim MyDialog As New ColorDialog() MyDialog.AllowFullOpen = False MyDialog.ShowHelp = True MyDialog.Color = TextBox1.ForeColor If (MyDialog.ShowDialog() = _ Windows.Forms.DialogResult.OK) Then TextBox1.BackColor = MyDialog.Color End If

End Sub

Page 18: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

Q & A

• Define a dialog box

• What are categories and types

• Name some dialog boxes

• Write example code of a dialog box

Page 19: Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.

• T

• H

• A

• N

• K

• S