// #define _DEBUG
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//add
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Windows.Threading;
using System.Runtime.InteropServices;
namespace PeacePadEditor01
{
partial class PPad1_Form1
{
//-----------------------------------------------------------------------------------------
// 印刷 (調査継続部分あり)
//-----------------------------------------------------------------------------------------
// 印刷する文字列
private string stringToPrint = "";
// ページカウンタ
private int ipage_count = 0;
// ページごとの複数印刷カウンタ
private int i_page_copy = 0;
/***********************************************************************/
/// <summary>
/// 印刷ページの設定ダイアログ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 印刷の設定ToolStripMenuItem_Click( object sender, EventArgs e )
{
// 印刷ページの設定ダイアログを表示
this.pageSetupDialog1.ShowDialog();
}
/***********************************************************************/
/// <summary>
/// ファイル>印刷 イベントハンドラ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 印刷PToolStripMenuItem_Click( object sender, EventArgs e )
{
// 印刷ダイアログを表示
DialogResult result = this.printDialog1.ShowDialog();
// 調査
// Console.WriteLine( "this.printDialog1.PrinterSettings.PrintRange = " + this.printDialog1.PrinterSettings.PrintRange.ToString() );
// ページごとの複数印刷カウンタ
this.i_page_copy = 0;
// If the result is OK then print the document.
this.runPrint( result );
}
/***********************************************************************/
/// <summary>
/// 印刷ボタン押下時の印刷処理
/// </summary>
/// <param name="result"></param>
private void runPrint( DialogResult result )
{
if( result == DialogResult.OK )
{
// Console.WriteLine( "this.printDialog1.PrinterSettings.Collate = " + this.printDialog1.PrinterSettings.Collate );
// 出力ページ数のリセット
this.i_out_page = 0;
// 出力総ページ数リセット
this.i_total_page = 0;
// リセット
this.bl_sw_count_page = false;
this.ipage_count = 0; // ページカウンタ
this.ReadFile(); // テキストデータ読込み処理
this.printDocument1.Print(); // ページごとの印刷イベントを実行
}
}
/***********************************************************************/
/// <summary>
/// テキストデータ読込み処理。(現在、ファイル読み取りは無い)
/// </summary>
private void ReadFile()
{
//string docName = this.Text; // "testPage.txt";
//string docPath = @"c:\";
string file_name2 = this.getCurrentTextBox2().getFileName();
printDocument1.DocumentName = this.Text;
// (修正)
stringToPrint = this.getCurrentTextBox().Text;
}
/***********************************************************************
////////////////////////////////////////////////////////////////////////
/// <summary>
/// 使用中:ページごとの印刷イベントハンドラ3b
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument1_PrintPage3b( object sender, System.Drawing.Printing.PrintPageEventArgs e )
{
int charactersOnPage = 0; // ページごとの文字数
int linesPerPage = 0; // ページごとの行数
// v0.98.0031
StringFormat sfmt = StringFormat.GenericTypographic; // new StringFormat();
float fl = this.getCurrentTextBox2().Font.Size * 4 * (1845f / 2048f); //0.8f;
float[] tab_stops = { fl };
sfmt.SetTabStops( 0, tab_stops );
// StringFormat.GenericDefault
// sfmt.SetTabStops
// sfmt.SetTabStops();
// 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.getCurrentTextBox2().Font,
e.MarginBounds.Size, sfmt, //StringFormat.GenericDefault, // .GenericTypographic,
out charactersOnPage, out linesPerPage );
// ヘッダ
if( true == this.ヘッタフッタを印刷ToolStripMenuItem.Checked )
{
this.drowPageHeaderForPrintOut( e );
}
// Draws the string within the bounds of the page
e.Graphics.DrawString( stringToPrint, this.getCurrentTextBox2().Font, Brushes.Black,
e.MarginBounds, sfmt ); // StringFormat.GenericDefault ); //.GenericTypographic );
// フッタ
if( true == this.ヘッタフッタを印刷ToolStripMenuItem.Checked )
{
this.drowPageFooterForPrintOut( e );
}
// ページごとの複数印刷(部数単位でのチェックOFF)
if( false == this.printDialog1.PrinterSettings.Collate )
{
// ページごとの複数印刷カウンタを判定。
if( this.i_page_copy < (this.printDialog1.PrinterSettings.Copies - 1) )
{
// ページごとの複数印刷カウンタをアップ。
this.i_page_copy++;
// string stringToPrint を変更しないで印刷。
}
else
{
// ページごとの複数印刷カウンタをリセット
this.i_page_copy = 0;
// Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring( charactersOnPage );
}
}
else
{
// 1度だけ出力
// 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);
}
////////////////////////////////////////////////////////////////////////
/// <summary>
/// 印刷時の各ページ毎のヘッダ出力
/// </summary>
/// <param name="e"></param>
private void drowPageHeaderForPrintOut( System.Drawing.Printing.PrintPageEventArgs e )
{
// ヘッダ
string file_name = Path.GetFileName( this.getCurrentTextBox2().getFileName());
this.drowPageHeaderForPrintOut( file_name, e );
}
////////////////////////////////////////////////////////////////////////
/// <summary>
/// ヘッダー文字列の幅を取得
/// </summary>
/// <param name="str_header"></param>
/// <param name="e"></param>
/// <returns></returns>
private int getStringWidthOnPrintOutPage( string str_header, System.Drawing.Printing.PrintPageEventArgs e )
{
Rectangle rect = new Rectangle( 0, 0, 0, this.getCurrentTextBox2().Font.Height + 1 );
int sum_chars = 0;
int linesPerPage = 0;
while( sum_chars < str_header.Length )
{
rect.Width += 1;
e.Graphics.MeasureString( str_header, this.getCurrentTextBox2().Font,
rect.Size, StringFormat.GenericTypographic,
out sum_chars, out linesPerPage );
}
return rect.Width;
}
////////////////////////////////////////////////////////////////////////
/// <summary>
/// 印刷時の各ページ毎のヘッダ出力(コア)
/// </summary>
/// <param name="str_header"></param>
/// <param name="e"></param>
private void drowPageHeaderForPrintOut( string str_header, System.Drawing.Printing.PrintPageEventArgs e )
{
// ヘッダのY位置
float fl_header_y = e.MarginBounds.Y - this.getCurrentTextBox2().Font.Height;
// ヘッダ文字列の横幅を求める。
int header_width = this.getStringWidthOnPrintOutPage( str_header, e );
// ヘッダのX位置
float fl_header_x = e.MarginBounds.X + (e.MarginBounds.Width - header_width) / 2;
// ヘッダの描画
e.Graphics.DrawString( str_header, this.getCurrentTextBox2().Font, Brushes.Black,
fl_header_x, fl_header_y, StringFormat.GenericTypographic );
}
// ページ数カウンタ
private int i_out_page = 0;
// 出力総ページ数
private int i_total_page = 0;
// 出力総ページ数制御用
private bool bl_sw_count_page = false;
////////////////////////////////////////////////////////////////////////
/// <summary>
/// フッタ出力
/// </summary>
/// <param name="e"></param>
private void drowPageFooterForPrintOut( System.Drawing.Printing.PrintPageEventArgs e )
{
if( false == this.bl_sw_count_page )
{
this.i_total_page = this.countTotalPageForPrintOut( this.stringToPrint, e );
this.bl_sw_count_page = true;
}
// フッタ
this.i_out_page++;
string str_footer = this.i_out_page.ToString() + "/" + this.i_total_page.ToString();
this.drowPageFooterForPrintOut( str_footer, e );
}
////////////////////////////////////////////////////////////////////////
/// <summary>
/// フッタ出力(コア)
/// </summary>
/// <param name="str_footer"></param>
/// <param name="e"></param>
private void drowPageFooterForPrintOut( string str_footer, System.Drawing.Printing.PrintPageEventArgs e )
{
// フッタのY位置
float fl_footer_y = e.MarginBounds.Y + e.MarginBounds.Height + 3;
// フッタ文字列の横幅を求める。
int footer_width = this.getStringWidthOnPrintOutPage( str_footer, e );
// フッタのX位置
float fl_footer_x = e.MarginBounds.X + (e.MarginBounds.Width - footer_width);
// フッタの描画
e.Graphics.DrawString( str_footer, this.getCurrentTextBox2().Font, Brushes.Black,
fl_footer_x, fl_footer_y, StringFormat.GenericTypographic );
}
////////////////////////////////////////////////////////////////////////
/// <summary>
/// 出力する総ページ数カウント
/// </summary>
/// <param name="str_to_print"></param>
/// <param name="e"></param>
/// <returns></returns>
private int countTotalPageForPrintOut( string str_to_print, System.Drawing.Printing.PrintPageEventArgs e )
{
int sum_chars = 0;
int linesPerPage = 0;
int i_page = 0;
while( 0 < str_to_print.Length )
{
i_page++;
e.Graphics.MeasureString( str_to_print, this.getCurrentTextBox2().Font,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out sum_chars, out linesPerPage );
str_to_print = str_to_print.Substring( sum_chars );
}
return i_page;
}
////////////////////////////////////////////////////////////////////////
/// <summary>
/// メニュー・「ヘッタフッタを印刷」
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ヘッタフッタを印刷ToolStripMenuItem_Click( object sender, EventArgs e )
{
if( true == this.ヘッタフッタを印刷ToolStripMenuItem.Checked )
{
this.ヘッタフッタを印刷ToolStripMenuItem.Checked = false;
}
else
{
this.ヘッタフッタを印刷ToolStripMenuItem.Checked = true;
}
}
/***********************************************************************/
/// <summary>
/// 印刷プレビューイベントハンドラ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 印刷プレビューVToolStripMenuItem_Click( object sender, EventArgs e )
{
this.printPreviewDialog1.ShowDialog();
}
/***********************************************************************/
/// <summary>
/// ファイルメニュー>印刷プレビュー
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printPreviewDialog1_Load( object sender, EventArgs e )
{
// int i_out_page = 0;
// string str_footer = i_out_page.ToString();
}
// 検討中
/***********************************************************************/
/// <summary>
/// 印刷ページ設定 イベントハンドラ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument1_QueryPageSettings( object sender, System.Drawing.Printing.QueryPageSettingsEventArgs e )
{
// 印刷ページのカウント
this.ipage_count++;
if( 1 == this.ipage_count )
{
// e.Cancel = true;
// e.PageSettings.
}
}
}
}