【问题标题】:C# substitute of VB 6 Printer ClassVB 6 打印机类的 C# 替代品
【发布时间】:2022-09-23 01:37:06
【问题描述】:

我需要一个 VB6 打印机类的替代品:

Microsoft.VisualBasic.PowerPacks.Printing.Compatibility

我在 C# 中检查了System.Drawing.Printing,但在 VB6 中没有直接替代 Printer 对象。

Set p = Printer 

任何链接都会更有帮助。

  • 你想达到什么目的? 1:1 翻译通常不是将 VB6 代码翻译为 VB.NET/C# 的最佳方式
  • @HelO\'Ween 我有一个非常旧的 Visual Basic 6 应用程序。它有一个代码来打印一些带有 FontSize、Margin 等属性的结果。我必须用 C# 重写这段代码。在想如果有上面提到的类的替代品,我的工作会变得简单。
  • 顺便说一句,有没有办法检查谁将此问题标记为已关闭以及出于什么原因?
  • System.Drawing.Printing 对我来说似乎是正确的。正如@HelO\'Ween 发布的那样,替换不会减少。我猜 VB6 有某种报告引擎,这在 .NET 中不可用。 StandardPrintController.OnStartPrint 基本上为您提供了一个 Graphics 对象来开始写入。
  • 使用 Microsoft.VisualBasic.PowerPacks.Printing.Compatibility 有什么问题?该课程的重点似乎正是您所要求的。

标签: c# .net vb6 vb6-migration


【解决方案1】:

这是说明如何使用Microsoft.VisualBasic.PowerPacks.Printing.Compatibility 的代码。这与我多年来在生产中使用的 VB6 代码几乎相同。应该注意的是,即使在 VB6 中,DeviceName 也是只读的,因此您需要迭代打印机列表以查找匹配项。

using Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6;

private void Test()
{
   //get the default printer
   Printer p = new Printer();
   MessageBox.Show(p.DeviceName);

   //print to non-default printer
   p = FindPrinter("some printer name");

   if (p != null)
   {
      p.CurrentX = 500;
      p.CurrentY = 500;
      p.Print("Some text out to specified printer");
      p.EndDoc();
   }
}

private Printer FindPrinter(string DeviceName)
{
   var Printers = new PrinterCollection();

   foreach (Printer p in Printers)
   {
      if (p.DeviceName == DeviceName) return p;
   }

   return null;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多