印刷

Similar documents
VB.NET解説

グラフィックス

ICONファイルフォーマット

VB 資料 電脳梁山泊烏賊塾 音声認識 System.Speech の利用 System.Speech に依るディクテーション ( 音声を文字列化 ).NetFramework3.0 以上 (Visual Studio 2010 以降 ) では 標準で System.Speech が用意されて居るの

ファイル操作

プロセス間通信

ファイル監視

NotifyIconコントロール

データアダプタ概要

API 連携方式 外部 DLL の呼び出し宣言 外部 DLL の呼び出し宣言のサンプルコード (Microsoft Visual C#.NET の場合 ) プログラムコードの先頭で using System.Runtime.InteropServices; が必要 クラスの内部に以下のような外部 D

ListViewコントロール

ファイル操作-インターネットキャッシュ

LogisticaTRUCKServer-Ⅱ距離計算サーバ/Active-Xコントロール/クライアント 概略   

GS1-128 の描画 DLL について (ver. 2.3) 動作環境など動作環境 WindowsXP Windows Vista Windows7 Windows8/8.1 Windows10 上記 OS について すべて日本語版を対象としております 32bit アプリケーションから呼び出される

ファイル操作-バイナリファイル

ルーレットプログラム

LogisticaTRUCKServer-Ⅱ距離計算サーバ/Active-Xコントロール/クライアント 概略   

ブロック パニック

Userコントロール

スライド 1

ウィンドウ操作 応用

VB実用⑫ 印刷Ⅱ(Printerオブジェクト)

スレッド操作 タイマー

プラグイン

ブロック崩し風テニス

グラフィックトレーニング 概要.NET のグラフィック描画は どんなことができるのでしょうか? グラフィックオブジェクトやグラフィック環境 概念を理解するためには クラスを使って馴れることが近道です 本 書に記載されているコードをカットアンドペーストして 一つ一つの機能を体験してください 前提 グラ

正規表現応用

チア ダンス

Microsoft Excel操作

Prog2_15th

Prog2_12th

C#の基本

平成 30 年度 プログラミング研修講座 岩手県立総合教育センター

VB実用⑬ 印刷Ⅲ(PrintFormメソッド)


クリッピング領域

Visual Basic 資料 電脳梁山泊烏賊塾 コレクション初期化子 コレクション初期化子 初めに.NET 版の Visual Basic では 其れ迄の Visual Basic 6.0 とは異なり 下記の例の様に変数宣言の構文に 初期値を代入する式が書ける様に成った 其の際 1 の様に単一の値

VB実用⑪ 印刷Ⅰ(プリンタ設定)

ハッシュテーブル

DAOの利用

パラパラ漫画


Secure iNetSuite for .NET 4.0Jの新仕様について

アプリケーション

PowerPoint プレゼンテーション

構造体

Prog2_4th

Visual Studio2008 C# で JAN13 バーコードイメージを作成 xbase 言語をご利用の現場でバーコードの出力が必要なことが多々あります xbase 言語製品によっては 標準でバーコード描画機能が付加されているものもあるようで す C# では バーコードフォントを利用したりバー

WebReportCafe

プロシード

VFD256 サンプルプログラム

PowerPoint プレゼンテーション

Case 0 sqlcmdi.parameters("?tencode").value = Iidata(0) sqlcmdi.parameters("?tenname").value = Iidata(1) 内容を追加します sqlcmdi.executenonquery() Case Else

バスケットボール

目次 はじめに... 3 システムの必要条件... 4 ライセンス認証... 4 アクティベーション... 6 開発... 7 手順 1. アプリケーションの作成... 7 手順 2. データソースの作成と代入... 7 手順 3. テンプレートの作成 手順 4. レポートビューアの追加

メール送信

D:\Documents\Visual Studio 2015\Projects\MyHomePage 用サンプル \ExcelAndWord\ExcelAndWord\MainForm.cs 1 /* */ Excel や Word とやりとりする ~9,20 仕様 Excel

倉庫番

Android Layout SDK プログラミング マニュアル

構造体

VB実用⑦ エクセル操作Ⅰ

mySQLの利用

GUIプログラムⅣ

3D回転体プログラム

ドッグファイト

グラフィックス 目次

画像閲覧プログラム

TestDesign for Web

プリンタ印字用 DLL 取扱説明書

プラグイン

パラパラ漫画

WebBrowserコントロール

C#の基本2 ~プログラムの制御構造~

Windows Layout SDK プログラミング マニュアル

テスト 1/7 ページ プレポスト Visual Studio による Windows アプリの開発 ( 基礎編 ) 受講日程受講番号氏名 1.NET Framework に関する記述で 誤っているものを選びなさい 1..NET Framework に含まれる CLR は プログラミング言語に依存し

3軸加速度センサーモジュール MM-2860 書込み済みマイコンプログラム通信コマンド概要

テキストファイルの入出力1

ガイダンス

3D回転体プログラム

XAML Do-It-Yourself 第 3 回ベントとトリガー XML Do-It-Yourself 第 3 回目は ベント処理とトリガーについて学習します Windows フォームゕプリケーションでは たとえば ボタンが押された というベントに対応する処理 ( ベントハンドラー ) を記述する

エクセル詳細 アドイン

ドライブは安全運転で in 滋賀♪

Prog2_2nd

平成 29 年度卒業研究 初心者のためのゲームプログラミング用 教材の開発 函館工業高等専門学校生産システム工学科情報コース 5 年 25 番細見政央指導教員東海林智也

Microsoft Word - VB.doc

VB実用Ⅲ⑩ フリーデータベースⅡ

CashDrawer ライブラリ API 仕様書 2014/07/09 CashDrawer ライブラリ API 仕様書 Rev / 10

ガイダンス

PYTHON 資料 電脳梁山泊烏賊塾 PYTHON 入門 関数とメソッド 関数とメソッド Python には関数 (function) とメソッド (method) が有る モジュール内に def で定義されて居る物が関数 クラス内に def で定義されて居る物がメソッドに成る ( 正確にはクラスが

MISAO with WPF

かべうちテニス

文書閲覧プログラム

LogisticaTRUCKServer-Ⅱ距離計算サーバ/Active-Xコントロール/クライアント 概略   

(Microsoft Word \203v\203\215\203O\203\211\203~\203\223\203O)

Microsoft Word -

ICONファイルフォーマット

A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3,

Prog1_10th

SharpShooter Reports.WPF 基本的な使い方 Last modified on: November 15, 2012 本ドキュメント内のスクリーンショットは英語表記ですが SharpShooter Reports JP( 日本語版 ) では日本語で表示されます

プログラミング基礎I(再)

Transcription:

印刷 Windows フォームに於ける印刷のサポート Windows フォームに於ける印刷では 主に ユーザーに依る印刷を可能にする為の PrintDocument コンポーネントと Windows オペレーティングシステムを常用して居るユーザーに見慣れたグラフィカルインターフェイスを提供する為の PrintPreviewDialog コントロール PrintDialog コンポーネント 及び PageSetupDialog コンポーネントが使用される 通常 印刷を行う場合には PrintDocument コンポーネントの新しいインスタンスを作成し PrinterSettings クラスと PageSettings クラスを使用して印刷対象を定義してから Print メソッドを呼び出してドキュメントを実際に印刷する Windows ベースのアプリケーションから印刷を行って居る間 PrintDocument コンポーネントは 印刷中止ダイアログボックスを表示して 印刷が実行中で有る事をユーザーに通知し ユーザーが印刷ジョブをキャンセル出来る様にする 標準の Windows フォーム印刷ジョブを作成する方法 Windows フォームでの印刷の基盤と成るのは PrintDocument コンポーネントで有る 具体的に謂えば PrintPage イベントで有る PrintPage イベントを処理するコードを記述する事に依り 印刷対象と印刷方法を指定出来る 印刷ジョブを作成するには 1. フォームに PrintDocument コンポーネントを追加する 2.PrintPage イベントを処理するコードを記述する 独自の印刷ロジックをコーディングする必要が有る 亦 印刷する対象も指定する必要が有る 次のコード例では 印刷対象と仕て PrintPage イベントハンドラ内で赤い四角形のサンプルグラフィックを作成して居る 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)) private void printdocument1_printpage(object sender, System.Drawing.Printing.PrintPageEventArgs e) e.graphics.fillrectangle(brushes.red, new Rectangle(500, 500, 500, 500)); -1-

