BaseMotherForm.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Reflection;
  10. using System.Drawing.Drawing2D;
  11. using LYFZ.WinAPI;
  12. namespace LYFZ.ComponentLibrary
  13. {
  14. /// <summary>
  15. /// 无边框窗体母窗体
  16. /// </summary>
  17. public partial class BaseMotherForm : Form
  18. {
  19. public BaseMotherForm()
  20. {
  21. InitializeComponent();
  22. SetStyles();
  23. this.Load += BaseMotherForm_Load;
  24. }
  25. void BaseMotherForm_Load(object sender, EventArgs e)
  26. {
  27. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  28. }
  29. void BaseMotherForm_DoubleClick(object sender, EventArgs e)
  30. {
  31. SetWindowState();
  32. }
  33. #region 减少闪烁
  34. private void SetStyles()
  35. {
  36. try
  37. {
  38. SetStyle(
  39. ControlStyles.UserPaint |
  40. ControlStyles.AllPaintingInWmPaint |
  41. ControlStyles.OptimizedDoubleBuffer |
  42. ControlStyles.ResizeRedraw |
  43. ControlStyles.DoubleBuffer, true);
  44. //强制分配样式重新应用到控件上
  45. UpdateStyles();
  46. base.AutoScaleMode = AutoScaleMode.None;
  47. }
  48. catch { }
  49. }
  50. #endregion
  51. private void BaseMotherForm_MouseDown(object sender, MouseEventArgs e)
  52. {
  53. if(e.Clicks==1)
  54. FormMove();
  55. }
  56. /// <summary>
  57. /// 移动窗体
  58. /// </summary>
  59. public void FormMove()
  60. {
  61. try
  62. {
  63. if (FormMobile)
  64. {
  65. Win32.FormMobile(this.Handle);
  66. }
  67. }
  68. catch { }
  69. }
  70. #region 变量属性
  71. //不显示FormBorderStyle属性
  72. [Browsable(false)]
  73. [EditorBrowsable(EditorBrowsableState.Never)]
  74. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  75. public new FormBorderStyle FormBorderStyle
  76. {
  77. get { return base.FormBorderStyle; }
  78. set { base.FormBorderStyle = FormBorderStyle.None; }
  79. }
  80. private bool _FormMobile = true;
  81. /// <summary>
  82. /// 窗体是否可以移动
  83. /// </summary>
  84. [DescriptionAttribute("窗体是否可以移动"), CategoryAttribute("自定义窗体属性"), DefaultValueAttribute(typeof(bool), "true")]
  85. public bool FormMobile
  86. {
  87. get { return _FormMobile; }
  88. set
  89. {
  90. if (_FormMobile != value)
  91. {
  92. _FormMobile = value;
  93. }
  94. }
  95. }
  96. private bool _isMaximized = true;
  97. /// <summary>
  98. /// 窗体是是允许最大化
  99. /// </summary>
  100. [Category("自定义窗体属性")]
  101. [Description("窗体是是允许最大化")]
  102. [DefaultValue(typeof(bool), "true")]
  103. public bool IsMaximized
  104. {
  105. get { return _isMaximized; }
  106. set { _isMaximized = value;}
  107. }
  108. private bool _isReion = false;
  109. /// <summary>
  110. /// 窗体是是否圆角
  111. /// </summary>
  112. [Category("自定义窗体属性")]
  113. [Description("窗体是是否圆角")]
  114. [DefaultValue(typeof(bool), "false")]
  115. public bool IsReion
  116. {
  117. get { return _isReion; }
  118. set { _isReion = value; }
  119. }
  120. bool _isFullScreen = false;
  121. /// <summary>
  122. /// 窗体WindowState为FormWindowState.Maximized值时,是否可以全屏显示
  123. /// </summary>
  124. [Category("自定义窗体属性")]
  125. [Description("窗体WindowState为FormWindowState.Maximized值时,是否可以全屏显示")]
  126. [DefaultValue(typeof(bool), "false")]
  127. public bool IsFullScreen
  128. {
  129. get { return _isFullScreen; }
  130. set { _isFullScreen = value; }
  131. }
  132. bool _isUserControlsSize = false;
  133. /// <summary>
  134. /// 窗体是否允许用户拖动控件大小
  135. /// </summary>
  136. [Category("自定义窗体属性")]
  137. [Description("窗体是否允许用户拖动控件大小")]
  138. [DefaultValue(typeof(bool), "false")]
  139. public bool IsUserControlsSize
  140. {
  141. get { return _isUserControlsSize; }
  142. set { _isUserControlsSize = value; }
  143. }
  144. #endregion
  145. //控件首次创建时被调用
  146. protected override void OnCreateControl()
  147. {
  148. try
  149. {
  150. base.OnCreateControl();
  151. if (IsReion)
  152. {
  153. SetReion();//需要圆角时开启
  154. }
  155. }
  156. catch { }
  157. }
  158. //圆角
  159. private void SetReion()
  160. {
  161. try
  162. {
  163. using (GraphicsPath path =
  164. GraphicsPathHelper.CreatePath(
  165. new Rectangle(Point.Empty, base.Size), 6, RoundStyle.All, true))
  166. {
  167. Region region = new Region(path);
  168. path.Widen(Pens.White);
  169. region.Union(path);
  170. this.Region = region;
  171. }
  172. }
  173. catch { }
  174. }
  175. //改变窗体大小时
  176. protected override void OnSizeChanged(EventArgs e)
  177. {
  178. try
  179. {
  180. base.OnSizeChanged(e);
  181. if (IsReion)
  182. {
  183. SetReion();//需要圆角时开启
  184. }
  185. if (!this._isFullScreen)
  186. {
  187. this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
  188. }
  189. //else {
  190. // this.MaximumSize = new Size(0,0);
  191. //}
  192. }
  193. catch { }
  194. }
  195. /// <summary>
  196. /// 设置窗体最大化和还原状态
  197. /// </summary>
  198. public void SetWindowState()
  199. {
  200. try
  201. {
  202. if (IsMaximized)
  203. {
  204. if (this.MaximizeBox && this.MinimizeBox)
  205. {
  206. if (this.WindowState == FormWindowState.Normal)
  207. {
  208. this.WindowState = FormWindowState.Maximized;
  209. }
  210. else
  211. {
  212. this.WindowState = FormWindowState.Normal;
  213. }
  214. }
  215. }
  216. }
  217. catch { }
  218. }
  219. #region 重写CreateParams
  220. protected override CreateParams CreateParams
  221. {
  222. get
  223. {
  224. try
  225. {
  226. const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
  227. CreateParams cp = base.CreateParams;
  228. cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
  229. return cp;
  230. }
  231. catch {
  232. return base.CreateParams;
  233. }
  234. }
  235. }
  236. #endregion
  237. #region 窗体可调大小
  238. const int Guying_HTLEFT = 10;
  239. const int Guying_HTRIGHT = 11;
  240. const int Guying_HTTOP = 12;
  241. const int Guying_HTTOPLEFT = 13;
  242. const int Guying_HTTOPRIGHT = 14;
  243. const int Guying_HTBOTTOM = 15;
  244. const int Guying_HTBOTTOMLEFT = 0x10;
  245. const int Guying_HTBOTTOMRIGHT = 17;
  246. protected override void WndProc(ref Message m)
  247. {
  248. if (IsUserControlsSize)
  249. {
  250. try
  251. {
  252. switch (m.Msg)
  253. {
  254. case 0x0084:
  255. base.WndProc(ref m);
  256. Point vPoint = new Point((int)m.LParam & 0xFFFF,
  257. (int)m.LParam >> 16 & 0xFFFF);
  258. vPoint = PointToClient(vPoint);
  259. if (vPoint.X <= 5)
  260. if (vPoint.Y <= 5)
  261. m.Result = (IntPtr)Guying_HTTOPLEFT;
  262. else if (vPoint.Y >= ClientSize.Height - 5)
  263. m.Result = (IntPtr)Guying_HTBOTTOMLEFT;
  264. else m.Result = (IntPtr)Guying_HTLEFT;
  265. else if (vPoint.X >= ClientSize.Width - 5)
  266. if (vPoint.Y <= 5)
  267. m.Result = (IntPtr)Guying_HTTOPRIGHT;
  268. else if (vPoint.Y >= ClientSize.Height - 5)
  269. m.Result = (IntPtr)Guying_HTBOTTOMRIGHT;
  270. else m.Result = (IntPtr)Guying_HTRIGHT;
  271. else if (vPoint.Y <= 5)
  272. m.Result = (IntPtr)Guying_HTTOP;
  273. else if (vPoint.Y >= ClientSize.Height - 5)
  274. m.Result = (IntPtr)Guying_HTBOTTOM;
  275. break;
  276. case 0x0201: //鼠标左键按下的消息
  277. m.Msg = 0x00A1; //更改消息为非客户区按下鼠标
  278. m.LParam = IntPtr.Zero; //默认值
  279. m.WParam = new IntPtr(2);//鼠标放在标题栏内
  280. base.WndProc(ref m);
  281. break;
  282. default:
  283. try
  284. {
  285. base.WndProc(ref m);
  286. }
  287. catch { }
  288. break;
  289. }
  290. }
  291. catch { }
  292. }
  293. else
  294. {
  295. try
  296. {
  297. base.WndProc(ref m);
  298. }
  299. catch { }
  300. }
  301. }
  302. #endregion
  303. protected override void CreateHandle()
  304. {
  305. if (!IsHandleCreated)
  306. {
  307. try
  308. {
  309. base.CreateHandle();
  310. }
  311. catch { }
  312. finally
  313. {
  314. if (!IsHandleCreated)
  315. {
  316. base.RecreateHandle();
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }