Windows Forms 프로그래밍에서는 인쇄 서비스 외에 인쇄 미리 보기를 제공하는 것이 일반적입니다. 애플리케이션에 인쇄 미리 보기를 간편하게 추가할 수 있는 방법은 PrintPreviewDialog 컨트롤을 사용하고, 파일 인쇄를 위한 PrintPage 이벤트 처리 논리를 결합하는 것입니다.
PrintPreviewDialog 컨트롤을 사용하여 텍스트 문서를 미리 보려면
Visual Studio에서 솔루션 탐색기 창을 사용하고 인쇄할 양식을 두 번 클릭합니다. 비주얼 디자이너가 열립니다.
도구 상자 창에서 PrintDocument 구성 요소와 PrintPreviewDialog 구성 요소를 두 번 클릭하여 폼에 추가합니다.
폼에
Button
을(를) 추가하거나 이미 폼에 있는 단추를 사용합니다.양식의 비주얼 디자이너에서 단추를 선택합니다. 속성 창에서 이벤트 필터 단추를 선택한 다음,
Click
이벤트를 두 번 클릭하여 이벤트 처리기를 생성합니다.Click
이벤트 코드가 표시되어야 합니다. 이벤트 처리기의 범위 외부에서documentContents
및stringToPrint
라는 클래스에 두 개의 비공개 문자열 변수를 추가합니다.// 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="";
' 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
Click
이벤트 처리기 코드로 돌아가 DocumentName 속성을 인쇄할 문서로 설정하고 문서를 연 다음 이전에 추가한 문자열까지 문서 내용을 읽습니다.string docName = "testPage.txt"; string docPath = @"C:\"; string fullPath = System.IO.Path.Combine(docPath, docName); printDocument1.DocumentName = docName; stringToPrint = System.IO.File.ReadAllText(fullPath);
Dim docName As String = "testPage.txt" Dim docPath As String = "C:\Users\v-rsatao\Desktop\" Dim fullPath As String = System.IO.Path.Combine(docPath, docName) PrintDocument1.DocumentName = docName stringToPrint = System.IO.File.ReadAllText(fullPath)
문서를 인쇄할 때와 마찬가지로, PrintPage 이벤트 처리기에서 Graphics 클래스의 PrintPageEventArgs 속성과 파일 내용을 사용하여 페이지당 줄 수를 계산하고 문서 내용을 렌더링합니다. 각 페이지가 그려진 후 마지막 페이지인지 확인하고
PrintPageEventArgs
의 HasMorePages 속성을 그에 맞게 설정합니다.PrintPage
가HasMorePages
가 될 때까지false
이벤트가 발생합니다. 문서 렌더링이 완료되면 렌더링할 문자열을 다시 설정합니다. 또한PrintPage
이벤트가 해당 이벤트 처리 메서드에 연결되어 있는지 확인합니다.비고
애플리케이션에서 인쇄를 구현한 경우 5단계와 6단계가 이미 완료되었을 수 있습니다.
다음 코드 예제에서 이벤트 처리기는 "testPage.txt" 파일을 폼에 사용된 것과 동일한 글꼴로 인쇄하는 데 사용됩니다.
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; }
Private 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 End Sub
Document 컨트롤의 PrintPreviewDialog 속성을 폼의 PrintDocument 구성 요소로 설정합니다.
printPreviewDialog1.Document = printDocument1;
PrintPreviewDialog1.Document = PrintDocument1
ShowDialog 컨트롤에 대해 PrintPreviewDialog 메서드를 호출합니다. 다음 강조된 코드는 일반적으로 단추의 이벤트 처리 메서드에서 ShowDialog를 호출합니다. ShowDialog 를 호출하면 PrintPage 이벤트가 발생하고 출력이
PrintPreviewDialog
컨트롤에 렌더링됩니다. 사용자가 대화 상자에서 인쇄 아이콘을 선택하면PrintPage
이벤트가 다시 발생하여 미리 보기 대화 상자 대신 프린터로 출력을 보냅니다. 따라서 4단계에서 렌더링 프로세스가 끝날 때 문자열이 다시 설정됩니다.다음 코드 예제에서는 폼의 단추에 대한 Click 이벤트 처리 메서드를 보여 줍니다. 이 이벤트 처리 메서드는 문서를 읽고 인쇄 미리 보기 대화 상자를 표시하는 메서드를 호출합니다.
private void Button1_Click(object sender, EventArgs e) { string docName = "testPage.txt"; string docPath = @"C:\"; string fullPath = System.IO.Path.Combine(docPath, docName); printDocument1.DocumentName = docName; stringToPrint = System.IO.File.ReadAllText(fullPath); printPreviewDialog1.Document = printDocument1; printPreviewDialog1.ShowDialog(); }
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim docName As String = "testPage.txt" Dim docPath As String = "C:\Users\v-rsatao\Desktop\" Dim fullPath As String = System.IO.Path.Combine(docPath, docName) PrintDocument1.DocumentName = docName stringToPrint = System.IO.File.ReadAllText(fullPath) PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() End Sub
참고하십시오
.NET Desktop feedback