Visual では フォームのコンストラクタに次のコードを挿入してイベントハンドラを登録する 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 に設定する PrintDialog1.Document = PrintDocument1 printdialog1.document = PrintDocument1; 3.ShowDialog メソッドを使用して PrintDialog コンポーネントを表示する PrintDialog1.ShowDialog( ) printdialog1.showdialog( ); 4. ダイアログボックスでユーザーが選択した内容は PrintDocument コンポーネントの PrinterSettings プロパティにコピーされる Windows フォームでユーザーのコンピュータに接続されたプリンタを選択する方法 ユーザーが既定のプリンタ以外のプリンタを印刷先と仕て選択する事は良く有る PrintDialog コンポーネントを使用すると 現在インストールされて居る複数のプリンタからユーザーが任意のプリンタを選択出来る様に成る PrintDialog コンポーネントを介して PrintDialog コンポーネントの DialogResult が取り込まれ 其れに基づいてプリンタが選択される 次の手順では テキストファイルを既定のプリンタで印刷する様に選択する 其の後で PrintDialog クラスがインスタンス化される -2-

プリンタを選択してファイルを印刷するには 1.PrintDialog コンポーネントを使用して 使用するプリンタを選択する 次のコード例では 2 つのイベントが処理される 最初のイベントで有る Button コントロールの Click イベントでは PrintDialog クラスがインスタンス化され ユーザーに依って選択されたプリンタが DialogResult プロパティで取り込まれる 2 番目のイベントで有る PrintDocument コンポーネントの PrintPage イベントでは 指定されたプリンタでサンプルドキュメントが印刷される 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 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)) 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 では フォームのコンストラクタに次のコードを挿入してイベントハンドラを登録する this.printdocument1.printpage += new System.Drawing.Printing.PrintPageEventHandler (this.printdocument1_printpage); this.button1.click += new System.EventHandler(this.button1_Click); -3-

