2016-07-06 11 views
0

グループボックスにいくつかのコントロールがあり、プリンタに送信したいのですが。グループボックスの内容をプリンタに送信する方法

このコードでは、グループボックスから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 
    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

アイデアは、あなたがPrintDocumentオブジェクトを持っているということです、あなたはそのPrintメソッドを呼び出します。アドバイスの後

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 

最終作業コードPrintPageイベントを発生させると、そのイベントを処理し、ハンドラメソッドではGDI +を使用して印刷するものを描画します。だから、あなたはこの行を取り除く必要があります:

doc = Doc_PrintPage() 

何ができているのでしょうか?メソッドの結果を変数PrintDocumentに代入しようとしています。これを意味のあるものにするには、メソッドはPrintDocumentオブジェクトを返す必要があります。あなたは、あなたが同様のハンドラを削除する必要があることを行う場合

AddHandler doc.PrintPage, AddressOf Doc_PrintPage 

:何をする必要があなたのPrintDocumentPrintPageイベントを処理するためにそのメソッドを登録しています。より良いオプションは、デザイナーのフォームにすべての印刷オブジェクトを追加し、ButtonまたはTextBoxのイベントハンドラーを作成するのと同じようにPrintDocumentPrintPageイベントハンドラーを作成することです。

印刷の詳細については、thisが便利です。

+0

私の目を開いた。有用なリンク。問題の領域に掲載された最終作業コード。 –

関連する問題