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("InterfaceName").Contains(filterStr)); if (dt1.Count() > 0) { DataTable dt2 = dt1.CopyToDataTable(); return dt2; } else return null; } /// /// 生成BUTTON /// 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作用差不多,但颜色会变本来的颜色,原因未知 } } } /// /// 销毁BUTTON /// 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(); } /// /// 动态按钮事件 /// /// /// 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(); } /// /// 刷新界面 /// private void FreshButton() { dtInterface = FilterInterfaceList(); DestroyItemButton(); CreateItemButton(); initButton(); } /// /// 根据输入的键值进行相应的操作 /// /// /// 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); } } } } } /// /// /// 生成模拟数据 /// /// 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; } } /// /// 初始化BUTTON /// private void initButton() { if (dtInterface == null) return; if (dtInterface.Rows.Count == 0) return; for(int i = 0;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(); } /// /// 这个重载函数里可以预先捕捉到一些按键,比如被系统默认捕获了的Tab键 /// 如果要改写Tab键的默认动作,要返回true,表示你已经处理过这个按键了 /// /// /// /// 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(); } } }