ModelFrom.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace BurronModel.TestModel
  12. {
  13. public partial class ModelFrom : Form
  14. {
  15. public ModelFrom()
  16. {
  17. InitializeComponent();
  18. }
  19. private void ModelFrom_Load(object sender, EventArgs e)
  20. {
  21. //if (!DesignMode)
  22. //{
  23. //窗体居中
  24. this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;//桌面的宽度的一半减去自身宽的的一半
  25. this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;//桌面的高度的一半减去自身高度的一半
  26. #region 窗体自适应调用
  27. this.Resize += new EventHandler(FormResize);
  28. X = this.Width;
  29. Y = this.Height;
  30. SetConSize(this);
  31. FormResize(new object(), new EventArgs());
  32. #endregion
  33. #region 双缓冲
  34. this.DoubleBuffered = true;//设置本窗体
  35. SetStyle(ControlStyles.UserPaint, true);
  36. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  37. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
  38. //SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
  39. //UpdateStyles();
  40. #endregion
  41. //}
  42. }
  43. #region 窗体及控件自适应
  44. private float X;
  45. private float Y;
  46. private void SetConSize(Control cons)
  47. {
  48. foreach (Control con in cons.Controls)
  49. {
  50. con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
  51. if (con.Controls.Count > 0)
  52. SetConSize(con);
  53. }
  54. }
  55. private void setControls(float newx, float newy, Control cons)
  56. {
  57. foreach (Control con in cons.Controls)
  58. {
  59. string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
  60. float a = Convert.ToSingle(mytag[0]) * newx;
  61. con.Width = (int)a;
  62. a = Convert.ToSingle(mytag[1]) * newy;
  63. con.Height = (int)(a);
  64. a = Convert.ToSingle(mytag[2]) * newx;
  65. con.Left = (int)(a);
  66. a = Convert.ToSingle(mytag[3]) * newy;
  67. con.Top = (int)(a);
  68. Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
  69. con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
  70. if (con.Controls.Count > 0)
  71. {
  72. setControls(newx, newy, con);
  73. }
  74. }
  75. }
  76. private void FormResize(object sender, EventArgs e)
  77. {
  78. float newx = (this.Width) / X;
  79. float newy = this.Height / Y;
  80. setControls(newx, newy, this);
  81. //this.Text = this.Width.ToString() + " " + this.Height.ToString();
  82. }
  83. #endregion
  84. #region 减少窗体变化带来的频闪
  85. protected override CreateParams CreateParams
  86. {
  87. get
  88. {
  89. CreateParams cp = base.CreateParams;
  90. //const int CS_NOCLOSE = 0x200; //禁用窗体关闭按钮
  91. //cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
  92. if (!DesignMode)
  93. {
  94. cp.ExStyle |= 0x02000000;
  95. return cp;
  96. }
  97. return cp;
  98. }
  99. }
  100. //protected override void WndProc(ref Message m)
  101. //{
  102. // if (m.Msg == 0x0014) // 禁掉清除背景消息
  103. // return;
  104. // base.WndProc(ref m);
  105. //}
  106. #endregion
  107. #region 窗体设置
  108. //窗体移动
  109. [DllImport("user32.dll")]
  110. public static extern bool ReleaseCapture();
  111. [DllImport("user32.dll")]
  112. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  113. private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
  114. {
  115. if (e.Clicks == 1)
  116. {
  117. //窗体移动
  118. if (e.Button == MouseButtons.Left)
  119. {
  120. ReleaseCapture(); //释放鼠标捕捉
  121. //发送左键点击的消息至该窗体(标题栏)
  122. SendMessage(this.Handle, 0xA1, 0x02, 0);
  123. }
  124. }
  125. }
  126. //窗体缩放
  127. const int Guying_HTLEFT = 10;
  128. const int Guying_HTRIGHT = 11;
  129. const int Guying_HTTOP = 12;
  130. const int Guying_HTTOPLEFT = 13;
  131. const int Guying_HTTOPRIGHT = 14;
  132. const int Guying_HTBOTTOM = 15;
  133. const int Guying_HTBOTTOMLEFT = 0x10;
  134. const int Guying_HTBOTTOMRIGHT = 17;
  135. protected override void WndProc(ref Message m)
  136. {
  137. switch (m.Msg)
  138. {
  139. case 0x0084:
  140. base.WndProc(ref m);
  141. Point vPoint = new Point((int)m.LParam & 0xFFFF,
  142. (int)m.LParam >> 16 & 0xFFFF);
  143. vPoint = PointToClient(vPoint);
  144. if (this.WindowState != FormWindowState.Normal)
  145. break; //源代码在窗体最大化状态下,鼠标移到窗口边缘也会出现拖动标识,添加这句代码可以避免
  146. if (vPoint.X <= 5)
  147. if (vPoint.Y <= 5)
  148. m.Result = (IntPtr)Guying_HTTOPLEFT;
  149. else if (vPoint.Y >= ClientSize.Height - 5)
  150. m.Result = (IntPtr)Guying_HTBOTTOMLEFT;
  151. else m.Result = (IntPtr)Guying_HTLEFT;
  152. else if (vPoint.X >= ClientSize.Width - 5)
  153. if (vPoint.Y <= 5)
  154. m.Result = (IntPtr)Guying_HTTOPRIGHT;
  155. else if (vPoint.Y >= ClientSize.Height - 5)
  156. m.Result = (IntPtr)Guying_HTBOTTOMRIGHT;
  157. else m.Result = (IntPtr)Guying_HTRIGHT;
  158. else if (vPoint.Y <= 2)
  159. m.Result = (IntPtr)Guying_HTTOP;
  160. else if (vPoint.Y >= ClientSize.Height - 5)
  161. m.Result = (IntPtr)Guying_HTBOTTOM;
  162. break;
  163. default:
  164. base.WndProc(ref m);
  165. break;
  166. }
  167. }
  168. #endregion
  169. }
  170. }