using PTMedicalInsurance; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static HaErBinMI.Forms.ToRecord.Helper.DropHelper; namespace WinFrom1 { public partial class ModelFrom : Form { public ModelFrom() { InitializeComponent(); } private void ModelFrom_SizeChanged(object sender, EventArgs e) { if (this.WindowState != FormWindowState.Maximized) { //MessageBox.Show("还原"); //buttonMax.BackgroundImage = Resources.最大化; } } private void ModelFrom_Load(object sender, EventArgs e) { //窗体居中 this.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2;//桌面的宽度的一半减去自身宽的的一半 this.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2;//桌面的高度的一半减去自身高度的一半 #region 窗体自适应调用 this.Resize += new EventHandler(FormResize); X = this.Width; Y = this.Height; SetConSize(this); FormResize(new object(), new EventArgs()); #endregion #region 双缓冲 this.DoubleBuffered = true;//设置本窗体 SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲 //SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); //UpdateStyles(); #endregion } #region 窗体及控件自适应 private float X; private float Y; private void SetConSize(Control cons) { foreach (Control con in cons.Controls) { con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size; if (con.Controls.Count > 0) SetConSize(con); } } private void setControls(float newx, float newy, Control cons) { foreach (Control con in cons.Controls) { string[] mytag = con.Tag.ToString().Split(new char[] { ':' }); float a = Convert.ToSingle(mytag[0]) * newx; con.Width = (int)a; a = Convert.ToSingle(mytag[1]) * newy; con.Height = (int)(a); a = Convert.ToSingle(mytag[2]) * newx; con.Left = (int)(a); a = Convert.ToSingle(mytag[3]) * newy; con.Top = (int)(a); Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy); con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit); if (con.Controls.Count > 0) { setControls(newx, newy, con); } } } private void FormResize(object sender, EventArgs e) { float newx = (this.Width) / X; float newy = this.Height / Y; setControls(newx, newy, this); //this.Text = this.Width.ToString() + " " + this.Height.ToString(); } #endregion #region 减少窗体变化带来的频闪 //protected override CreateParams CreateParams //防止界面闪烁 //{ // //get // //{ // // CreateParams paras = base.CreateParams; // // paras.ExStyle |= 0x02000000; // // return paras; // //} // get // { // const int CS_NOCLOSE = 0x200; // CreateParams cp = base.CreateParams; // cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE; // cp.ExStyle |= 0x02000000; // return cp; // } //} protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; //const int CS_NOCLOSE = 0x200; //禁用窗体关闭按钮 //cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE; if (!DesignMode) { cp.ExStyle |= 0x02000000; return cp; } return cp; } } //protected override void WndProc(ref Message m) //{ // if (m.Msg == 0x0014) // 禁掉清除背景消息 // return; // base.WndProc(ref m); //} #endregion #region 窗体设置 //窗体移动 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); private void menuStrip1_MouseDown(object sender, MouseEventArgs e) { if (e.Clicks == 1) { //窗体移动 if (e.Button == MouseButtons.Left) { ReleaseCapture(); //释放鼠标捕捉 //发送左键点击的消息至该窗体(标题栏) SendMessage(this.Handle, 0xA1, 0x02, 0); } } } //窗体缩放 const int HTLEFT = 10; const int HTRIGHT = 11; const int HTTOP = 12; const int HTTOPLEFT = 13; const int HTTOPRIGHT = 14; const int HTBOTTOM = 15; const int HTBOTTOMLEFT = 0x10; const int HTBOTTOMRIGHT = 17; protected override void WndProc(ref Message m) { switch (m.Msg) { case 0x0084: base.WndProc(ref m); Point vPoint = new Point((int)m.LParam & 0xFFFF, (int)m.LParam >> 16 & 0xFFFF); vPoint = PointToClient(vPoint); if (this.WindowState != FormWindowState.Normal) break; //源代码在窗体最大化状态下,鼠标移到窗口边缘也会出现拖动标识,添加这句代码可以避免 if (vPoint.X <= 5) if (vPoint.Y <= 5) m.Result = (IntPtr)HTTOPLEFT; else if (vPoint.Y >= ClientSize.Height - 5) m.Result = (IntPtr)HTBOTTOMLEFT; else m.Result = (IntPtr)HTLEFT; else if (vPoint.X >= ClientSize.Width - 5) if (vPoint.Y <= 5) m.Result = (IntPtr)HTTOPRIGHT; else if (vPoint.Y >= ClientSize.Height - 5) m.Result = (IntPtr)HTBOTTOMRIGHT; else m.Result = (IntPtr)HTRIGHT; else if (vPoint.Y <= 2) m.Result = (IntPtr)HTTOP; else if (vPoint.Y >= ClientSize.Height - 5) m.Result = (IntPtr)HTBOTTOM; break; default: base.WndProc(ref m); break; } } #endregion #region 下拉框自动选择 /// /// 下拉框自动选择 /// /// 数据源(字典) /// 框体 public void ComBoxChoose(Dictionary data, ComboBox comBox,bool drop) { comBox.Items.Clear(); List temp = new List(); foreach (var item in data) { //循环添加数据源 if (item.Value.Contains(comBox.Text)) { ComboBoxItem com = new ComboBoxItem(); com.Text = item.Value; com.Value = item.Key; temp.Add(com); } } if (temp.Count < 1) { //输入参数未找到时添加空白选择 ComboBoxItem zero = new ComboBoxItem(); zero.Text = ""; zero.Value = ""; temp.Add(zero); } try { comBox.Items.AddRange(temp.ToArray()); comBox.SelectionStart = comBox.Text.Length; //更新索引 Cursor = Cursors.Default; //设置光标 if (drop) { comBox.DroppedDown = true; //自动弹出下拉框 } } catch (Exception ee) { comBox.SelectedIndex = -1; } } #endregion } }