다음을 통해 공유


방법: 폼 인쇄 미리 보기

업데이트: 2007년 11월

이 예제에서는 현재 폼의 복사본을 인쇄 미리 보기로 보는 방법을 보여 줍니다.

예제

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, 
    int nXDest, int nYDest, int nWidth, int nHeight, 
    IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
    Graphics mygraphics = this.CreateGraphics();
    Size s = this.Size;
    memoryImage = new Bitmap(s.Width, s.Height, 
        mygraphics);
    Graphics memoryGraphics = Graphics.FromImage(
        memoryImage);
    IntPtr dc1 = mygraphics.GetHdc();
    IntPtr dc2 = memoryGraphics.GetHdc();
    BitBlt(dc2, 0, 0, this.ClientRectangle.Width,
        this.ClientRectangle.Height, dc1, 0, 0, 
        13369376);
    mygraphics.ReleaseHdc(dc1);
    memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object 
    sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, 
    System.EventArgs e)
{
    CaptureScreen();
    printPreviewDialog1.Show();
}

코드 컴파일

이 예제에는 다음 사항이 필요합니다.

  • PrintPage 이벤트 처리기가 있는 printDocument1이라는 PrintDocument 구성 요소

  • Document 속성이 printDocument1로 설정된 printPreviewDialog1이라는 PrintPreviewDialog 구성 요소

  • Click 이벤트 처리기가 있는 printButton이라는 Button

예제 코드가 기존의 이벤트 처리기를 대체합니다. printButton을 클릭하면 폼의 인쇄 미리 보기가 표시됩니다.

강력한 프로그래밍

다음 조건에서 예외가 발생합니다.

  • 프린터에 액세스할 수 있는 권한이 없는 경우

  • 비관리 코드를 사용할 수 있는 권한이 없는 경우

  • 설치된 프린터가 없는 경우

  • 인쇄 미리 보기 대화 상자가 이미 사라진 경우 (인쇄 미리 보기 대화 상자를 닫은 경우)

보안

이 예제를 실행하려면 비관리 코드를 실행하고 프린터에 액세스할 수 있는 권한을 가지고 있어야 합니다.

참고 항목

개념

Visual C#에서 사용자 인터페이스 디자인

기타 리소스

Windows Forms 사용자 지정, 표시 및 인쇄

Visual C# 둘러보기