如何在默认打印机上打印测试页?

如何在默认打印机上打印测试页?

问题描述:

如何在默认打印机上打印测试页

how to print Test-page on default Printer

使用 C# Winform 代码?

using C# Winform Code ?

提前致谢

要生成内置的 Windows 测试页,您还可以对 PrintUI.dll 使用 p/invoke.这是一个简单的类,可让您执行此操作:

To generate the built-in Windows test page, you can also use p/invoke against PrintUI.dll. Here's a simple class which lets you do this:

public static class PrintTestPageHelper
{
    [DllImport("printui.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern void PrintUIEntryW(IntPtr hwnd, 
        IntPtr hinst, string lpszCmdLine, int nCmdShow);

    /// <summary>
    /// Print a Windows test page.
    /// </summary>
    /// <param name="printerName">
    /// Format: \\Server\printer name, for example:
    /// \\myserver\sap3
    /// </param>
    public static void Print(string printerName)
    {
        var printParams = string.Format(@"/k /n{0}", printerName);
        PrintUIEntryW(IntPtr.Zero, IntPtr.Zero, printParams, 0);
    }
}

public class Program
{

    static void Main(string[] args)
    {
        PrintTestPageHelper.Print(@"\\printserver.code4life.com\sap3");

        Console.WriteLine("Press enter to exit.");
        Console.ReadLine();
    }

}