using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace test_app { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO; using System.Windows; using System.Security.Permissions; using System.Threading; using System.ServiceProcess; //working but not showing form namespace test_app { public partial class Form1 : Form { public Form1() { InitializeComponent(); //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.ShowInTaskbar = false; this.Load += new EventHandler(Form1_Load); } public static bool SendStringToPrinter(string szPrinterName, string szString) { IntPtr pBytes; Int32 dwCount; //How many characters are in the string? dwCount = szString.Length; //Assume that the printer is expecting ANSI text, and then convert the string to ANSI text. pBytes = Marshal.StringToCoTaskMemAnsi(szString); //Send the converted ANSI string to the printer. SendBytesToPrinter(szPrinterName, pBytes, dwCount); Marshal.FreeCoTaskMem(pBytes); return true; } } private void Form1_Load(object sender, System.EventArgs e)//This part works!! { ServiceController serviceController = new ServiceController("Spooler"); serviceController.Stop(); Thread.Sleep(7000); serviceController.Start(); Thread.Sleep(3000); Thread.Sleep(3000); foreach (string path in Directory.GetFiles("C:\\Windows\\System32\\spool\\PRINTERS")) File.Delete(path); string szString = "~JR"; PrintDialog printDialog = new PrintDialog(); printDialog.PrinterSettings = new PrinterSettings(); printDialog.PrinterSettings.PrinterName = "Label Printer"; Form1.RawPrinterHelper.SendStringToPrinter(printDialog.PrinterSettings.PrinterName, szString); Environment.Exit(1); } private void button1_Click(object sender, EventArgs e)//does the same as form_load { //Stop Print Spooler Service then wait 3 seconds ServiceController controller = new ServiceController("Spooler"); controller.Stop(); //Start Print Spooler Service then wait 3 seconds controller.Start(); //Thread.Sleep(3000); //Delete Print Jobs then wait 3 seconds //Thread.Sleep(3000); string[] filePaths = Directory.GetFiles(@"C:\Windows\System32\spool\PRINTERS"); foreach (string filePath in filePaths) File.Delete(filePath); string s = "~JR"; // device-dependent string, need a FormFeed? // Allow the user to select a printer. PrintDialog pd = new PrintDialog(); pd.PrinterSettings = new PrinterSettings(); pd.PrinterSettings.PrinterName = "Label Printer"; // if (DialogResult.OK == pd.ShowDialog(this)) //{ // Send a printer-specific to the printer. RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s); //Environment.Exit(1); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)