NoTitleForm.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using Azylee.WinformSkin.APIUtils;
  2. using Azylee.WinformSkin.StyleUtils;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8. namespace Azylee.WinformSkin.FormUI.NoTitle
  9. {
  10. public partial class NoTitleForm : Form
  11. {
  12. Label BorderTop = new Label();
  13. Label BorderBottom = new Label();
  14. Label BorderLeft = new Label();
  15. Label BorderRight = new Label();
  16. #region 属性
  17. //窗体边框粗细
  18. private int _Border = 1;
  19. [Category("Style")]
  20. [Description("窗体边框粗细")]
  21. [DefaultValue(typeof(int), "1")]
  22. public int Border
  23. {
  24. get { return _Border; }
  25. set
  26. {
  27. if (_Border != value)
  28. {
  29. _Border = value;
  30. SetBorder();
  31. }
  32. }
  33. }
  34. //窗体边框颜色
  35. private Color _BorderColor = Color.Black;
  36. [Category("Style")]
  37. [Description("窗体边框颜色")]
  38. [DefaultValue(typeof(Color), "Black")]
  39. public Color BorderColor
  40. {
  41. get { return _BorderColor; }
  42. set
  43. {
  44. if (_BorderColor != value)
  45. {
  46. _BorderColor = value;
  47. SetBorder();
  48. }
  49. }
  50. }
  51. //设置炫彩模式
  52. private bool _Colorful = false;
  53. [Category("Style")]
  54. [Description("炫彩模式")]
  55. [DefaultValue(typeof(bool), "false")]
  56. public bool Colorful
  57. {
  58. get { return _Colorful; }
  59. set
  60. {
  61. if (_Colorful != value)
  62. {
  63. _Colorful = value;
  64. if (value) SetColorful();
  65. }
  66. }
  67. }
  68. #endregion
  69. public NoTitleForm()
  70. {
  71. InitializeComponent();
  72. SetStyle(ControlStyles.UserPaint, true);
  73. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  74. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
  75. }
  76. private void NoTitleForm_Load(object sender, EventArgs e)
  77. {
  78. SetBorder();
  79. if (_Colorful) SetColorful();
  80. }
  81. /// <summary>
  82. /// 设置无标题窗口可拖动
  83. /// </summary>
  84. /// <param name="e"></param>
  85. protected override void OnMouseMove(MouseEventArgs e)
  86. {
  87. base.OnMouseMove(e);
  88. if (e.Button == MouseButtons.Left)
  89. {
  90. FormStyleAPI.ReleaseCapture();
  91. FormStyleAPI.SendMessage(Handle, FormStyleAPI.WM_NCLBUTTONDOWN, FormStyleAPI.HTCAPTION, 0);
  92. }
  93. }
  94. /// <summary>
  95. /// 设置窗口边框
  96. /// </summary>
  97. protected void SetBorder()
  98. {
  99. if (_Border > 0)
  100. {
  101. BorderTop.BackColor = _BorderColor;
  102. BorderTop.Width = Width;
  103. BorderTop.Height = _Border;
  104. Controls.Add(BorderTop);
  105. BorderTop.BringToFront();
  106. BorderTop.Top = 0;
  107. BorderTop.Left = 0;
  108. BorderBottom.BackColor = _BorderColor;
  109. BorderBottom.Width = Width;
  110. BorderBottom.Height = _Border;
  111. Controls.Add(BorderBottom);
  112. BorderBottom.BringToFront();
  113. BorderBottom.Top = Height - _Border;
  114. BorderBottom.Left = 0;
  115. BorderLeft.BackColor = _BorderColor;
  116. BorderLeft.Width = _Border;
  117. BorderLeft.Height = Height;
  118. Controls.Add(BorderLeft);
  119. BorderLeft.BringToFront();
  120. BorderLeft.Top = 0;
  121. BorderLeft.Left = 0;
  122. BorderRight.BackColor = _BorderColor;
  123. BorderRight.Width = _Border;
  124. BorderRight.Height = Height;
  125. Controls.Add(BorderRight);
  126. BorderRight.BringToFront();
  127. BorderRight.Top = 0;
  128. BorderRight.Left = Width - _Border;
  129. }
  130. }
  131. /// <summary>
  132. /// 设置炫彩模式
  133. /// </summary>
  134. protected void SetColorful()
  135. {
  136. string[] colors = ColorStyle.Warm.Concat(ColorStyle.Silence).ToArray();
  137. int index = new Random().Next(colors.Length);
  138. BackColor = ColorTranslator.FromHtml(colors[index]);
  139. }
  140. #region 界面优化
  141. /// <summary>
  142. /// 避免拖动窗口闪烁,使用会导致Windows自带动画失效
  143. /// </summary>
  144. protected override CreateParams CreateParams
  145. {
  146. get
  147. {
  148. const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
  149. CreateParams cp = base.CreateParams;
  150. cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
  151. cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED(任务栏点击最大化最小化)
  152. return cp;
  153. }
  154. }
  155. //protected override void OnPaint(PaintEventArgs e)
  156. //{
  157. // base.OnPaint(e);
  158. // //Graphics g = CreateGraphics();
  159. // //g.DrawRectangle(new Pen(Color.Red, 1), new Rectangle(0, 0, Width, Height));
  160. //}
  161. #endregion
  162. #region Invoke UI操作
  163. public void UIEnable(Control ctrl, bool enable = true)
  164. {
  165. Invoke(new Action(() =>
  166. {
  167. ctrl.Enabled = enable;
  168. }));
  169. }
  170. public void UIVisible(Control ctrl, bool enable = true)
  171. {
  172. Invoke(new Action(() =>
  173. {
  174. ctrl.Visible = enable;
  175. }));
  176. }
  177. public void UILabel(Label label, string s)
  178. {
  179. Invoke(new Action(() =>
  180. {
  181. label.Text = s;
  182. }));
  183. }
  184. public void UIClose()
  185. {
  186. Invoke(new Action(() =>
  187. {
  188. try { Close(); } catch { }
  189. }));
  190. }
  191. #endregion
  192. }
  193. }