Form2.cs 8.7 KB

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