ModelFrom.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using PTMedicalInsurance;
  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.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using static HaErBinMI.Forms.ToRecord.Helper.DropHelper;
  13. namespace WinFrom1
  14. {
  15. public partial class ModelFrom : Form
  16. {
  17. public ModelFrom()
  18. {
  19. InitializeComponent();
  20. }
  21. private void ModelFrom_SizeChanged(object sender, EventArgs e)
  22. {
  23. if (this.WindowState != FormWindowState.Maximized)
  24. {
  25. //MessageBox.Show("还原");
  26. //buttonMax.BackgroundImage = Resources.最大化;
  27. }
  28. }
  29. private void ModelFrom_Load(object sender, EventArgs e)
  30. {
  31. //窗体居中
  32. this.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2;//桌面的宽度的一半减去自身宽的的一半
  33. this.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2;//桌面的高度的一半减去自身高度的一半
  34. #region 窗体自适应调用
  35. this.Resize += new EventHandler(FormResize);
  36. X = this.Width;
  37. Y = this.Height;
  38. SetConSize(this);
  39. FormResize(new object(), new EventArgs());
  40. #endregion
  41. #region 双缓冲
  42. this.DoubleBuffered = true;//设置本窗体
  43. SetStyle(ControlStyles.UserPaint, true);
  44. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  45. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
  46. //SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
  47. //UpdateStyles();
  48. #endregion
  49. }
  50. #region 窗体及控件自适应
  51. private float X;
  52. private float Y;
  53. private void SetConSize(Control cons)
  54. {
  55. foreach (Control con in cons.Controls)
  56. {
  57. con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
  58. if (con.Controls.Count > 0)
  59. SetConSize(con);
  60. }
  61. }
  62. private void setControls(float newx, float newy, Control cons)
  63. {
  64. foreach (Control con in cons.Controls)
  65. {
  66. string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
  67. float a = Convert.ToSingle(mytag[0]) * newx;
  68. con.Width = (int)a;
  69. a = Convert.ToSingle(mytag[1]) * newy;
  70. con.Height = (int)(a);
  71. a = Convert.ToSingle(mytag[2]) * newx;
  72. con.Left = (int)(a);
  73. a = Convert.ToSingle(mytag[3]) * newy;
  74. con.Top = (int)(a);
  75. Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
  76. con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
  77. if (con.Controls.Count > 0)
  78. {
  79. setControls(newx, newy, con);
  80. }
  81. }
  82. }
  83. private void FormResize(object sender, EventArgs e)
  84. {
  85. float newx = (this.Width) / X;
  86. float newy = this.Height / Y;
  87. setControls(newx, newy, this);
  88. //this.Text = this.Width.ToString() + " " + this.Height.ToString();
  89. }
  90. #endregion
  91. #region 减少窗体变化带来的频闪
  92. //protected override CreateParams CreateParams //防止界面闪烁
  93. //{
  94. // //get
  95. // //{
  96. // // CreateParams paras = base.CreateParams;
  97. // // paras.ExStyle |= 0x02000000;
  98. // // return paras;
  99. // //}
  100. // get
  101. // {
  102. // const int CS_NOCLOSE = 0x200;
  103. // CreateParams cp = base.CreateParams;
  104. // cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
  105. // cp.ExStyle |= 0x02000000;
  106. // return cp;
  107. // }
  108. //}
  109. protected override CreateParams CreateParams
  110. {
  111. get
  112. {
  113. CreateParams cp = base.CreateParams;
  114. //const int CS_NOCLOSE = 0x200; //禁用窗体关闭按钮
  115. //cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
  116. if (!DesignMode)
  117. {
  118. cp.ExStyle |= 0x02000000;
  119. return cp;
  120. }
  121. return cp;
  122. }
  123. }
  124. //protected override void WndProc(ref Message m)
  125. //{
  126. // if (m.Msg == 0x0014) // 禁掉清除背景消息
  127. // return;
  128. // base.WndProc(ref m);
  129. //}
  130. #endregion
  131. #region 窗体设置
  132. //窗体移动
  133. [DllImport("user32.dll")]
  134. public static extern bool ReleaseCapture();
  135. [DllImport("user32.dll")]
  136. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  137. private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
  138. {
  139. if (e.Clicks == 1)
  140. {
  141. //窗体移动
  142. if (e.Button == MouseButtons.Left)
  143. {
  144. ReleaseCapture(); //释放鼠标捕捉
  145. //发送左键点击的消息至该窗体(标题栏)
  146. SendMessage(this.Handle, 0xA1, 0x02, 0);
  147. }
  148. }
  149. }
  150. //窗体缩放
  151. const int HTLEFT = 10;
  152. const int HTRIGHT = 11;
  153. const int HTTOP = 12;
  154. const int HTTOPLEFT = 13;
  155. const int HTTOPRIGHT = 14;
  156. const int HTBOTTOM = 15;
  157. const int HTBOTTOMLEFT = 0x10;
  158. const int HTBOTTOMRIGHT = 17;
  159. protected override void WndProc(ref Message m)
  160. {
  161. switch (m.Msg)
  162. {
  163. case 0x0084:
  164. base.WndProc(ref m);
  165. Point vPoint = new Point((int)m.LParam & 0xFFFF,
  166. (int)m.LParam >> 16 & 0xFFFF);
  167. vPoint = PointToClient(vPoint);
  168. if (this.WindowState != FormWindowState.Normal)
  169. break; //源代码在窗体最大化状态下,鼠标移到窗口边缘也会出现拖动标识,添加这句代码可以避免
  170. if (vPoint.X <= 5)
  171. if (vPoint.Y <= 5)
  172. m.Result = (IntPtr)HTTOPLEFT;
  173. else if (vPoint.Y >= ClientSize.Height - 5)
  174. m.Result = (IntPtr)HTBOTTOMLEFT;
  175. else m.Result = (IntPtr)HTLEFT;
  176. else if (vPoint.X >= ClientSize.Width - 5)
  177. if (vPoint.Y <= 5)
  178. m.Result = (IntPtr)HTTOPRIGHT;
  179. else if (vPoint.Y >= ClientSize.Height - 5)
  180. m.Result = (IntPtr)HTBOTTOMRIGHT;
  181. else m.Result = (IntPtr)HTRIGHT;
  182. else if (vPoint.Y <= 2)
  183. m.Result = (IntPtr)HTTOP;
  184. else if (vPoint.Y >= ClientSize.Height - 5)
  185. m.Result = (IntPtr)HTBOTTOM;
  186. break;
  187. default:
  188. base.WndProc(ref m);
  189. break;
  190. }
  191. }
  192. #endregion
  193. #region 下拉框自动选择
  194. /// <summary>
  195. /// 下拉框自动选择
  196. /// </summary>
  197. /// <param name="data">数据源(字典)</param>
  198. /// <param name="comBox">框体</param>
  199. public void ComBoxChoose(Dictionary<string, string> data, ComboBox comBox,bool drop)
  200. {
  201. comBox.Items.Clear();
  202. List<ComboBoxItem> temp = new List<ComboBoxItem>();
  203. foreach (var item in data)
  204. {
  205. //循环添加数据源
  206. if (item.Value.Contains(comBox.Text))
  207. {
  208. ComboBoxItem com = new ComboBoxItem();
  209. com.Text = item.Value;
  210. com.Value = item.Key;
  211. temp.Add(com);
  212. }
  213. }
  214. if (temp.Count < 1)
  215. {
  216. //输入参数未找到时添加空白选择
  217. ComboBoxItem zero = new ComboBoxItem();
  218. zero.Text = "";
  219. zero.Value = "";
  220. temp.Add(zero);
  221. }
  222. try
  223. {
  224. comBox.Items.AddRange(temp.ToArray());
  225. comBox.SelectionStart = comBox.Text.Length; //更新索引
  226. Cursor = Cursors.Default; //设置光标
  227. if (drop)
  228. {
  229. comBox.DroppedDown = true; //自动弹出下拉框
  230. }
  231. }
  232. catch (Exception ee)
  233. {
  234. comBox.SelectedIndex = -1;
  235. }
  236. }
  237. #endregion
  238. }
  239. }