Form2.cs 8.1 KB

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