Windows フォームでグラフィックスを印刷する方法 Windows ベースのアプリケーションでグラフィックスの印刷が必要に成る事は良く有る Graphics クラスは 画面やプリンタ等のデバイスにオブジェクトを描画する手段を提供する グラフィックスを印刷するには 1. フォームに PrintDocument コンポーネントを追加する 2.PrintPage イベントハンドラで PrintPageEventArgs クラスの Graphics プロパティを使用して印刷するグラフィックスの種類をプリンタに指示する 外接する四角形内に青い楕円を作成する為に使用されるイベントハンドラのコード例を次に示す 四角形の位置は点 100, 150 で始まり 寸法は幅 250 高さ 250 で有る 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)) private void printdocument1_printpage(object sender, System.Drawing.Printing.PrintPageEventArgs e) e.graphics.fillrectangle(brushes.blue, new Rectangle(100, 150, 250, 250)); Visual では フォームのコンストラクタに次のコードを挿入してイベントハンドラを登録する this.printdocument1.printpage += new System.Drawing.Printing.PrintPageEventHandler (this.printdocument1_printpage); Windows フォームで複数ページのテキストファイルを印刷する方法 Windows ベースのアプリケーションでテキストを印刷するのは 極く一般的な操作で有る Graphics クラスは 画面やプリンタ等のデバイスにオブジェクト ( グラフィックスやテキスト ) を描画するメソッドを提供する TextRenderer の DrawText メソッドでは 印刷はサポートされて居ない 次のコード例に示す様に 印刷する為にテキストを描画するには 常に Graphics の DrawString メソッドを使用する必要が有る テキストを印刷するには 1. フォームに PrintDocument コンポーネントと文字列を追加する Private printdocument1 As New PrintDocument( ) Private stringtoprint As String private PrintDocument printdocument1 = new PrintDocument( ); private string stringtoprint; -4-

2. 文書を印刷する場合 DocumentName プロパティを印刷する文書に設定し 前以て追加して有った文字列に文書の内容を読み込む 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 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 ファイルの内容を印刷するコード例を次に示す 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) -5-

