2010-03-10 44 views

回答

3

VB6和.NET处理打印的方式完全不同。这些例子是最简单的例子,只是为了让你了解这个过程。

在VB6你一步控制打印机的步骤:

Private Sub PrintMyPicture() 

    'Place to store my picture 
    Dim myPicture As IPictureDisp 
    'Load the picture into the variable 
    Set myPicture = LoadPicture("C:\temp\myPictureFile.bmp") 

    'Draw the picture on the printer, just off the edge of the page 
    Printer.PaintPicture myPicture, 10, 10 

    'Done printing! 
    Printer.EndDoc 
End Sub 

瞧,您的图片会默认打印机的出。 PaintPicture方法接受宽度,高度和一些其他参数来帮助获取图像,Printer对象为您提供有关打印机的各种信息。

在.NET中,它有其他方式。您开始打印,并且打印机将为每个页面引发一个事件,直到您告诉其停止。每一页的事件给你的图形对象时,您可以使用所有标准System.Drawing中的类和方法的借鉴:

'Class variable 
Private WithEvents printer As System.Drawing.Printing.PrintDocument 


' Kick off the printing process. This will cause the printer_PrintPage event chain to start firing. 
Public Sub PrintMyPicture() 
'Set up the printer 

    printer.PrinterSettings.PrinterName = "MyPrinterNameInPrintersAndFaxes" 
    printer.Print() 
End Sub 

'This event will keep firing while e.HasMorePages = True. 
Private Sub printer_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printer.PrintPage 



    'Load the picture 
    Dim myPicture As System.Drawing.Image = System.Drawing.Image.FromFile("C:\temp\myPictureFile.bmp") 

    'Print the Image. 'e' is the Print events that the printer provides. In e is a graphics object on hwich you can draw. 
    '10, 10 is the position to print the picture. 
    e.Graphics.DrawImage(myPicture, 10, 10) 

    'Clean up 
    myPicture.Dispose() 
    myPicture = Nothing 

    'Tell the printer that there are no more pages to print. This will cause the document to be finalised and come out of the printer. 
    e.HasMorePages = False 
End Sub 

同样,也有在的DrawImage多了很多参数和PrintDocument的对象。