すらすらプログラマーへのお問い合わせ
すらすらのブログ
アクセス数  累計:000,099,223  昨日:000,000,137  本日:000,000,123
Loading
印刷
.NET プログラミング
プロパティグリッド
印刷
リストボックス
スレッド
リストビュー
インストーラー
やってみよう!

できるVisual Studio 2015 Windows /Android/iOS アプリ対応

独習C# 第3版

VisualBasic2013パーフェクトマスター (Perfect Master SERIES)

プログラミング.NET Framework 第4版 (Microsoft Press)

VisualBasic2013逆引き大全555の極意

猫でもわかるWindowsプログラミング 第4版 (猫でもわかるプログラミング)

VisualC#2013逆引き大全555の極意

印刷ダイアログ① 最終更新:2010/07/15
【Amazon ランキング:ゲーム - プレイステーション4】

ここでは印刷ダイアログを表示する方法を紹介します。.NET には PrintDialog というダイアログフォームが用意されていますので今回はこれを使ってみようと思います。
印刷ダイアログの使い方
印刷ダイアログの使い方はいたって簡単です。インスタンスを作成して参照するプリンタドキュメントを設定した後に ShowDialog でダイアログを呼び出すだけです。ダイアログで「OK」ボタンをクリックすると設定された内容が参照で設定してプリンタドキュメントに反映されます。
[VB]
Using dlog As PrintDialog = New PrintDialog
    
' プリンタオブジェクト設定
    dlog.Document = doc

    
' プリンタダイアログ表示
    If dlog.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then
        Return
    End If
End
 Using

[C#]
using (PrintDialog dlog = new PrintDialog())
{
    
// プリンタオブジェクト設定
    dlog.Document = doc;

    
// プリンタダイアログ表示
    if(dlog.ShowDialog(this) != DialogResult.OK)
        
return;
}
VB[Form1.vb]
Imports System.Drawing.Printing

Public Class Form1

    
Private Const MAX_PAGE_COUNT As Integer = 5

    
' ページ番号
    Private _pageNumber As Integer = 0

    
Private Sub Button1_Click(ByVal sender As System.Object, _
        
ByVal e As System.EventArgs) Handles Button1.Click

        
Using doc As PrintDocument = New PrintDocument

            
Using dlog As PrintDialog = New PrintDialog
                
' プリンタオブジェクト設定
                dlog.Document = doc

                
' プリンタダイアログ表示
                If dlog.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then
                    Return
                End If
            End Using

            ' PrintPage イベントハンドル追加
            AddHandler doc.PrintPage, AddressOf doc_PrintPage

            
' ページ番号初期化
            _pageNumber = 1

            
' 印刷実行
            doc.Print()

        
End Using

    End Sub

    ' 印刷イベント処理
    Private Sub doc_PrintPage(ByVal sender As Object, _
        
ByVal e As System.Drawing.Printing.PrintPageEventArgs)

        
' 印刷範囲枠を描画
        e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds)

        
' ページ数印刷
        e.Graphics.DrawString(String.Format("{0}ページ", _pageNumber), _
            
New Font("MSゴシック", 20, FontStyle.Bold, GraphicsUnit.Point), _
            Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)

        
If _pageNumber < MAX_PAGE_COUNT Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If

        ' ページ番号加算
        _pageNumber = _pageNumber + 1
    
End Sub

End
 Class
C#[Form1.cs]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace Print_CS
{
    
public partial class Form1 : Form
    {
        
private const int MAX_PAGE_COUNT = 5;

        
// ページ番号
        private int _pageNumber = 0;

        
public Form1()
        {
            InitializeComponent();
        }

        
private void Button1_Click(object sender, EventArgs e)
        {
            
using (PrintDocument doc = new PrintDocument())
            {
                
using (PrintDialog dlog = new PrintDialog())
                {
                    
// プリンタオブジェクト設定
                    dlog.Document = doc;

                    
// プリンタダイアログ表示
                    if(dlog.ShowDialog(this) != DialogResult.OK)
                        
return;                    
                }

                
// PrintPage イベントハンドル追加
                doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);

                
// ページ番号初期化
                _pageNumber = 1;

                
// 印刷実行
                doc.Print();
            }
        }

        
// 印刷イベント処理
        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {

            
// 印刷範囲枠を描画
            e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds);

            
// ページ数印刷
            e.Graphics.DrawString(String.Format("{0}ページ", _pageNumber),
                
new Font("MS ゴシック", 20, FontStyle.Bold, GraphicsUnit.Point),
                
Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);

            
if (_pageNumber < MAX_PAGE_COUNT)
            {
                e.HasMorePages = 
true;
            }
            
else
            {
                e.HasMorePages = 
false;
            }

            
// ページ番号加算
            _pageNumber = _pageNumber + 1;
        }
    }
}
※このページで紹介しているサンプルコードについて管理者は動作保障をいたしません※
※サンプルコードを使用する場合は、自己責任でお願いします※

【楽天 ランキング:パソコン・周辺機器 - パソコン周辺機器】




このサイトはフリーソフトのMerge HTMLで作成されています。
このサイトはリンクフリーです。

ページの先頭に戻る Copyright© 2010-2015 Jun.Shiozaki All rights reserved.