我有一个用C++编写的COM组件,它具有打印功能。此打印功能将打印机hDC作为参数,其中包括用于打印的所有设置。以前,这是从VB6代码中调用的,在Printer对象上设置所有内容后,Printer.hdc将在此处工作。
代码从VB6转换为VB.NET,我已经弄清楚了我需要做的大部分事情。旧的打印机对象可通过Microsoft.VisualBasic.PowerPacks.Printing.Compability.VB6.Printer类使用,但此处不支持旧的hdc属性。
谁能告诉我怎么弄到这个hdc?这个hdc和System.Drawing.Printing.PrinterSettings对象上的GetHdevmode()是一样的吗?
发布于 2010-01-13 01:07:23
您可以从PrinterSettings.CreateMeasurementGraphics()返回的Graphics对象中获取一个。使用Graphics.GetHdc()方法。在打印之后,不要忘记ReleaseHdc()。
发布于 2010-01-13 14:37:46
hdc不同于getdevmode,但是你可以不使用Hdc在.net中做任何事情。如果使用旧代码可以节省时间,您可以从图形对象中获取hdc并使用它,就像nobugz的答案一样。但是,如果您有一个用于打印机的图形对象,则直接绘制到图形对象并完全跳过hdc可能会更简单。
发布于 2017-12-05 05:58:03
这是一个类似于suggested by Hans的方法,但它使用了一个表单控件。如果您正在使用表单控件,这可能是一种更简洁的方法。
将Windows Forms工具箱中的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)
https://stackoverflow.com/questions/2050686
复制相似问题