Form2.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. lock ("B")
  82. {
  83. PrintHandler.zplprint(req);
  84. }
  85. }
  86. }
  87. else if (requestContext.Request.Url.PathAndQuery == "/default?type=printer")
  88. {
  89. PrintDocument PrintDoc = new PrintDocument();
  90. string sDefault = PrintDoc.PrinterSettings.PrinterName;//默认打印机名
  91. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  92. json = new JObject(
  93. new JObject(
  94. new JProperty("deviceType", "printer"),
  95. new JProperty("uid", sDefault),
  96. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  97. new JProperty("name", sDefault),
  98. new JProperty("connection", "driver"),
  99. new JProperty("version", 2),
  100. new JProperty("manufacturer", "Zebra Technologies")
  101. )
  102. ).ToString();
  103. }
  104. else if (requestContext.Request.Url.PathAndQuery == "/available")
  105. {
  106. List<string> PrintC = new List<string>();
  107. foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  108. {
  109. PrintC.Add(sPrint);
  110. }
  111. json = new JObject(
  112. new JProperty(
  113. "printer", new JArray
  114. (
  115. from sPrint in PrintC
  116. select new JObject(
  117. new JObject(
  118. new JProperty("deviceType", "printer"),
  119. new JProperty("uid", sPrint),
  120. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  121. new JProperty("name", sPrint),
  122. new JProperty("connection", "driver"),
  123. new JProperty("version", 2),
  124. new JProperty("manufacturer", "Zebra Technologies")
  125. )
  126. )
  127. )
  128. )
  129. ).ToString();
  130. }
  131. else
  132. {
  133. json = Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" });
  134. }
  135. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(json);
  136. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  137. request.Response.ContentLength64 = buffer.Length;
  138. var output = request.Response.OutputStream;
  139. output.Write(buffer, 0, buffer.Length);
  140. output.Close();
  141. }));
  142. threadsub.Start(requestContext);
  143. }
  144. }
  145. private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  146. {
  147. ThrednHttpPostRequest.Abort();
  148. httpPostRequest.Close();
  149. this.Close();
  150. this.Dispose();
  151. }
  152. private void 打开根目录ToolStripMenuItem_Click(object sender, EventArgs e)
  153. {
  154. System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.BaseDirectory);
  155. }
  156. }
  157. }