| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace PTMedInsuInterface.Forms
- {
- public partial class InterfaceChooser : Form
- {
- const int ITEM_WIDTH = 300;
- const int ITEM_HEIGTH = 80;
- const int ITEM_ROW_SPACE = 6;
- const int ITEM_COL_SPACE = 8;
- int CURRENT_PAGE = 1;
- int PAGE_SIZE;
- int ITEM_ROWS;
- int ITEM_COLS;
- DataTable dtInterface = new DataTable();
- DataTable dtOriginalInterface = new DataTable();
- public DataRow drSelectedInterface;
- public InterfaceChooser(JArray ja)
- {
- InitializeComponent();
- try
- {
- //需要将窗体的KeyPreview属性设置为true(如下图所示:),将系统传入的键值先传递给窗体,再传递给控件。MSDN关于KeyPreview的说明:如果窗体将接收所有键事件,则为 true;如果窗体上当前选定控件接收键事件,则为 false。默认为 false
- this.KeyPreview = true;
- ITEM_COLS = pnlClient.Width /(ITEM_WIDTH + ITEM_COL_SPACE);
- ITEM_ROWS= pnlClient.Height /(ITEM_HEIGTH + ITEM_ROW_SPACE);
- PAGE_SIZE = ITEM_COLS * ITEM_ROWS;
- CreateItemButton();
-
- //窗体的大小固定
- this.MaximizeBox = false;
- this.MinimizeBox = false;
- //默认可调整大小的边框模式
- this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
- //固定的三维边框
- this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
- //出现在屏幕正中央
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- //获取医保接口清单
-
- dtOriginalInterface = (DataTable)ja.ToObject(typeof(DataTable));
- dtInterface = dtOriginalInterface;
- }
- catch (Exception ex)
- {
- throw new Exception("InterfaceChooser构造出错:" + ex.Message);
- }
- }
- private DataTable FilterInterfaceList()
- {
- string filterStr = tbLocate.Text;
- var dt1 = dtOriginalInterface.AsEnumerable()
- .Where(p => p.Field<string>("InterfaceName").Contains(filterStr));
- if (dt1.Count() > 0)
- {
- DataTable dt2 = dt1.CopyToDataTable<DataRow>();
- return dt2;
- }
- else
- return null;
-
- }
- /// <summary>
- /// 生成BUTTON
- /// </summary>
- private void CreateItemButton()
- {
- Sunny.UI.UIButton btn;
- for (int y = 1; y <=ITEM_ROWS; y++)
- {
- for (int x = 1; x <=ITEM_COLS; x++)
- {
- btn = new Sunny.UI.UIButton();
- btn.Parent = pnlClient;
- btn.Visible = false;
- btn.Name = "Item_" + ((y-1)*ITEM_COLS +x).ToString();
- btn.Width = ITEM_WIDTH;
- btn.Height = ITEM_HEIGTH;
- btn.Left = ITEM_COL_SPACE * x + (x - 1) * btn.Width;
- btn.Top = ITEM_ROW_SPACE * y + (y - 1) * btn.Height;
- //btn.Location = new System.Drawing.Point(btn.Left, btn.Top);
- btn.FillColor = Color.LightBlue;
- btn.ForeColor = Color.Black;
- //btn.BackColor = Color.LightBlue;
- //btn.Font = new Font("宋体", 10);
- //btn.Font = new Font("宋体", 10, FontStyle.Bold);
- btn.Click += Btn_Click;
- btn.Tag = (y - 1) * ITEM_COLS + x;
- btn.Text = btn.Name;
- btn.TextAlign = ContentAlignment.MiddleCenter;
- btn.TabStop = true;
- //pnlClient.Controls.Add(btn); //跟parent作用差不多,但颜色会变本来的颜色,原因未知
- }
- }
- }
- /// <summary>
- /// 销毁BUTTON
- /// </summary>
- private void DestroyItemButton()
- {
- Sunny.UI.UIButton btn;
- for (int y = 1; y <= ITEM_ROWS; y++)
- {
- for (int x = 1; x <= ITEM_COLS; x++)
- {
-
- btn = (Sunny.UI.UIButton)pnlClient.Controls[0];
- btn.Dispose();
- }
- }
- }
- private void clickButton(string buttonName)
- {
- Sunny.UI.UIButton btn;
- //btn = (Sunny.UI.UIButton)pnlClient.Controls["Item_3"];
- btn = (Sunny.UI.UIButton)pnlClient.Controls[buttonName];
- //Btn_Click(btn,null);
- btn.PerformClick();
- }
- /// <summary>
- /// 动态按钮事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Btn_Click(object sender, EventArgs e)
- {
- drSelectedInterface = (DataRow)((Sunny.UI.UIButton)sender).Tag;
- DialogResult = DialogResult.OK;
- Close();
- }
- private void uiButton1_Click(object sender, EventArgs e)
- {
- FreshButton();
- }
- /// <summary>
- /// 刷新界面
- /// </summary>
- private void FreshButton()
- {
- dtInterface = FilterInterfaceList();
- DestroyItemButton();
- CreateItemButton();
- initButton();
- }
- /// <summary>
- /// 根据输入的键值进行相应的操作
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- //if (e.Alt && e.Control && e.KeyCode == Keys.F2)
- //{
- // MessageBox.Show("You press the Alt and Ctrl and F2 buttons!");
- //}
- //if (e.KeyData == Keys.Up)
- //{
- // MessageBox.Show("You press the Up buttons!");
- //}
- //if (e.KeyValue == 27)
- //{
- // MessageBox.Show("You press the Esc buttons!");
- //}
-
- if (e.KeyData == Keys.Home)
- {
- FreshButton();
- }
- if (e.KeyData == Keys.PageUp)
- {
-
- if (CURRENT_PAGE >1) CURRENT_PAGE--;
- FreshButton();
-
- }
- if (e.KeyData == Keys.PageDown)
- {
- int rows = dtInterface.Rows.Count;
- if (CURRENT_PAGE < (rows / PAGE_SIZE + 1)) CURRENT_PAGE++;
- FreshButton();
- }
- if (e.KeyData == Keys.Escape)
- {
- DialogResult = DialogResult.No;
- Close();
- }
- if (tbLocate.Focused)
- {
- return;
- }
- //大写的A...Z.因中文输入法的原因,无法识别小写,所以固定为大写
- if ((e.KeyValue > 64) && (e.KeyValue < 91))
- {
- //MessageBox.Show(e.KeyData.ToString());
- for (int i = 0; i < pnlClient.Controls.Count; i++)
- {
- if (pnlClient.Controls[i].Visible == true)
- {
- Sunny.UI.UIButton btn;
- btn = (Sunny.UI.UIButton)pnlClient.Controls[i];
- DataRow dr = (DataRow)btn.Tag;
- if (dr["InterfaceNO"].ToString() == e.KeyData.ToString())
- {
- Btn_Click(btn, null);
- }
- }
- }
- }
- }
- /// <summary>
- ///
- /// 生成模拟数据
- /// </summary>
- /// <returns></returns>
- private DataTable CreateSimulatedData()
- {
- try
- {
- DataTable dt = new DataTable("interface");
- dt.Columns.Add("ID", Type.GetType("System.Int32")); //第1列
- dt.Columns[0].AutoIncrement = true; //自动增加
- dt.Columns[0].AutoIncrementSeed = 1; //起始为1
- dt.Columns[0].AutoIncrementStep = 1; //步长为1
- dt.Columns.Add("InterfaceNO", Type.GetType("System.String")); //第2列
- dt.Columns.Add("InterfaceName", Type.GetType("System.String")); //第3列
- dt.Columns.Add("CenterURL", Type.GetType("System.String")); //第4列
- dt.Columns.Add("DLLName", Type.GetType("System.String")); //第4列
- dt.Rows.Add(new object[] { null, "A", "哈尔滨基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/" , "GuangZhouMI.dll" }); // 第1行
- dt.Rows.Add(new object[] { null, "B", "广州基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第2行
- dt.Rows.Add(new object[] { null, "C", "成都基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第3行
- dt.Rows.Add(new object[] { null, "D", "合肥基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第4行
- dt.Rows.Add(new object[] { null, "E", "云南基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第5行
- dt.Rows.Add(new object[] { null, "F", "郑州基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第6行
- dt.Rows.Add(new object[] { null, "G", "南宁基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第7行
- dt.Rows.Add(new object[] { null, "H", "北京基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第8行
- dt.Rows.Add(new object[] { null, "I", "天津基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第9行
- dt.Rows.Add(new object[] { null, "J", "长春基线版医保", @"http://19.15.78.136:20001/ebus/gdyb_inf/poc/hsa/hgs/", "GuangZhouMI.dll" }); // 第10行
- return dt;
- }
- catch (Exception )
- {
- return null;
- }
- }
- /// <summary>
- /// 初始化BUTTON
- /// </summary>
- private void initButton()
- {
- if (dtInterface == null) return;
- if (dtInterface.Rows.Count == 0) return;
- for(int i = 0;i<dtInterface.Rows.Count;i++)
- {
- Sunny.UI.UIButton btn;
- if (i < ((CURRENT_PAGE - 1) * PAGE_SIZE )) continue;
- string buttonName = "Item_" + ((i % PAGE_SIZE)+ 1 ).ToString();
- btn = (Sunny.UI.UIButton)pnlClient.Controls[buttonName];
- btn.FillColor = Color.LightBlue;
- btn.ForeColor = Color.Black;
- btn.Visible = true;
- btn.Text = dtInterface.Rows[i]["InterfaceName"].ToString() + "\n\r" + "【快捷键" + dtInterface.Rows[i]["InterfaceNO"].ToString() + "】";
- btn.Tag = dtInterface.Rows[i];
- //if (i == ((CURRENT_PAGE - 1) * PAGE_SIZE))
- //{
- // btn.Focus();
- // btn.Selected = true;
- //}
- if (i >= (CURRENT_PAGE * ITEM_ROWS * ITEM_COLS - 1)) return;
- }
- }
- private void tbLocate_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == System.Convert.ToChar(13))
- {
-
- FreshButton();
- }
- }
- private void btPrior_Click(object sender, EventArgs e)
- {
- if (CURRENT_PAGE > 1) CURRENT_PAGE--;
- FreshButton();
- }
- private void btNext_Click(object sender, EventArgs e)
- {
- int rows = dtInterface.Rows.Count;
- if (CURRENT_PAGE < (rows / PAGE_SIZE + 1)) CURRENT_PAGE++;
- FreshButton();
- }
- /// <summary>
- /// 这个重载函数里可以预先捕捉到一些按键,比如被系统默认捕获了的Tab键
- /// 如果要改写Tab键的默认动作,要返回true,表示你已经处理过这个按键了
- /// </summary>
- /// <param name="msg"></param>
- /// <param name="keyData"></param>
- /// <returns></returns>
- protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
- {
- Control ctl = this.ActiveControl;
- if (keyData == Keys.Tab)
- {
- if (ctl.Name == "pnlClient")
- {
- foreach (Control c in ctl.Controls)//遍历控件
- {
- ((Sunny.UI.UIButton)c).Selected = false;
- if (c.Focused)//判断是否有焦点
- {
- ((Sunny.UI.UIButton)c).Selected = true;
- }
- }
- }
- }
- bool ret = base.ProcessCmdKey(ref msg, keyData);
- return ret;
- }
- private void InterfaceChooser_Load(object sender, EventArgs e)
- {
- FreshButton();
- }
- }
- }
|