Form2.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. }
  39. private string getRequestPayload(HttpListenerContext req)
  40. {
  41. return "";
  42. }
  43. private static void httpPostRequestHandle()
  44. {
  45. while (true)
  46. {
  47. HttpListenerContext requestContext = httpPostRequest.GetContext();
  48. Thread threadsub = new Thread(new ParameterizedThreadStart((requestcontext) =>
  49. {
  50. HttpListenerContext request = (HttpListenerContext)requestcontext;
  51. //Response
  52. request.Response.StatusCode = 200;
  53. request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
  54. request.Response.ContentType = "application/json";
  55. requestContext.Response.ContentEncoding = Encoding.UTF8;
  56. string json = "";
  57. if (requestContext.Request.Url.PathAndQuery == "/write")
  58. {
  59. Stream stream = requestContext.Request.InputStream;
  60. var memoryStream = new MemoryStream();
  61. //将基础流写入内存流
  62. const int bufferLength = 2 * 1024 * 1024;
  63. byte[] buffera = new byte[bufferLength];
  64. int actual = stream.Read(buffera, 0, bufferLength);
  65. if (actual > 0)
  66. {
  67. memoryStream.Write(buffera, 0, actual);
  68. }
  69. memoryStream.Position = 0;
  70. string req = System.Text.Encoding.UTF8.GetString(buffera);
  71. if (req.Contains("\"" + "chooseprintername" + "\""))
  72. {
  73. req = req.Remove(req.IndexOf("device"), req.IndexOf("data") - 2);
  74. lock ("A")
  75. {
  76. PrintHandler.vendorZplPrint(req);
  77. }
  78. }
  79. else
  80. {
  81. PrintHandler.zplprint(req);
  82. }
  83. }
  84. else if (requestContext.Request.Url.PathAndQuery == "/default?type=printer")
  85. {
  86. PrintDocument PrintDoc = new PrintDocument();
  87. string sDefault = PrintDoc.PrinterSettings.PrinterName;//默认打印机名
  88. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  89. json = new JObject(
  90. new JObject(
  91. new JProperty("deviceType", "printer"),
  92. new JProperty("uid", sDefault),
  93. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  94. new JProperty("name", sDefault),
  95. new JProperty("connection", "driver"),
  96. new JProperty("version", 2),
  97. new JProperty("manufacturer", "Zebra Technologies")
  98. )
  99. ).ToString();
  100. }
  101. else if (requestContext.Request.Url.PathAndQuery == "/available")
  102. {
  103. List<string> PrintC = new List<string>();
  104. foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  105. {
  106. PrintC.Add(sPrint);
  107. }
  108. json = new JObject(
  109. new JProperty(
  110. "printer", new JArray
  111. (
  112. from sPrint in PrintC
  113. select new JObject(
  114. new JObject(
  115. new JProperty("deviceType", "printer"),
  116. new JProperty("uid", sPrint),
  117. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  118. new JProperty("name", sPrint),
  119. new JProperty("connection", "driver"),
  120. new JProperty("version", 2),
  121. new JProperty("manufacturer", "Zebra Technologies")
  122. )
  123. )
  124. )
  125. )
  126. ).ToString();
  127. }
  128. else
  129. {
  130. json = Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" });
  131. }
  132. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(json);
  133. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  134. request.Response.ContentLength64 = buffer.Length;
  135. var output = request.Response.OutputStream;
  136. output.Write(buffer, 0, buffer.Length);
  137. output.Close();
  138. }));
  139. threadsub.Start(requestContext);
  140. }
  141. }
  142. private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  143. {
  144. Close();
  145. }
  146. private void 打开根目录ToolStripMenuItem_Click(object sender, EventArgs e)
  147. {
  148. System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory);
  149. }
  150. private void Form2_FormClosing(object sender, FormClosingEventArgs e)
  151. {
  152. 提示.ShowBalloonTip(30, "提示", "程序关闭", ToolTipIcon.Info);
  153. if (ThrednHttpPostRequest.IsAlive)
  154. ThrednHttpPostRequest.Abort();
  155. if (httpPostRequest.IsListening)
  156. httpPostRequest.Close();
  157. Dispose();
  158. }
  159. private void Form2_Shown(object sender, EventArgs e)
  160. {
  161. Visible = false;
  162. }
  163. }
  164. }