2010-01-12 100 views
1

我有用C++编写的具有打印功能的COM组件。此打印功能将打印机hDC作为参数,其中包含用于打印的所有设置。以前,这是从VB6代码调用的,Printer.hdc可以在设置Printer对象上的所有内容后生效。如何获得打印机HDC

该代码从VB6转换为VB.NET,并且我已经找出了我需要做的大部分事情。旧的Printer对象可通过Microsoft.VisualBasic.PowerPacks.Printing.Compability.VB6.Printer类获得,但此处不支持旧的hdc属性。

谁能告诉我如何得到这个hdc? 这个hdc与对象上的GetHdevmode()相同吗?

回答

2

您可以从PrinterSettings.CreateMeasurementGraphics()返回的Graphics对象中取出一个。使用Graphics.GetHdc()方法。打印后不要忘记ReleaseHdc()。

+0

当我创建一个新的'PrinterSettings'对象,它实际上是使用之前设置了值的兼容性“Printer”对象的值初始化的。因此,这与调用'Printer.hdc'相同! 非常感谢! – awe 2010-01-13 12:01:26

1

Hdc与getdevmode不同,但是您可以在不使用hdc的情况下在.net中执行所有操作。如果使用旧代码节省时间,您可以从图形对象获取hdc,并像nobugz的回答一样使用它。但是,如果您有打印机的图形对象,直接绘制到图形对象并跳过HDC可能会更简单。

0

下面是一个类似的方法,但是它使用了一个窗体控件suggested by Hans。如果你正在使用表单控件,这可能是一个更清洁的方法。

将Windows窗体工具箱中的PrintDocument放置到窗体中。

然后添加以下代码以处理打印页(作为例子):

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 
    Dim printerhdc As IntPtr = e.Graphics.GetHdc() 

    ' Do whatever you need to do to get the right image 
    XYZ.Load file(currentpagenumber) 
    XYZ.Render(printerhdc.ToInt64, 25, 25, Width, Height) 

    CurrentPageNumber += 1 

    If CurrentPageNumber < TotalPageCount Then 
    e.HasMorePages = True 
    Else 
    e.HasMorePages = False 
    End If 
    e.Graphics.ReleaseHdc(printerhdc) 
End Sub 

... 

'Gather all the files you need and put their names in an arraylist. 
'Then issue the print command 
PrintDocument1.Print 

' You've just printed your files 

源:http://www.vbforums.com/showthread.php?247493-Good-ol-Printer-hDC

(来源:http://www.vbforums.com/showthread.php?247493-Good-ol-Printer-hDC

相关问题