BaseMotherForm.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 (Exception ex)
  157. {
  158. MessageBox.Show("控件首次创建时:" + ex.Message + ex.Source);
  159. }
  160. }
  161. //圆角
  162. private void SetReion()
  163. {
  164. try
  165. {
  166. using (GraphicsPath path =
  167. GraphicsPathHelper.CreatePath(
  168. new Rectangle(Point.Empty, base.Size), 6, RoundStyle.All, true))
  169. {
  170. Region region = new Region(path);
  171. path.Widen(Pens.White);
  172. region.Union(path);
  173. this.Region = region;
  174. }
  175. }
  176. catch { }
  177. }
  178. //改变窗体大小时
  179. protected override void OnSizeChanged(EventArgs e)
  180. {
  181. try
  182. {
  183. base.OnSizeChanged(e);
  184. if (IsReion)
  185. {
  186. SetReion();//需要圆角时开启
  187. }
  188. if (!this._isFullScreen)
  189. {
  190. this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
  191. }
  192. //else {
  193. // this.MaximumSize = new Size(0,0);
  194. //}
  195. }
  196. catch { }
  197. }
  198. /// <summary>
  199. /// 设置窗体最大化和还原状态
  200. /// </summary>
  201. public void SetWindowState()
  202. {
  203. try
  204. {
  205. if (IsMaximized)
  206. {
  207. if (this.MaximizeBox && this.MinimizeBox)
  208. {
  209. if (this.WindowState == FormWindowState.Normal)
  210. {
  211. this.WindowState = FormWindowState.Maximized;
  212. }
  213. else
  214. {
  215. this.WindowState = FormWindowState.Normal;
  216. }
  217. }
  218. }
  219. }
  220. catch { }
  221. }
  222. #region 重写CreateParams
  223. protected override CreateParams CreateParams
  224. {
  225. get
  226. {
  227. try
  228. {
  229. const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
  230. CreateParams cp = base.CreateParams;
  231. cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
  232. return cp;
  233. }
  234. catch {
  235. return base.CreateParams;
  236. }
  237. }
  238. }
  239. #endregion
  240. #region 窗体可调大小
  241. const int Guying_HTLEFT = 10;
  242. const int Guying_HTRIGHT = 11;
  243. const int Guying_HTTOP = 12;
  244. const int Guying_HTTOPLEFT = 13;
  245. const int Guying_HTTOPRIGHT = 14;
  246. const int Guying_HTBOTTOM = 15;
  247. const int Guying_HTBOTTOMLEFT = 0x10;
  248. const int Guying_HTBOTTOMRIGHT = 17;
  249. protected override void WndProc(ref Message m)
  250. {
  251. if (IsUserControlsSize)
  252. {
  253. try
  254. {
  255. switch (m.Msg)
  256. {
  257. case 0x0084:
  258. base.WndProc(ref m);
  259. Point vPoint = new Point((int)m.LParam & 0xFFFF,
  260. (int)m.LParam >> 16 & 0xFFFF);
  261. vPoint = PointToClient(vPoint);
  262. if (vPoint.X <= 5)
  263. if (vPoint.Y <= 5)
  264. m.Result = (IntPtr)Guying_HTTOPLEFT;
  265. else if (vPoint.Y >= ClientSize.Height - 5)
  266. m.Result = (IntPtr)Guying_HTBOTTOMLEFT;
  267. else m.Result = (IntPtr)Guying_HTLEFT;
  268. else if (vPoint.X >= ClientSize.Width - 5)
  269. if (vPoint.Y <= 5)
  270. m.Result = (IntPtr)Guying_HTTOPRIGHT;
  271. else if (vPoint.Y >= ClientSize.Height - 5)
  272. m.Result = (IntPtr)Guying_HTBOTTOMRIGHT;
  273. else m.Result = (IntPtr)Guying_HTRIGHT;
  274. else if (vPoint.Y <= 5)
  275. m.Result = (IntPtr)Guying_HTTOP;
  276. else if (vPoint.Y >= ClientSize.Height - 5)
  277. m.Result = (IntPtr)Guying_HTBOTTOM;
  278. break;
  279. case 0x0201: //鼠标左键按下的消息
  280. m.Msg = 0x00A1; //更改消息为非客户区按下鼠标
  281. m.LParam = IntPtr.Zero; //默认值
  282. m.WParam = new IntPtr(2);//鼠标放在标题栏内
  283. base.WndProc(ref m);
  284. break;
  285. default:
  286. try
  287. {
  288. base.WndProc(ref m);
  289. }
  290. catch { }
  291. break;
  292. }
  293. }
  294. catch {
  295. try
  296. {
  297. base.WndProc(ref m);
  298. }
  299. catch { }
  300. }
  301. }
  302. else
  303. {
  304. try
  305. {
  306. base.WndProc(ref m);
  307. }
  308. catch { }
  309. }
  310. }
  311. #endregion
  312. protected override void CreateHandle()
  313. {
  314. if (!IsHandleCreated)
  315. {
  316. try
  317. {
  318. base.CreateHandle();
  319. }
  320. catch { }
  321. finally
  322. {
  323. if (!IsHandleCreated)
  324. {
  325. try
  326. {
  327. base.RecreateHandle();
  328. }
  329. catch { }
  330. }
  331. }
  332. }
  333. }
  334. }
  335. }