【问题标题】:Printing Strings打印字符串
【发布时间】:2012-12-26 05:19:39
【问题描述】:

我正在开发一个需要打印字符串的程序,我已经准备好字符串,我只需要知道如何打印,我从来没有做过,所以我不知道从哪里开始想法? 这是我目前所拥有的,但我不知道从这里去哪里

PrintDocument output = new PrintDocument();
output.DocumentName = "Test Results";

【问题讨论】:

标签: c# string printing


【解决方案1】:

您对“打印”的定义非常模糊。在这部分互联网中最常见的打印含义是打印或显示文本到控制台或命令窗口。

因此,如果您想打印到控制台窗口,请使用 System.Console 类上的方法:

http://msdn.microsoft.com/en-us/library/system.console.writeline.aspx

例如:

String yourname = "Mr. Nice";
Console.WriteLine("Hello {0}", yourname);

会显示:

你好,尼斯先生

【讨论】:

  • 在打印机上打印出来
【解决方案2】:

您可以使用Console.WriteLine("text") 打印到标准输出。

看看字符串插值,使向字符串添加变量更容易。这是在 C# 6 中引入的,具有以下语法。

Console.Writeline($"I am {cool}"); 其中cool 是变量名:)

【讨论】:

    【解决方案3】:
    using System;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Windows.Forms;
    
    namespace Test
    {
    public class Print
    {
        PrintDocument myPrintDocument = new PrintDocument();
    
        public void ShowPrintDialog()
        {
            PrintDialog myDialog = new PrintDialog();
            myDialog.Document = myPrintDocument;
            if(myDialog.ShowDialog() == DialogResult.OK)
                Print();
        }
    
        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            e.Graphics.DrawString("Your String To Print In Document", this.Font, Brushes.Red, new PointF());
            e.HasMorePages = false;
        }
    }
    }
    

    【讨论】:

    • 也许我遗漏了一些东西,但我无法编译这个示例。您已经覆盖了OnPrintPage,但类Print 不继承自对象以外的任何东西,对象不公开虚拟方法OnPrintPage。此外,您的方法 Print() 未在任何地方声明。您在发布之前是否对此进行了测试?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    相关资源
    最近更新 更多