2016-07-06 2 views
0

몇 가지 컨트롤이있는 그룹 상자가있어서 프린터로 보내려고합니다.groupbox의 내용을 프린터로 보내는 방법

이 코드는 groupbox에서 bmp 파일을 작성합니다. 버튼 클릭시 어떻게 프린터로 보낼 수 있습니까?

Private Sub Doc_PrintPage(sender As Object, e As PrintPageEventArgs) 
    Dim x As Single = e.MarginBounds.Left 
    Dim y As Single = e.MarginBounds.Top 
    Dim bmp As New Bitmap(Me.GroupBox1.Width, Me.GroupBox1.Height) 
    Me.GroupBox1.DrawToBitmap(bmp, New Rectangle(0, 0, Me.GroupBox1.Width, Me.GroupBox1.Height)) 
    e.Graphics.DrawImage(DirectCast(bmp, Image), x, y) 
End Sub 

내가 버튼을 클릭 이벤트에 있습니다 조언 후

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim doc As New PrintDocument() 
    doc = Doc_PrintPage() 
    Dim dlgSettings As New PrintDialog() 
    dlgSettings.Document = doc 
    If dlgSettings.ShowDialog() = DialogResult.OK Then 
     doc.Print() 
    End If 
End Sub 

최종 작업 코드 :

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    BMP = New Bitmap(GroupBox1.Width, GroupBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb) 
    GroupBox1.DrawToBitmap(BMP, New Rectangle(0, 0, GroupBox1.Width, GroupBox1.Height)) 
    Dim pd As New PrintDocument 
    Dim pdialog As New PrintDialog 
    AddHandler pd.PrintPage, (Sub(s, args) 
            args.Graphics.DrawImage(BMP, 0, 0) 
            args.HasMorePages = False 
           End Sub) 
    pdialog.ShowDialog() 
    pd.PrinterSettings.PrinterName = pdialog.PrinterSettings.PrinterName 
    pd.Print() 
End Sub 

답변

0

아이디어는 당신이 Print 메소드를 호출하는 PrintDocument 객체를 가지고있다, PrintPage 이벤트를 발생 시키면 해당 이벤트를 처리하고 핸들러 메서드에서 GDI +를 사용하여 인쇄 할 내용을 그립니다. 따라서이 줄을 제거해야합니다.

doc = Doc_PrintPage() 

무엇을 할 수 있습니까? 메소드의 결과를 PrintDocument 변수에 지정하려고합니다. 이를 의미있게하기 위해서 메서드는 PrintDocument 개체를 반환해야합니다. 그렇지 않은 개체는 반환해야합니다. 당신이해야 할 것은 당신의 PrintDocumentPrintPage 이벤트를 처리하는 방법을 등록 할 수 있습니다 :

AddHandler doc.PrintPage, AddressOf Doc_PrintPage 

당신은 당신이뿐만 아니라 핸들러를 제거 할 필요가 있다고 할 경우

. 더 나은 옵션은 디자이너의 양식에 모든 인쇄 오브젝트를 추가 한 다음 Button 또는 TextBox에 대한 이벤트 핸들러를 작성하는 것과 마찬가지로 PrintDocument에 대한 PrintPage 이벤트 핸들러를 작성하는 것입니다.

인쇄에 대한 자세한 내용은 this을 참조하십시오.

+0

내 눈이 열렸습니다. 유용한 링크. 질문 영역에 게시 된 최종 작업 코드. –