如何获得打印机 HDC

发布于 2024-08-17 23:37:43 字数 424 浏览 7 评论 0原文

我有一个用 C++ 编写的 COM 组件,具有打印功能。此打印功能将打印机 hDC 作为参数,其中包括用于打印的所有设置。以前,这是从 VB6 代码调用的,在设置 Printer 对象上的所有内容后,Printer.hdc 将在此处工作。

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

谁能告诉我如何获得这个hdc? 此 hdc 与 System.Drawing.Printing.PrinterSettings 对象上的 GetHdevmode() 相同吗?

I have a COM component written in C++ that has a Print function. This print function takes the Printer hDC as a parameter that includes all settings to use for the print. Previously, this was called from VB6 code, and Printer.hdc would work here after setting everything on the Printer object.

The code was converted from VB6 to VB.NET, and I have figured out most of things that I need to do. The old Printer object is available through the Microsoft.VisualBasic.PowerPacks.Printing.Compability.VB6.Printer class, but the old hdc property is not supported here.

Can anyone tell me how to get this hdc?
Is this hdc the same as GetHdevmode() on a System.Drawing.Printing.PrinterSettings object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼睛会笑 2024-08-24 23:37:43

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

You can get one out of the Graphics object returned by PrinterSettings.CreateMeasurementGraphics(). Use the Graphics.GetHdc() method. Don't forget ReleaseHdc() after printing.

岁吢 2024-08-24 23:37:43

hdc 与 getdevmode 不同,但您可以在 .net 中执行所有操作,而无需使用 hdc。如果使用旧代码可以节省时间,您可以从图形对象中获取 hdc 并按照 nobugz 的答案使用它。但是,如果您有打印机的图形对象,则直接绘制到图形对象并完全跳过 hdc 可能会更简单。

Hdc is not the same as getdevmode, but you can do everything in .net without using hdc. If it saves time using the old code, you can get the hdc from the graphics object and use it as in nobugz's answer. But if you have a graphics object for the printer, it might be simpler to draw directly to the graphics object and skip the hdc altogether.

美人骨 2024-08-24 23:37:43

这是与 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

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

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

Here's a similar approach to the one suggested by Hans but it uses a form control. If you are using a form control anyway, this might be a cleaner approach.

Place a PrintDocument from the Windows Forms toolbox to your form.

Then add the following code to handle the print page (as an example):

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文