Form2.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using Microsoft.Win32;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Drawing.Printing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Windows.Forms;
  15. using UAS_PRINT.Properties;
  16. namespace UAS_PRINT
  17. {
  18. public partial class Form2 : Form
  19. {
  20. public Form2()
  21. {
  22. InitializeComponent();
  23. }
  24. private static HttpListener httpPostRequest = new HttpListener();
  25. private Thread ThrednHttpPostRequest;
  26. private void Form2_Load(object sender, EventArgs e)
  27. {
  28. string path = Application.ExecutablePath;
  29. RegistryKey rk = Registry.LocalMachine;
  30. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  31. rk2.SetValue("UAS_PRINT.exe", path);
  32. rk2.Close();
  33. rk.Close();
  34. httpPostRequest.Prefixes.Add("http://localhost:9100/");
  35. // httpPostRequest.Prefixes.Add("https://localhost:9100/");
  36. httpPostRequest.Start();
  37. ThrednHttpPostRequest = new Thread(new ThreadStart(httpPostRequestHandle));
  38. ThrednHttpPostRequest.Start();
  39. 提示.ShowBalloonTip(30, "提示", "程序启动", ToolTipIcon.Info);
  40. FTPtoolStripMenuItem.Checked = Settings.Default.ifuseftp;
  41. toolStripMenuItem1.Text = Settings.Default.Printnum.ToString();
  42. //string req = File.ReadAllText(@"C:\Users\Hcsy\Documents\Tencent Files\814802334\FileRecv\4.txt", Encoding.UTF8);
  43. //if (req.Contains("\"" + "chooseprintername" + "\""))
  44. //{
  45. // req = req.Remove(req.IndexOf("device"), req.IndexOf("data") - 2);
  46. // lock ("A")
  47. // {
  48. // PrintHandler.vendorZplPrint(req);
  49. // }
  50. //}
  51. //else
  52. //{
  53. // PrintHandler.zplprint(req);
  54. //}
  55. }
  56. private string getRequestPayload(HttpListenerContext req)
  57. {
  58. return "";
  59. }
  60. private static void httpPostRequestHandle()
  61. {
  62. while (true)
  63. {
  64. HttpListenerContext requestContext = httpPostRequest.GetContext();
  65. Thread threadsub = new Thread(new ParameterizedThreadStart((requestcontext) =>
  66. {
  67. HttpListenerContext request = (HttpListenerContext)requestcontext;
  68. //Response
  69. request.Response.StatusCode = 200;
  70. request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
  71. request.Response.ContentType = "application/json";
  72. requestContext.Response.ContentEncoding = Encoding.UTF8;
  73. string json = "";
  74. if (requestContext.Request.Url.PathAndQuery == "/write")
  75. {
  76. Stream stream = requestContext.Request.InputStream;
  77. StreamReader sr = new StreamReader(stream);
  78. string req = sr.ReadToEnd();
  79. sr.Close();
  80. if (req.Contains("\"" + "chooseprintername" + "\""))
  81. {
  82. req = req.Remove(req.IndexOf("device"), req.IndexOf("data") - 2);
  83. lock ("A")
  84. {
  85. PrintHandler.vendorZplPrint(req);
  86. }
  87. }
  88. else
  89. {
  90. PrintHandler.zplprint(req);
  91. }
  92. }
  93. else if (requestContext.Request.Url.PathAndQuery == "/default?type=printer")
  94. {
  95. PrintDocument PrintDoc = new PrintDocument();
  96. string sDefault = PrintDoc.PrinterSettings.PrinterName;//默认打印机名
  97. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  98. json = new JObject(
  99. new JObject(
  100. new JProperty("deviceType", "printer"),
  101. new JProperty("uid", sDefault),
  102. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  103. new JProperty("name", sDefault),
  104. new JProperty("connection", "driver"),
  105. new JProperty("version", 2),
  106. new JProperty("manufacturer", "Zebra Technologies")
  107. )
  108. ).ToString();
  109. }
  110. else if (requestContext.Request.Url.PathAndQuery == "/available")
  111. {
  112. List<string> PrintC = new List<string>();
  113. foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  114. {
  115. PrintC.Add(sPrint);
  116. }
  117. json = new JObject(
  118. new JProperty(
  119. "printer", new JArray
  120. (
  121. from sPrint in PrintC
  122. select new JObject(
  123. new JObject(
  124. new JProperty("deviceType", "printer"),
  125. new JProperty("uid", sPrint),
  126. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  127. new JProperty("name", sPrint),
  128. new JProperty("connection", "driver"),
  129. new JProperty("version", 2),
  130. new JProperty("manufacturer", "Zebra Technologies")
  131. )
  132. )
  133. )
  134. )
  135. ).ToString();
  136. }
  137. else
  138. {
  139. json = Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" });
  140. }
  141. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(json);
  142. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  143. request.Response.ContentLength64 = buffer.Length;
  144. var output = request.Response.OutputStream;
  145. output.Write(buffer, 0, buffer.Length);
  146. output.Close();
  147. }));
  148. threadsub.Start(requestContext);
  149. }
  150. }
  151. private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  152. {
  153. Close();
  154. }
  155. private void 打开根目录ToolStripMenuItem_Click(object sender, EventArgs e)
  156. {
  157. System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory);
  158. }
  159. private void Form2_FormClosing(object sender, FormClosingEventArgs e)
  160. {
  161. 提示.ShowBalloonTip(30, "提示", "程序关闭", ToolTipIcon.Info);
  162. if (ThrednHttpPostRequest.IsAlive)
  163. ThrednHttpPostRequest.Abort();
  164. if (httpPostRequest.IsListening)
  165. httpPostRequest.Close();
  166. Dispose();
  167. }
  168. private void Form2_Shown(object sender, EventArgs e)
  169. {
  170. Visible = false;
  171. }
  172. private void FTPtoolStripMenuItem_Click(object sender, EventArgs e)
  173. {
  174. Settings.Default.ifuseftp = FTPtoolStripMenuItem.Checked;
  175. Settings.Default.Save();
  176. }
  177. private void toolStripMenuItem1_TextChanged(object sender, EventArgs e)
  178. {
  179. try
  180. {
  181. int a = int.Parse(toolStripMenuItem1.Text);
  182. Settings.Default.Printnum = a;
  183. Settings.Default.Save();
  184. }
  185. catch {
  186. MessageBox.Show("录入数量须为整数");
  187. Settings.Default.Printnum = 1;
  188. Settings.Default.Save();
  189. toolStripMenuItem1.Text = "1";
  190. }
  191. }
  192. }
  193. }