using System; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace CSharpPrinting { public class Form1 : System.Windows.Forms.Form { public Form1() { MenuItem fileMenuItem = new MenuItem("&File"); MenuItem filePageSetupMenuItem = new MenuItem("Page Set&up...", new EventHandler(filePageSetupMenuItem_Click)); MenuItem filePrintPreviewMenuItem = new MenuItem("Print Pre&view", new EventHandler(filePrintPreviewMenuItem_Click)); MenuItem filePrintMenuItem = new MenuItem("&Print...", new EventHandler(filePrintMenuItem_Click), Shortcut.CtrlP); fileMenuItem.MenuItems.Add(filePageSetupMenuItem); fileMenuItem.MenuItems.Add(filePrintPreviewMenuItem); fileMenuItem.MenuItems.Add(filePrintMenuItem); this.Menu = new MainMenu(); this.Menu.MenuItems.Add(fileMenuItem); } // -------------- event handlers ------------------------------------- private void filePrintMenuItem_Click(Object sender , EventArgs e) { } private void filePrintPreviewMenuItem_Click(Object sender , EventArgs e) { } private void filePageSetupMenuItem_Click(Object sender , EventArgs e) { } //-------------- end of event handlers ------------------------------ [STAThread] static void Main() { Application.Run(new Form1()); } } }fileMenuItem에 있는 세 가지 메뉴 항목 각각을 선언 부분에서 이벤트 핸들러와 연결하고 있다는 것을 알 수 있다. 세 가지 메뉴 항목에 대한 이벤트 핸들러는 다음 코드에 나와 있는 것처럼 filePageSetupMenuItem_Click, filePrintPreviewMenuItem_Click, filePrintMenuItem_Click이며 이 코드는 클래스 생성자 부분에 있다.
MenuItem filePageSetupMenuItem = new MenuItem("Page Set&up...", new EventHandler(filePageSetupMenuItem_Click)); MenuItem filePrintPreviewMenuItem = new MenuItem("Print Pre&view", new EventHandler(filePrintPreviewMenuItem_Click)); MenuItem filePrintMenuItem = new MenuItem("&Print...", new EventHandler(filePrintMenuItem_Click), Shortcut.CtrlP);목록 1의 코드에서 볼 수 있는 것처럼 세 가지 이벤트 핸들러는 현재 비어있다.
private void filePrintMenuItem_Click(Object sender , EventArgs e) { } private void filePrintPreviewMenuItem_Click(Object sender , EventArgs e) { } private void filePageSetupMenuItem_Click(Object sender , EventArgs e) { }이제, 이 템플릿에 인쇄 기능을 추가하기 위해 코드를 추가할 준비는 끝났다. 이제부터는 인쇄 기능을 추가해보자.
private PrintDocument printDoc = new PrintDocument();
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
private void filePrintMenuItem_Click(Object sender, EventArgs e) { printDoc.Print(); }
private void printDoc_PrintPage(Object sender , PrintPageEventArgs e) { String textToPrint = ".NET Printing is easy"; Font printFont = new Font("Courier New", 12); e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, 0, 0); }
참고 도서 |
using System; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace CSharpPrinting { public class Form1 : System.Windows.Forms.Form { private PrintDocument printDoc = new PrintDocument(); public Form1() { MenuItem fileMenuItem = new MenuItem("&File"); MenuItem filePageSetupMenuItem = new MenuItem("Page Set&up...", new EventHandler(filePageSetupMenuItem_Click)); MenuItem filePrintPreviewMenuItem = new MenuItem("Print Pre&view", new EventHandler(filePrintPreviewMenuItem_Click)); MenuItem filePrintMenuItem = new MenuItem("&Print...", new EventHandler(filePrintMenuItem_Click), Shortcut.CtrlP); fileMenuItem.MenuItems.Add(filePageSetupMenuItem); fileMenuItem.MenuItems.Add(filePrintPreviewMenuItem); fileMenuItem.MenuItems.Add(filePrintMenuItem); this.Menu = new MainMenu(); this.Menu.MenuItems.Add(fileMenuItem); printDoc.PrintPage += new PrintPageEventHandler( printDoc_PrintPage); } // -------------- event handlers --------------------------------- private void filePrintMenuItem_Click(Object sender , EventArgs e) { printDoc.Print(); } private void filePrintPreviewMenuItem_Click(Object sender , EventArgs e) { } private void filePageSetupMenuItem_Click(Object sender , EventArgs e) { } private void printDoc_PrintPage(Object sender , PrintPageEventArgs e) { String textToPrint = ".NET Printing is easy"; Font printFont = new Font("Courier New", 12); e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, 0, 0); } //-------------- end of event handlers ------------------------------- [STAThread] static void Main() { Application.Run(new Form1()); } } }이제 폼을 실행하고 Ctrl+P를 누르면(물론, 여러분이 사용하고 있는 컴퓨터에 프린터가 연결되고 있고, 올바른 드라이버가 설치되어 있다고 가정한 것임), 프린터는 ".NET printing is easy."라는 문자열을 인쇄할 것이다. 쉽지 않은가?
private void filePrintMenuItem_Click(Object sender , EventArgs e) { PrintDialog dlg = new PrintDialog(); dlg.Document = printDoc; if (dlg.ShowDialog() == DialogResult.OK) { printDoc.Print(); } }목록 3의 filePrintMenuItem_Click 이벤트 핸들러는 사용자가 인쇄 대화 상자의 OK 버튼을 클릭하는 경우에만 프린터에 출력을 보낸다. 그러나, 사용자가 인쇄 대화 상자에서 변경한 설정은 여러분이 이러한 옵션에 대한 코드를 작성하지 않는 한 적용되지 않는다.
private PageSettings pgSettings = new PageSettings();
private void filePrintMenuItem_Click(Object sender , EventArgs e) { printDoc.DefaultPageSettings = pgSettings; PrintDialog dlg = new PrintDialog(); dlg.Document = printDoc; if (dlg.ShowDialog() == DialogResult.OK) { printDoc.Print(); } }
private void filePageSetupMenuItem_Click(Object sender , EventArgs e) { PageSetupDialog pageSetupDialog = new PageSetupDialog(); pageSetupDialog.PageSettings = pgSettings; pageSetupDialog.AllowOrientation = true; pageSetupDialog.AllowMargins = true; pageSetupDialog.ShowDialog(); }
private void printDoc_PrintPage(Object sender , PrintPageEventArgs e) { String textToPrint = ".NET Printing is easy"; Font printFont = new Font("Courier New", 12); int leftMargin = e.MarginBounds.Left; int topMargin = e.MarginBounds.Top; e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin); }
using System; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace CSharpPrinting { public class Form1 : System.Windows.Forms.Form { private PrintDocument printDoc = new PrintDocument(); private PageSettings pgSettings = new PageSettings(); public Form1() { MenuItem fileMenuItem = new MenuItem("&File"); MenuItem filePageSetupMenuItem = new MenuItem( "Page Set&up...", new EventHandler( filePageSetupMenuItem_Click)); MenuItem filePrintPreviewMenuItem = new MenuItem( "Print Pre&view", new EventHandler( filePrintPreviewMenuItem_Click)); MenuItem filePrintMenuItem = new MenuItem("&Print...", new EventHandler(filePrintMenuItem_Click), Shortcut.CtrlP); fileMenuItem.MenuItems.Add(filePageSetupMenuItem); fileMenuItem.MenuItems.Add(filePrintPreviewMenuItem); fileMenuItem.MenuItems.Add(filePrintMenuItem); this.Menu = new MainMenu(); this.Menu.MenuItems.Add(fileMenuItem); printDoc.PrintPage += new PrintPageEventHandler( printDoc_PrintPage); } // -------------- event handlers ---------------------- private void filePrintMenuItem_Click(Object sender , EventArgs e) { printDoc.DefaultPageSettings = pgSettings; PrintDialog dlg = new PrintDialog(); dlg.Document = printDoc; if (dlg.ShowDialog() == DialogResult.OK) { printDoc.Print(); } } private void filePrintPreviewMenuItem_Click(Object sender , EventArgs e) { } private void filePageSetupMenuItem_Click(Object sender , EventArgs e) { PageSetupDialog pageSetupDialog = new PageSetupDialog(); pageSetupDialog.PageSettings = pgSettings; pageSetupDialog.AllowOrientation = true; pageSetupDialog.AllowMargins = true; pageSetupDialog.ShowDialog(); } private void printDoc_PrintPage(Object sender , PrintPageEventArgs e) { String textToPrint = ".NET Printing is easy"; Font printFont = new Font("Courier New", 12); int leftMargin = e.MarginBounds.Left; int topMargin = e.MarginBounds.Top; e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin); } //-------------- end of event handlers ------------------ [STAThread] static void Main() { Application.Run(new Form1()); } } }프린터 설정
private PrinterSettings prtSettings = new PrinterSettings();
private void filePageSetupMenuItem_Click(Object sender , EventArgs e) { PageSetupDialog pageSetupDialog = new PageSetupDialog(); pageSetupDialog.PageSettings = pgSettings; pageSetupDialog.PrinterSettings = prtSettings; pageSetupDialog.AllowOrientation = true; pageSetupDialog.AllowMargins = true; pageSetupDialog.ShowDialog(); }
private void filePrintPreviewMenuItem_Click(Object sender , EventArgs e) { PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.Document = printDoc; dlg.ShowDialog(); }인쇄 미리 보기 대화 상자는 System.Windows.Forms.PrintPreviewDialog 클래스로 표현되어 있다. 인자를 사용하지 않는 생성자를 사용해서 인쇄 미리보기 대화 상자의 인스턴스를 생성할 수 있다. 인스턴스를 생성한 다음에 인쇄를 하기 위해 PrintDocument 객체를 PrintPreviewDialog 객체의 Document 속성에 할당한다. ShowDialog 메소드가 호출될 때 PrintDocument 객체의 PrintPage 이벤트가 호출된다. 그러나 출력은 프린터에 보내지지 않고 PrintPreviewDialog 객체로 보내진다. 전체 코드는 목록 5에 나와 있다.
using System; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace CSharpPrinting { public class Form1 : System.Windows.Forms.Form { private PrintDocument printDoc = new PrintDocument(); private PageSettings pgSettings = new PageSettings(); private PrinterSettings prtSettings = new PrinterSettings(); public Form1() { MenuItem fileMenuItem = new MenuItem("&File"); MenuItem filePageSetupMenuItem = new MenuItem("Page Set&up...", new EventHandler(filePageSetupMenuItem_Click)); MenuItem filePrintPreviewMenuItem = new MenuItem("Print Pre&view", new EventHandler(filePrintPreviewMenuItem_Click)); MenuItem filePrintMenuItem = new MenuItem("&Print...", new EventHandler(filePrintMenuItem_Click), Shortcut.CtrlP); fileMenuItem.MenuItems.Add(filePageSetupMenuItem); fileMenuItem.MenuItems.Add(filePrintPreviewMenuItem); fileMenuItem.MenuItems.Add(filePrintMenuItem); this.Menu = new MainMenu(); this.Menu.MenuItems.Add(fileMenuItem); printDoc.PrintPage += new PrintPageEventHandler( printDoc_PrintPage); } // -------------- event handlers ------------------------------------ private void filePrintMenuItem_Click(Object sender , EventArgs e) { printDoc.DefaultPageSettings = pgSettings; PrintDialog dlg = new PrintDialog(); dlg.Document = printDoc; if (dlg.ShowDialog() == DialogResult.OK) { printDoc.Print(); } } private void filePrintPreviewMenuItem_Click(Object sender , EventArgs e) { PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.Document = printDoc; dlg.ShowDialog(); } private void filePageSetupMenuItem_Click(Object sender , EventArgs e) { PageSetupDialog pageSetupDialog = new PageSetupDialog(); pageSetupDialog.PageSettings = pgSettings; pageSetupDialog.PrinterSettings = prtSettings; pageSetupDialog.AllowOrientation = true; pageSetupDialog.AllowMargins = true; pageSetupDialog.ShowDialog(); } private void printDoc_PrintPage(Object sender , PrintPageEventArgs e) { String textToPrint = ".NET Printing is easy"; Font printFont = new Font("Courier New", 12); int leftMargin = e.MarginBounds.Left; int topMargin = e.MarginBounds.Top; e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin); } //-------------- end of event handlers ----------------------------- [STAThread] static void Main() { Application.Run(new Form1()); } } }결론
이전 글 : 자바 개발자를 위한 최고의 전문지 JDJ 선정, 2002 BEST JAVA !
다음 글 : PHP 레퍼런스 고급편
최신 콘텐츠