Form2.cs 7.8 KB

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