■ Windows フォームに於ける印刷のサポート ■ Windows フォームに於ける印刷では、主に、ユーザーに依る印刷を可能にする為の PrintDocument コ ンポーネントと、Windows オペレーティングシステムを常用して居るユーザーに見慣れたグラフィカ ルインターフェイスを提供する為のPrintPreviewDialog コントロール、PrintDialog コンポーネント、 及び、PageSetupDialog コンポーネントが使用される。 通常、印刷を行う場合には、PrintDocument コンポーネントの新しいインスタンスを作成し、
PrinterSettings クラスと PageSettings クラスを使用して印刷対象を定義してから、Print メソッドを 呼び出してドキュメントを実際に印刷する。 Windows ベースのアプリケーションから印刷を行って居る間、PrintDocument コンポーネントは、印 刷中止ダイアログボックスを表示して、印刷が実行中で有る事をユーザーに通知し、ユーザーが印刷ジ ョブをキャンセル出来る様にする。 ■ 標準の Windows フォーム印刷ジョブを作成する方法 Windows フォームでの印刷の基盤と成るのは、PrintDocument コンポーネントで有る。具体的に謂え ば、PrintPage イベントで有る。PrintPage イベントを処理するコードを記述する事に依り、印刷対象 と印刷方法を指定出来る。 印刷ジョブを作成するには 1.フォームにPrintDocument コンポーネントを追加する。 2.PrintPage イベントを処理するコードを記述する。 独自の印刷ロジックをコーディングする必要が有る。亦、印刷する対象も指定する必要が有る。 次のコード例では、印刷対象と仕てPrintPage イベントハンドラ内で赤い四角形のサンプルグラフ ィックを作成して居る。 Visual Basic
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _ ByVal e As System.Drawing.Printing.PrintPageEventArgs) _ Handles PrintDocument1.PrintPage
e.Graphics.FillRectangle(Brushes.Red, New Rectangle(500, 500, 500, 500)) End Sub
C# private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
e.Graphics.FillRectangle(Brushes.Red, new Rectangle(500, 500, 500, 500)); }
印
印刷
刷
Visual C#では、フォームのコンストラクタに次のコードを挿入してイベントハンドラを登録する。 C# this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler (this.printDocument1_PrintPage); 亦、BeginPrint イベントと EndPrint イベントに対するコードを記述する事が必要な場合も有る。其の 場合、各ページが印刷される度に、印刷する合計ページ数を表す整数がデクリメントされる様にも出来 る。 ※ フォームに PrintDialog コンポーネントを追加すると、明快で効率的なユーザーインターフェイス をユーザーに提供出来る。PrintDialog コンポーネントの Document プロパティを設定する事に依 り、フォーム上で作業中の印刷ドキュメントに関連するプロパティを設定出来る。 ■ 実行時に PrintDialog のユーザー入力をキャプチャする方法 印刷に関連するオプションはデザイン時に設定出来るが、多くの場合ユーザーの選択に依って、実行時 に此等のオプションの変更が必要と成る事が有る。PrintDialog コンポーネントと PrintDocument コン ポーネントを使用して、文書を印刷するユーザー入力をキャプチャ出来る。 印刷オプションをプログラムで変更するには 1.フォームにPrintDialog コンポーネントと PrintDocument コンポーネントを追加する。 2.PrintDialog の Document プロパティをフォームに追加した PrintDocument に設定する。
Visual Basic PrintDialog1.Document = PrintDocument1 C# printDialog1.Document = PrintDocument1; 3.ShowDialog メソッドを使用して PrintDialog コンポーネントを表示する。 Visual Basic PrintDialog1.ShowDialog( ) C# printDialog1.ShowDialog( ); 4 . ダ イ ア ロ グ ボ ッ ク ス で ユ ー ザ ー が 選 択 し た 内 容 は 、PrintDocument コ ン ポ ー ネ ン ト の PrinterSettings プロパティにコピーされる。 ■ Windows フォームでユーザーのコンピュータに接続されたプリンタを選択する方法 ユーザーが既定のプリンタ以外のプリンタを印刷先と仕て選択する事は良く有る。PrintDialog コンポ ーネントを使用すると、現在インストールされて居る複数のプリンタからユーザーが任意のプリンタを 選 択 出 来 る 様 に 成 る 。PrintDialog コ ン ポ ーネ ン ト を 介 し て 、 PrintDialog コ ン ポー ネ ン トの DialogResult が取り込まれ、其れに基づいてプリンタが選択される。 次の手順では、テキストファイルを既定のプリンタで印刷する様に選択する。其の後で、PrintDialog クラスがインスタンス化される。
プリンタを選択してファイルを印刷するには 1.PrintDialog コンポーネントを使用して、使用するプリンタを選択する。 次のコード例では、2 つのイベントが処理される。最初のイベントで有る Button コントロールの Click イベントでは、PrintDialog クラスがインスタンス化され、ユーザーに依って選択されたプリ ンタがDialogResult プロパティで取り込まれる。 2 番目のイベントで有る PrintDocument コンポーネントの PrintPage イベントでは、指定された プリンタでサンプルドキュメントが印刷される。 Visual Basic
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Button1.Click
Dim PrintDialog1 As New PrintDialog( ) PrintDialog1.Document = PrintDocument1
Dim result As DialogResult = PrintDialog1.ShowDialog( )
If (result = DialogResult.OK) Then PrintDocument1.Print( )
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _ ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.FillRectangle(Brushes.Red, New Rectangle(500, 500, 500, 500)) End Sub
C#
private void button1_Click(object sender, System.EventArgs e) {
PrintDialog printDialog1 = new PrintDialog( ); printDialog1.Document = printDocument1; DialogResult result = printDialog1.ShowDialog( ); if (result == DialogResult.OK)
{
printDocument1.Print( ); }
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
e.Graphics.FillRectangle(Brushes.Red, new Rectangle(500, 500, 500, 500)); } Visual C#では、フォームのコンストラクタに次のコードを挿入してイベントハンドラを登録する。 C# this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler (this.printDocument1_PrintPage);
■ Windows フォームでグラフィックスを印刷する方法
Windows ベースのアプリケーションでグラフィックスの印刷が必要に成る事は良く有る。Graphics ク ラスは、画面やプリンタ等のデバイスにオブジェクトを描画する手段を提供する。
グラフィックスを印刷するには
1.フォームにPrintDocument コンポーネントを追加する。
2.PrintPage イベントハンドラで、PrintPageEventArgs クラスの Graphics プロパティを使用して 印刷するグラフィックスの種類をプリンタに指示する。
外接する四角形内に青い楕円を作成する為に使用されるイベントハンドラのコード例を次に示す。 四角形の位置は点100, 150 で始まり、寸法は幅 250、高さ 250 で有る。
Visual Basic
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _ ByVal e As System.Drawing.Printing.PrintPageEventArgs) _ Handles PrintDocument1.PrintPage
e.Graphics.FillEllipse(Brushes.Blue, New Rectangle(100, 150, 250, 250)) End Sub
C#
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
e.Graphics.FillRectangle(Brushes.Blue, new Rectangle(100, 150, 250, 250)); } Visual C#では、フォームのコンストラクタに次のコードを挿入してイベントハンドラを登録する。 C# this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler (this.printDocument1_PrintPage); ■ Windows フォームで複数ページのテキストファイルを印刷する方法 Windows ベースのアプリケーションでテキストを印刷するのは、極く一般的な操作で有る。Graphics クラスは、画面やプリンタ等のデバイスにオブジェクト(グラフィックスやテキスト)を描画するメソ ッドを提供する。 ※ TextRenderer の DrawText メソッドでは、印刷はサポートされて居ない。次のコード例に示す様に、 印刷する為にテキストを描画するには、常にGraphics の DrawString メソッドを使用する必要が有 る。 テキストを印刷するには 1.フォームにPrintDocument コンポーネントと文字列を追加する。 Visual Basic Private printDocument1 As New PrintDocument( ) Private stringToPrint As String
C#
private PrintDocument printDocument1 = new PrintDocument( ); private string stringToPrint;
2.文書を印刷する場合、DocumentName プロパティを印刷する文書に設定し、前以て追加して有っ た文字列に文書の内容を読み込む。
Visual Basic Dim docName As String = "testPage.txt"
Dim docPath As String = "c:¥"
printDocument1.DocumentName = docName
Dim stream As New FileStream(docPath + docName, FileMode.Open) Try
Dim reader As New StreamReader(stream) Try stringToPrint = reader.ReadToEnd( ) Finally reader.Dispose( ) End Try Finally stream.Dispose( ) End Try C# string docName = "testPage.txt";
string docPath = @"c:¥";
printDocument1.DocumentName = docName;
using (FileStream stream = new FileStream(docPath + docName, FileMode.Open)) using (StreamReader reader = new StreamReader(stream))
{
stringToPrint = reader.ReadToEnd( ); }
3.PrintPage イベントハンドラで PrintPageEventArgs クラスの Graphics プロパティと文書の内容 を使用し、1 ページ当りの行の長さと行数を計算する。各ページの描画後に、其のページが最終ペ ージか何うかを確認し、結果に応じて PrintPageEventArgs の HasMorePages プロパティを設定 する。HasMorePages が false に成る迄、PrintPage イベントが発生する。亦、PrintPage イベン トがイベント処理メソッドに関連付けられて居る事を確認する。
イベントハンドラを使用して、フォームに使用したのと同じフォントでtestPage.txt ファイルの内
容を印刷するコード例を次に示す。
Visual Basic
Private Sub printDocument1_PrintPage(ByVal sender As Object, _ ByVal e As PrintPageEventArgs)
Dim charactersOnPage As Integer = 0 Dim linesPerPage As Integer = 0
' Sets the value of charactersOnPage to the number of characters ' of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _ StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
' Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _ e.MarginBounds, StringFormat.GenericTypographic)
' Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage)
' Check to see if more pages are to be printed. e.HasMorePages = stringToPrint.Length > 0 End Sub
C#
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
int charactersOnPage = 0; int linesPerPage = 0;
// Sets the value of charactersOnPage to the number of characters // of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage);
// Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage);
// Check to see if more pages are to be printed. e.HasMorePages = (stringToPrint.Length > 0); } 4.Print メソッドを呼び出す。PrintPage イベントが発生する。 Visual Basic printDocument1.Print( ) C# printDocument1.Print( ); 使用例 Visual Basic Imports System Imports System.Drawing Imports System.IO Imports System.Drawing.Printing Imports System.Windows.Forms Public Class Form1
Inherits Form
Private printButton As Button
Private printDocument1 As New PrintDocument( ) Private stringToPrint As String
Public Sub New( )
Me.printButton.Location = New System.Drawing.Point(12, 51) Me.printButton.Size = New System.Drawing.Size(75, 23) Me.printButton.Text = "Print"
Me.ClientSize = New System.Drawing.Size(292, 266) End Sub
Private Sub ReadFile( )
Dim docName As String = "testPage.txt" Dim docPath As String = "c:¥"
printDocument1.DocumentName = docName
Dim stream As New FileStream(docPath + docName, FileMode.Open) Try
Dim reader As New StreamReader(stream) Try stringToPrint = reader.ReadToEnd( ) Finally reader.Dispose( ) End Try Finally stream.Dispose( ) End Try End Sub
Private Sub printDocument1_PrintPage(ByVal sender As Object, _ ByVal e As PrintPageEventArgs)
Dim charactersOnPage As Integer = 0 Dim linesPerPage As Integer = 0
' Sets the value of charactersOnPage to the number of characters ' of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _ StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
' Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _ e.MarginBounds, StringFormat.GenericTypographic)
' Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage)
' Check to see if more pages are to be printed. e.HasMorePages = stringToPrint.Length > 0
End Sub
Private Sub printButton_Click(ByVal sender As Object, ByVal e As EventArgs) ReadFile( )
printDocument1.Print( ) End Sub
<STAThread( )> _ Shared Sub Main( )
Application.EnableVisualStyles( )
Application.Run(New Form1( )) End Sub End Class C# using System; using System.Drawing; using System.IO; using System.Drawing.Printing; using System.Windows.Forms; namespace PrintApp {
public class Form1 : Form {
private Button printButton;
private PrintDocument printDocument1 = new PrintDocument( ); private string stringToPrint;
public Form1( ) {
this.printButton = new System.Windows.Forms.Button( ); this.printButton.Location = new System.Drawing.Point(12, 51); this.printButton.Size = new System.Drawing.Size(75, 23); this.printButton.Text = "Print";
this.printButton.Click += new System.EventHandler(this.printButton_Click); this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.printButton);
// Associate the PrintPage event handler with the PrintPage event. printDocument1.PrintPage +=
new PrintPageEventHandler(printDocument1_PrintPage); }
private void ReadFile( ) {
string docName = "testPage.txt"; string docPath = @"c:¥";
printDocument1.DocumentName = docName;
using (FileStream stream = new FileStream(docPath + docName, FileMode.Open)) using (StreamReader reader = new StreamReader(stream))
{
stringToPrint = reader.ReadToEnd( ); }
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
int charactersOnPage = 0; int linesPerPage = 0;
// Sets the value of charactersOnPage to the number of characters // of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage);
// Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage);
// Check to see if more pages are to be printed. e.HasMorePages = (stringToPrint.Length > 0); }
private void printButton_Click(object sender, EventArgs e) { ReadFile( ); printDocument1.Print( ); } [STAThread] static void Main( ) { Application.EnableVisualStyles( ); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1( )); } } } ■ Windows フォームの印刷ジョブを完了する方法 印刷ジョブを伴うワードプロセッサや其他のアプリケーションでは、多くの場合、印刷ジョブが完了し たと謂うメッセージをユーザーに表示するオプションが用意されて居る。PrintDocument コンポーネ ントのEndPrint イベントを処理する事に依って、Windows フォームに此の機能を用意出来る。 次の手順では、PrintDocument コンポーネントの有る Windows ベースのアプリケーションを作成して 居る必要が有る。此れはWindows ベースのアプリケーションからの印刷を有効にする標準的な方法で 有る。 印刷ジョブを完了するには 1.PrintDocument コンポーネントの DocumentName プロパティを設定する。 Visual Basic PrintDocument1.DocumentName = "MyTextFile" C# printDocument1.DocumentName = "MyTextFile"; 2.EndPrint イベントを処理するコードを記述する。 次のコード例では、ドキュメントの印刷が完了した事を示すメッセージ ボックスが表示される。 Visual Basic
Private Sub PrintDocument1_EndPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint MessageBox.Show(PrintDocument1.DocumentName + " has finished printing.")
C#
private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
MessageBox.Show(printDocument1.DocumentName + " has finished printing."); } Visual C#では、フォームのコンストラクタに次のコードを挿入してイベントハンドラを登録する。 C# this.printDocument1.EndPrint += new System.Drawing.Printing.PrintEventHandler (this.printDocument1_EndPrint); ■ Windows フォームを印刷する方法 開発プロセスに於いて、多くの場合、Windows フォームのコピーを印刷する必要が有る。現在のフォ ームのコピーをCopyFromScreen メソッドを使用して印刷する方法を次のコード例に示す。 使用例 Visual Basic Imports System Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Printing Public Class Form1
Inherits Form
Private WithEvents printButton As New Button
Private WithEvents printDocument1 As New PrintDocument
Public Sub New( )
printButton.Text = "Print Form" Me.Controls.Add(printButton) End Sub
Dim memoryImage As Bitmap
Private Sub CaptureScreen( )
Dim myGraphics As Graphics = Me.CreateGraphics( ) Dim s As Size = Me.Size
memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage) memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s) End Sub
Private Sub printDocument1_PrintPage(ByVal sender As System.Object, _ ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _ printDocument1.PrintPage
e.Graphics.DrawImage(memoryImage, 0, 0) End Sub
System.EventArgs) Handles printButton.Click CaptureScreen( )
printDocument1.Print( ) End Sub
Public Shared Sub Main( )
Application.Run(New Form1( )) End Sub End Class C# using System; using System.Windows.Forms; using System.Drawing; using System.Drawing.Printing; public class Form1 : Form {
private Button printButton = new Button( );
private PrintDocument printDocument1 = new PrintDocument( );
public Form1( ) {
printButton.Text = "Print Form";
printButton.Click += new EventHandler(printButton_Click);
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage); this.Controls.Add(printButton);
}
void printButton_Click(object sender, EventArgs e) { CaptureScreen( ); printDocument1.Print( ); } Bitmap memoryImage;
private void CaptureScreen( ) {
Graphics myGraphics = this.CreateGraphics( ); Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics); Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s); }
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0); }
public static void Main( ) {
Application.Run(new Form1( )); }
■ Windows フォームで印刷プレビューを使用して印刷する方法 Windows フォームのプログラミングでは、印刷サービスの他に印刷プレビューを実装する事は一般的 で有る。アプリケーションに印刷プレビューサービスを追加する簡単な方法は、ファイルの印刷に、 PrintPreviewDialog コントロールと PrintPage イベント処理ロジックを組み合わせて使用する事で有 る。 PrintPreviewDialog コントロールを使用してテキスト文書をプレビューするには 1.フォームにPrintPreviewDialog、PrintDocument、及び、2 つの文字列を追加する。 Visual Basic
Private printPreviewDialog1 As New PrintPreviewDialog( ) Private WithEvents printDocument1 As New PrintDocument( ) ' Declare a string to hold the entire document contents.
Private documentContents As String
' Declare a variable to hold the portion of the document that ' is not printed.
Private stringToPrint As String
C#
private PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog( ); private PrintDocument printDocument1 = new PrintDocument( );
// Declare a string to hold the entire document contents. private string documentContents;
// Declare a variable to hold the portion of the document that // is not printed.
private string stringToPrint;
2.DocumentName プロパティを印刷する文書に設定し、前以て追加して有った文字列に文書の内容 を読み込む。
Visual Basic Private Sub ReadDocument( )
Dim docName As String = "testPage.txt" Dim docPath As String = "c:¥"
printDocument1.DocumentName = docName
Dim stream As New FileStream(docPath + docName, FileMode.Open) Try
Dim reader As New StreamReader(stream) Try documentContents = reader.ReadToEnd( ) Finally reader.Dispose( ) End Try Finally stream.Dispose( ) End Try stringToPrint = documentContents End Sub
C# private void ReadDocument( )
{
string docName = "testPage.txt"; string docPath = @"c:¥";
printDocument1.DocumentName = docName;
using (FileStream stream = new FileStream(docPath + docName, FileMode.Open)) using (StreamReader reader = new StreamReader(stream))
{ documentContents = reader.ReadToEnd( ); } stringToPrint = documentContents; } 3.文書を印刷する場合と同様に、PrintPage イベントハンドラでは、PrintPageEventArgs クラスの Graphics プロパティとファイルの内容を使用し、1 ページ当りの行数を計算し、文書の内容を描画 す る 。 各 ペ ー ジ の 描 画 後 に 、 其 の ペ ー ジ が 最 終 ペ ー ジ か 何 う か を 確 認 し 、 結 果 に 応 じ て PrintPageEventArgs の HasMorePages プロパティを設定する。HasMorePages が false に成る迄、 PrintPage イベントが発生する。文書の描画が完了したら、描画対象の文字列をリセットする。亦、 PrintPage イベントがイベント処理メソッドに関連付けられて居る事を確認する。 ※ アプリケーションに印刷機能を実装済みの場合、手順 2 と 3 は完了して居る事も有る。 イベントハンドラを使用して、フォームに使用したのと同じフォントでtestPage.txt ファイルを印 刷するコード例を次に示す。 Visual Basic Sub printDocument1_PrintPage(ByVal sender As Object, _
ByVal e As PrintPageEventArgs) Handles printDocument1.PrintPage
Dim charactersOnPage As Integer = 0 Dim linesPerPage As Integer = 0
' Sets the value of charactersOnPage to the number of characters ' of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _ StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
' Draws the string within the bounds of the page.
e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _ e.MarginBounds, StringFormat.GenericTypographic)
' Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage)
' Check to see if more pages are to be printed. e.HasMorePages = stringToPrint.Length > 0
' If there are no more pages, reset the string to be printed. If Not e.HasMorePages Then
stringToPrint = documentContents End If
End Sub
C#
void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
int charactersOnPage = 0; int linesPerPage = 0;
// Sets the value of charactersOnPage to the number of characters // of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage);
// Draws the string within the bounds of the page.
e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage);
// Check to see if more pages are to be printed. e.HasMorePages = (stringToPrint.Length > 0);
// If there are no more pages, reset the string to be printed. if (!e.HasMorePages)
stringToPrint = documentContents; }
4.PrintPreviewDialog コントロールの Document プロパティをフォームの PrintDocument コンポー ネントに設定する。
Visual Basic printPreviewDialog1.Document = printDocument1
C# printPreviewDialog1.Document = printDocument1;
5.PrintPreviewDialog コントロールの ShowDialog メソッドを呼び出す。通常、ボタンの Click イベ ント処理メソッドからShowDialog を呼び出す。ShowDialog を呼び出すと、PrintPage イベント
が発生し、出力を PrintPreviewDialog コントロールに描画する。ユーザーがダイアログボックス の印刷アイコンをクリックするとPrintPage イベントが再発生し、出力はプレビューダイアログボ ックスでは無くプリンタに送信される。此れが、手順3 で描画プロセスの最後に文字列をリセット する理由で有る。 フォーム上に有るボタンのClick イベント処理メソッドのコード例を次に示す。此のイベント処理 メソッドは、文書を読み込んで印刷プレビューダイアログボックスを表示するメソッドを呼び出す。 Visual Basic
Private Sub printPreviewButton_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles printPreviewButton.Click
ReadDocument( )
printPreviewDialog1.Document = printDocument1 printPreviewDialog1.ShowDialog( )
C#
private void printPreviewButton_Click(object sender, EventArgs e) { ReadDocument( ); printPreviewDialog1.Document = printDocument1; printPreviewDialog1.ShowDialog( ); } 使用例 Visual Basic Imports System Imports System.Drawing Imports System.IO Imports System.Drawing.Printing Imports System.Windows.Forms Class Form1 Inherits Form
Private WithEvents printPreviewButton As Button
Private printPreviewDialog1 As New PrintPreviewDialog( ) Private WithEvents printDocument1 As New PrintDocument( )
' Declare a string to hold the entire document contents. Private documentContents As String
' Declare a variable to hold the portion of the document that is not printed. Private stringToPrint As String
Public Sub New( )
Me.printPreviewButton = New System.Windows.Forms.Button( ) Me.printPreviewButton.Location = New System.Drawing.Point(12, 12) Me.printPreviewButton.Size = New System.Drawing.Size(125, 23) Me.printPreviewButton.Text = "Print Preview"
Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.printPreviewButton)
End Sub
Private Sub ReadDocument( )
Dim docName As String = "testPage.txt" Dim docPath As String = "c:¥"
printDocument1.DocumentName = docName
Dim stream As New FileStream(docPath + docName, FileMode.Open) Try
Dim reader As New StreamReader(stream) Try documentContents = reader.ReadToEnd( ) Finally reader.Dispose( ) End Try Finally stream.Dispose( )
End Try
stringToPrint = documentContents End Sub
Sub printDocument1_PrintPage(ByVal sender As Object, _
ByVal e As PrintPageEventArgs) Handles printDocument1.PrintPage
Dim charactersOnPage As Integer = 0 Dim linesPerPage As Integer = 0
' Sets the value of charactersOnPage to the number of characters ' of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _ StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
' Draws the string within the bounds of the page.
e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _ e.MarginBounds, StringFormat.GenericTypographic)
' Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage)
' Check to see if more pages are to be printed. e.HasMorePages = stringToPrint.Length > 0
' If there are no more pages, reset the string to be printed. If Not e.HasMorePages Then
stringToPrint = documentContents End If
End Sub
Private Sub printPreviewButton_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles printPreviewButton.Click ReadDocument( ) printPreviewDialog1.Document = printDocument1 printPreviewDialog1.ShowDialog( ) End Sub <STAThread( )> _ Shared Sub Main( )
Application.EnableVisualStyles( ) Application.SetCompatibleTextRenderingDefault(False) Application.Run(New Form1( )) End Sub End Class C# using System; using System.Drawing; using System.IO; using System.Drawing.Printing; using System.Windows.Forms; namespace PrintPreviewApp {
public partial class Form1 : Form {
private Button printPreviewButton;
private PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog( ); private PrintDocument printDocument1 = new PrintDocument( );
// Declare a string to hold the entire document contents. private string documentContents;
// Declare a variable to hold the portion of the document that is not printed. private string stringToPrint;
public Form1( ) {
this.printPreviewButton = new System.Windows.Forms.Button( ); this.printPreviewButton.Location = new System.Drawing.Point(12, 12); this.printPreviewButton.Size = new System.Drawing.Size(125, 23); this.printPreviewButton.Text = "Print Preview";
this.printPreviewButton.Click += new
System.EventHandler(this.printPreviewButton_Click); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.printPreviewButton);
printDocument1.PrintPage +=
new PrintPageEventHandler(printDocument1_PrintPage); }
private void ReadDocument( ) {
string docName = "testPage.txt"; string docPath = @"c:¥";
printDocument1.DocumentName = docName;
using (FileStream stream = new FileStream(docPath + docName, FileMode.Open)) using (StreamReader reader = new StreamReader(stream))
{ documentContents = reader.ReadToEnd( ); } stringToPrint = documentContents; }
void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
int charactersOnPage = 0; int linesPerPage = 0;
// Sets the value of charactersOnPage to the number of characters // of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage);
// Draws the string within the bounds of the page.
e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage);
// Check to see if more pages are to be printed. e.HasMorePages = (stringToPrint.Length > 0);
// If there are no more pages, reset the string to be printed. if (!e.HasMorePages)
stringToPrint = documentContents; }
private void printPreviewButton_Click(object sender, EventArgs e) { ReadDocument( ); printPreviewDialog1.Document = printDocument1; printPreviewDialog1.ShowDialog( ); } [STAThread] static void Main( ) { Application.EnableVisualStyles( ); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1( )); } } }