2016-07-06 113 views
0

我有一個有一些控件的組框,並且我想將它發送給打印機。如何將羣組內容的內容發送到打印機

我有這個代碼即構建從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

的想法是,你有一個PrintDocument對象,調用其Print方法,它會引發它的PrintPage事件,您處理該事件,並在處理程序方法中使用GDI +繪製要打印的內容。所以,你需要擺脫這條線:

doc = Doc_PrintPage() 

這可能會做什麼?您正試圖將方法的結果分配給變量PrintDocument。爲了使其具有意義,該方法必須返回一個PrintDocument對象,但事實並非如此。你需要做的是註冊一個方法來處理事件PrintPagePrintDocument的:

AddHandler doc.PrintPage, AddressOf Doc_PrintPage 

如果你這樣做,那麼你需要刪除的處理程序也是如此。更好的選擇是在設計器中將所有打印對象添加到表單中,然後爲PrintDocument創建一個PrintPage事件處理程序,就像您爲ButtonTextBox創建事件處理程序一樣。

有關打印的更多信息,您可能會發現this有用。

+0

睜開了眼睛。有用的鏈接。在問題區發佈最終工作代碼。 –