【问题标题】:How can I get the printer HDC如何获得打印机 HDC
【发布时间】:2011-01-04 06:35:06
【问题描述】:

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

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

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

【问题讨论】:

    标签: vb.net printing vb6-migration powerpacks


    【解决方案1】:

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

    【讨论】:

    • 当我创建一个新的PrinterSettings 对象时,它实际上是使用我之前设置的兼容性Printer 对象中的值进行初始化的。因此,这实际上与调用Printer.hdc 相同!非常感谢!
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2020-12-14
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-26
        相关资源
        最近更新 更多