目的
前回のC#で印刷可能範囲いっぱいに枠線を書くにプリンタを選択しても同じように動作するように少し変更したもの。
環境
前回と同じ。
ソースコード
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; using System.Drawing.Printing; using System.Drawing; namespace _20190608_PrintTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void 印刷ToolStripMenuItem_Click(object sender, EventArgs e) { try { MessageBox.Show( "Printer Name : " + printDocument.PrinterSettings.PrinterName, "Printer Name"); // 原点を設定する。 printDocument.OriginAtMargins = true; printDialog.Document = printDocument; if (printDialog.ShowDialog() == DialogResult.OK) { printDocument.DefaultPageSettings = new PageSettings(printDocument.PrinterSettings); printDocument.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); MessageBox.Show( "Printer Name : " + printDocument.PrinterSettings.PrinterName, "Printer Name"); printDocument.Print(); } else { return; } } catch (Exception ex) { MessageBox.Show(ex.Message, "エラー"); } } private void printDocument_PrintPage(object sender, PrintPageEventArgs e) { MessageBox.Show( "X : " + e.PageSettings.PrintableArea.X.ToString() + "\n" + "Y : " + e.PageSettings.PrintableArea.Y.ToString() + "\n" + "Width : " + e.PageSettings.PrintableArea.Width.ToString() + "\n" + "Height : " + e.PageSettings.PrintableArea.Height.ToString(), "PrintableArea" ); // 印刷可能な領域を枠で囲む。 e.Graphics.DrawRectangle( new Pen(Color.Blue, 2), e.PageSettings.PrintableArea.X, e.PageSettings.PrintableArea.Y, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height ); } } }