InterfaceChooser.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace PTMedInsuInterface.Forms
  12. {
  13. public partial class InterfaceChooser : Form
  14. {
  15. const int ITEM_WIDTH = 300;
  16. const int ITEM_HEIGTH = 80;
  17. const int ITEM_ROW_SPACE = 6;
  18. const int ITEM_COL_SPACE = 8;
  19. int CURRENT_PAGE = 1;
  20. int PAGE_SIZE;
  21. int ITEM_ROWS;
  22. int ITEM_COLS;
  23. DataTable dtInterface = new DataTable();
  24. DataTable dtOriginalInterface = new DataTable();
  25. public DataRow drSelectedInterface;
  26. public InterfaceChooser(JArray ja)
  27. {
  28. InitializeComponent();
  29. try
  30. {
  31. //需要将窗体的KeyPreview属性设置为true(如下图所示:),将系统传入的键值先传递给窗体,再传递给控件。MSDN关于KeyPreview的说明:如果窗体将接收所有键事件,则为 true;如果窗体上当前选定控件接收键事件,则为 false。默认为 false
  32. this.KeyPreview = true;
  33. ITEM_COLS = pnlClient.Width /(ITEM_WIDTH + ITEM_COL_SPACE);
  34. ITEM_ROWS= pnlClient.Height /(ITEM_HEIGTH + ITEM_ROW_SPACE);
  35. PAGE_SIZE = ITEM_COLS * ITEM_ROWS;
  36. CreateItemButton();
  37. //窗体的大小固定
  38. this.MaximizeBox = false;
  39. this.MinimizeBox = false;
  40. //默认可调整大小的边框模式
  41. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
  42. //固定的三维边框
  43. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
  44. //出现在屏幕正中央
  45. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  46. //获取医保接口清单
  47. dtOriginalInterface = (DataTable)ja.ToObject(typeof(DataTable));
  48. dtInterface = dtOriginalInterface;
  49. }
  50. catch (Exception ex)
  51. {
  52. throw new Exception("InterfaceChooser构造出错:" + ex.Message);
  53. }
  54. }
  55. private DataTable FilterInterfaceList()
  56. {
  57. string filterStr = tbLocate.Text;
  58. var dt1 = dtOriginalInterface.AsEnumerable()
  59. .Where(p => p.Field<string>("InterfaceName").Contains(filterStr));
  60. if (dt1.Count() > 0)
  61. {
  62. DataTable dt2 = dt1.CopyToDataTable<DataRow>();
  63. return dt2;
  64. }
  65. else
  66. return null;
  67. }
  68. /// <summary>
  69. /// 生成BUTTON
  70. /// </summary>
  71. private void CreateItemButton()
  72. {
  73. Sunny.UI.UIButton btn;
  74. for (int y = 1; y <=ITEM_ROWS; y++)
  75. {
  76. for (int x = 1; x <=ITEM_COLS; x++)
  77. {
  78. btn = new Sunny.UI.UIButton();
  79. btn.Parent = pnlClient;
  80. btn.Visible = false;
  81. btn.Name = "Item_" + ((y-1)*ITEM_COLS +x).ToString();
  82. btn.Width = ITEM_WIDTH;
  83. btn.Height = ITEM_HEIGTH;
  84. btn.Left = ITEM_COL_SPACE * x + (x - 1) * btn.Width;
  85. btn.Top = ITEM_ROW_SPACE * y + (y - 1) * btn.Height;
  86. //btn.Location = new System.Drawing.Point(btn.Left, btn.Top);
  87. btn.FillColor = Color.LightBlue;
  88. btn.ForeColor = Color.Black;
  89. //btn.BackColor = Color.LightBlue;
  90. //btn.Font = new Font("宋体", 10);
  91. //btn.Font = new Font("宋体", 10, FontStyle.Bold);
  92. btn.Click += Btn_Click;
  93. btn.Tag = (y - 1) * ITEM_COLS + x;
  94. btn.Text = btn.Name;
  95. btn.TextAlign = ContentAlignment.MiddleCenter;
  96. btn.TabStop = true;
  97. //pnlClient.Controls.Add(btn); //跟parent作用差不多,但颜色会变本来的颜色,原因未知
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// 销毁BUTTON
  103. /// </summary>
  104. private void DestroyItemButton()
  105. {
  106. Sunny.UI.UIButton btn;
  107. for (int y = 1; y <= ITEM_ROWS; y++)
  108. {
  109. for (int x = 1; x <= ITEM_COLS; x++)
  110. {
  111. btn = (Sunny.UI.UIButton)pnlClient.Controls[0];
  112. btn.Dispose();
  113. }
  114. }
  115. }
  116. private void clickButton(string buttonName)
  117. {
  118. Sunny.UI.UIButton btn;
  119. //btn = (Sunny.UI.UIButton)pnlClient.Controls["Item_3"];
  120. btn = (Sunny.UI.UIButton)pnlClient.Controls[buttonName];
  121. //Btn_Click(btn,null);
  122. btn.PerformClick();
  123. }
  124. /// <summary>
  125. /// 动态按钮事件
  126. /// </summary>
  127. /// <param name="sender"></param>
  128. /// <param name="e"></param>
  129. private void Btn_Click(object sender, EventArgs e)
  130. {
  131. drSelectedInterface = (DataRow)((Sunny.UI.UIButton)sender).Tag;
  132. DialogResult = DialogResult.OK;
  133. Close();
  134. }
  135. private void uiButton1_Click(object sender, EventArgs e)
  136. {
  137. FreshButton();
  138. }
  139. /// <summary>
  140. /// 刷新界面
  141. /// </summary>
  142. private void FreshButton()
  143. {
  144. dtInterface = FilterInterfaceList();
  145. DestroyItemButton();
  146. CreateItemButton();
  147. initButton();
  148. }
  149. /// <summary>
  150. /// 根据输入的键值进行相应的操作
  151. /// </summary>
  152. /// <param name="sender"></param>
  153. /// <param name="e"></param>
  154. private void Form1_KeyDown(object sender, KeyEventArgs e)
  155. {
  156. //if (e.Alt && e.Control && e.KeyCode == Keys.F2)
  157. //{
  158. // MessageBox.Show("You press the Alt and Ctrl and F2 buttons!");
  159. //}
  160. //if (e.KeyData == Keys.Up)
  161. //{
  162. // MessageBox.Show("You press the Up buttons!");
  163. //}
  164. //if (e.KeyValue == 27)
  165. //{
  166. // MessageBox.Show("You press the Esc buttons!");
  167. //}
  168. if (e.KeyData == Keys.Home)
  169. {
  170. FreshButton();
  171. }
  172. if (e.KeyData == Keys.PageUp)
  173. {
  174. if (CURRENT_PAGE >1) CURRENT_PAGE--;
  175. FreshButton();
  176. }
  177. if (e.KeyData == Keys.PageDown)
  178. {
  179. int rows = dtInterface.Rows.Count;
  180. if (CURRENT_PAGE < (rows / PAGE_SIZE + 1)) CURRENT_PAGE++;
  181. FreshButton();
  182. }
  183. if (e.KeyData == Keys.Escape)
  184. {
  185. DialogResult = DialogResult.No;
  186. Close();
  187. }
  188. if (tbLocate.Focused)
  189. {
  190. return;
  191. }
  192. //大写的A...Z.因中文输入法的原因,无法识别小写,所以固定为大写
  193. if ((e.KeyValue > 64) && (e.KeyValue < 91))
  194. {
  195. //MessageBox.Show(e.KeyData.ToString());
  196. for (int i = 0; i < pnlClient.Controls.Count; i++)
  197. {
  198. if (pnlClient.Controls[i].Visible == true)
  199. {
  200. Sunny.UI.UIButton btn;
  201. btn = (Sunny.UI.UIButton)pnlClient.Controls[i];
  202. DataRow dr = (DataRow)btn.Tag;
  203. if (dr["InterfaceNO"].ToString() == e.KeyData.ToString())
  204. {
  205. Btn_Click(btn, null);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. /// <summary>
  212. ///
  213. /// 生成模拟数据
  214. /// </summary>
  215. /// <returns></returns>
  216. private DataTable CreateSimulatedData()
  217. {
  218. try
  219. {
  220. DataTable dt = new DataTable("interface");
  221. dt.Columns.Add("ID", Type.GetType("System.Int32")); //第1列
  222. dt.Columns[0].AutoIncrement = true; //自动增加
  223. dt.Columns[0].AutoIncrementSeed = 1; //起始为1
  224. dt.Columns[0].AutoIncrementStep = 1; //步长为1
  225. dt.Columns.Add("InterfaceNO", Type.GetType("System.String")); //第2列
  226. dt.Columns.Add("InterfaceName", Type.GetType("System.String")); //第3列
  227. dt.Columns.Add("CenterURL", Type.GetType("System.String")); //第4列
  228. dt.Columns.Add("DLLName", Type.GetType("System.String")); //第4列
  229. dt.Rows.Add(new object[] { null, "A", "哈尔滨基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/" , "GuangZhouMI.dll" }); // 第1行
  230. dt.Rows.Add(new object[] { null, "B", "广州基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第2行
  231. dt.Rows.Add(new object[] { null, "C", "成都基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第3行
  232. dt.Rows.Add(new object[] { null, "D", "合肥基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第4行
  233. dt.Rows.Add(new object[] { null, "E", "云南基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第5行
  234. dt.Rows.Add(new object[] { null, "F", "郑州基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第6行
  235. dt.Rows.Add(new object[] { null, "G", "南宁基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第7行
  236. dt.Rows.Add(new object[] { null, "H", "北京基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第8行
  237. dt.Rows.Add(new object[] { null, "I", "天津基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第9行
  238. dt.Rows.Add(new object[] { null, "J", "长春基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第10行
  239. return dt;
  240. }
  241. catch (Exception )
  242. {
  243. return null;
  244. }
  245. }
  246. /// <summary>
  247. /// 初始化BUTTON
  248. /// </summary>
  249. private void initButton()
  250. {
  251. if (dtInterface == null) return;
  252. if (dtInterface.Rows.Count == 0) return;
  253. for(int i = 0;i<dtInterface.Rows.Count;i++)
  254. {
  255. Sunny.UI.UIButton btn;
  256. if (i < ((CURRENT_PAGE - 1) * PAGE_SIZE )) continue;
  257. string buttonName = "Item_" + ((i % PAGE_SIZE)+ 1 ).ToString();
  258. btn = (Sunny.UI.UIButton)pnlClient.Controls[buttonName];
  259. btn.FillColor = Color.LightBlue;
  260. btn.ForeColor = Color.Black;
  261. btn.Visible = true;
  262. btn.Text = dtInterface.Rows[i]["InterfaceName"].ToString() + "\n\r" + "【快捷键" + dtInterface.Rows[i]["InterfaceNO"].ToString() + "】";
  263. btn.Tag = dtInterface.Rows[i];
  264. //if (i == ((CURRENT_PAGE - 1) * PAGE_SIZE))
  265. //{
  266. // btn.Focus();
  267. // btn.Selected = true;
  268. //}
  269. if (i >= (CURRENT_PAGE * ITEM_ROWS * ITEM_COLS - 1)) return;
  270. }
  271. }
  272. private void tbLocate_KeyPress(object sender, KeyPressEventArgs e)
  273. {
  274. if (e.KeyChar == System.Convert.ToChar(13))
  275. {
  276. FreshButton();
  277. }
  278. }
  279. private void btPrior_Click(object sender, EventArgs e)
  280. {
  281. if (CURRENT_PAGE > 1) CURRENT_PAGE--;
  282. FreshButton();
  283. }
  284. private void btNext_Click(object sender, EventArgs e)
  285. {
  286. int rows = dtInterface.Rows.Count;
  287. if (CURRENT_PAGE < (rows / PAGE_SIZE + 1)) CURRENT_PAGE++;
  288. FreshButton();
  289. }
  290. /// <summary>
  291. /// 这个重载函数里可以预先捕捉到一些按键,比如被系统默认捕获了的Tab键
  292. /// 如果要改写Tab键的默认动作,要返回true,表示你已经处理过这个按键了
  293. /// </summary>
  294. /// <param name="msg"></param>
  295. /// <param name="keyData"></param>
  296. /// <returns></returns>
  297. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  298. {
  299. Control ctl = this.ActiveControl;
  300. if (keyData == Keys.Tab)
  301. {
  302. if (ctl.Name == "pnlClient")
  303. {
  304. foreach (Control c in ctl.Controls)//遍历控件
  305. {
  306. ((Sunny.UI.UIButton)c).Selected = false;
  307. if (c.Focused)//判断是否有焦点
  308. {
  309. ((Sunny.UI.UIButton)c).Selected = true;
  310. }
  311. }
  312. }
  313. }
  314. bool ret = base.ProcessCmdKey(ref msg, keyData);
  315. return ret;
  316. }
  317. private void InterfaceChooser_Load(object sender, EventArgs e)
  318. {
  319. FreshButton();
  320. }
  321. }
  322. }