' 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 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 イベントが発生する printdocument1.print( ) printdocument1.print( ); 使用例 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 = New System.Windows.Forms.Button( ) -6-

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) 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 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 Private Sub printbutton_click(byval sender As Object, ByVal e As EventArgs) ReadFile( ) printdocument1.print( ) <STAThread( )> _ Shared Sub Main( ) Application.EnableVisualStyles( ) Application.SetCompatibleTextRenderingDefault(False) -7-

Application.Run(New Form1( )) End Class 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); -8-

// 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 プロパティを設定する PrintDocument1.DocumentName = "MyTextFile" printdocument1.documentname = "MyTextFile"; 2.EndPrint イベントを処理するコードを記述する 次のコード例では ドキュメントの印刷が完了した事を示すメッセージボックスが表示される 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.") -9-

private void printdocument1_endprint(object sender, System.Drawing.Printing.PrintEventArgs e) MessageBox.Show(printDocument1.DocumentName + " has finished printing."); Visual では フォームのコンストラクタに次のコードを挿入してイベントハンドラを登録する this.printdocument1.endprint += new System.Drawing.Printing.PrintEventHandler (this.printdocument1_endprint); Windows フォームを印刷する方法 開発プロセスに於いて 多くの場合 Windows フォームのコピーを印刷する必要が有る 現在のフォームのコピーを CopyFromScreen メソッドを使用して印刷する方法を次のコード例に示す 使用例 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) 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) 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) Private Sub printbutton_click(byval sender As System.Object, ByVal e As _ -10-

System.EventArgs) Handles printbutton.click CaptureScreen( ) printdocument1.print( ) Public Shared Sub Main( ) Application.Run(New Form1( )) End Class 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( )); -11-

Windows フォームで印刷プレビューを使用して印刷する方法 Windows フォームのプログラミングでは 印刷サービスの他に印刷プレビューを実装する事は一般的で有る アプリケーションに印刷プレビューサービスを追加する簡単な方法は ファイルの印刷に PrintPreviewDialog コントロールと PrintPage イベント処理ロジックを組み合わせて使用する事で有る PrintPreviewDialog コントロールを使用してテキスト文書をプレビューするには 1. フォームに PrintPreviewDialog PrintDocument 及び 2 つの文字列を追加する 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 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 プロパティを印刷する文書に設定し 前以て追加して有った文字列に文書の内容を読み込む 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 -12-

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 ファイルを印刷するコード例を次に示す 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 -13-

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 コンポーネントに設定する printpreviewdialog1.document = printdocument1 printpreviewdialog1.document = printdocument1; 5.PrintPreviewDialog コントロールの ShowDialog メソッドを呼び出す 通常 ボタンの Click イベント処理メソッドから ShowDialog を呼び出す ShowDialog を呼び出すと PrintPage イベントが発生し 出力を PrintPreviewDialog コントロールに描画する ユーザーがダイアログボックスの印刷アイコンをクリックすると PrintPage イベントが再発生し 出力はプレビューダイアログボックスでは無くプリンタに送信される 此れが 手順 3 で描画プロセスの最後に文字列をリセットする理由で有る フォーム上に有るボタンの Click イベント処理メソッドのコード例を次に示す 此のイベント処理メソッドは 文書を読み込んで印刷プレビューダイアログボックスを表示するメソッドを呼び出す Private Sub printpreviewbutton_click(byval sender As Object, _ ByVal e As EventArgs) Handles printpreviewbutton.click ReadDocument( ) printpreviewdialog1.document = printdocument1 printpreviewdialog1.showdialog( ) -14-

使用例 private void printpreviewbutton_click(object sender, EventArgs e) ReadDocument( ); printpreviewdialog1.document = printdocument1; printpreviewdialog1.showdialog( ); 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) 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( ) -15-

End Try stringtoprint = documentcontents 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 Private Sub printpreviewbutton_click(byval sender As Object, _ ByVal e As EventArgs) Handles printpreviewbutton.click ReadDocument( ) printpreviewdialog1.document = printdocument1 printpreviewdialog1.showdialog( ) <STAThread( )> _ Shared Sub Main( ) Application.EnableVisualStyles( ) Application.SetCompatibleTextRenderingDefault(False) Application.Run(New Form1( )) End Class using System; using System.Drawing; using System.IO; using System.Drawing.Printing; using System.Windows.Forms; namespace PrintPreviewApp -16-

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); -17-

// 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( )); -18-