how to print Test-page on default Printer?

后端 未结 3 605
误落风尘
误落风尘 2021-01-25 14:40

how to print Test-page on default Printer

using C# Winform Code ?

thank\'s in advance

相关标签:
3条回答
  • 2021-01-25 15:05

    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();
        }
    
    }
    
    0 讨论(0)
  • 2021-01-25 15:21

    As ho1 said the WMI can accommodate you:

    ManagementClass processClass = new ManagementClass("Win32_Printer");
    ManagementBaseObject outP = processClass.InvokeMethod("PrintTestPage", null);
    if (Convert.ToUInt32(outP["ReturnValue"]) != 0)
    {
        MessageBox.Show("Failed to print test page.");
    }
    

    Whenever you call print on a PrintDocument without specifying a printer it will of course use the default:

    PrintDocument doc = new PrintDocument();
    doc.Print(); // will print a blank page
    
    0 讨论(0)
  • 2021-01-25 15:29

    Use WMI, specifically, the PrintTestPage method of the Win32_Printer class.

    See this MSDN article for how to execute a WMI method from C#: How To: Execute a Method

    0 讨论(0)
提交回复
热议问题