NoTitleForm.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6. using Y.Utils.DrawUtils.ColorUtils;
  7. using Y.Utils.WindowsUtils.APIUtils;
  8. namespace Y.Skin.YoForm.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. }
  73. private void NoTitleForm_Load(object sender, EventArgs e)
  74. {
  75. SetBorder();
  76. if (_Colorful) SetColorful();
  77. }
  78. /// <summary>
  79. /// 设置无标题窗口可拖动
  80. /// </summary>
  81. /// <param name="e"></param>
  82. protected override void OnMouseMove(MouseEventArgs e)
  83. {
  84. base.OnMouseMove(e);
  85. if (e.Button == MouseButtons.Left)
  86. {
  87. FormStyleAPI.ReleaseCapture();
  88. FormStyleAPI.SendMessage(Handle, FormStyleAPI.WM_NCLBUTTONDOWN, FormStyleAPI.HTCAPTION, 0);
  89. }
  90. }
  91. /// <summary>
  92. /// 设置窗口边框
  93. /// </summary>
  94. protected void SetBorder()
  95. {
  96. if (_Border > 0)
  97. {
  98. BorderTop.BackColor = _BorderColor;
  99. BorderTop.Width = Width;
  100. BorderTop.Height = _Border;
  101. Controls.Add(BorderTop);
  102. BorderTop.BringToFront();
  103. BorderTop.Top = 0;
  104. BorderTop.Left = 0;
  105. BorderBottom.BackColor = _BorderColor;
  106. BorderBottom.Width = Width;
  107. BorderBottom.Height = _Border;
  108. Controls.Add(BorderBottom);
  109. BorderBottom.BringToFront();
  110. BorderBottom.Top = Height - _Border;
  111. BorderBottom.Left = 0;
  112. BorderLeft.BackColor = _BorderColor;
  113. BorderLeft.Width = _Border;
  114. BorderLeft.Height = Height;
  115. Controls.Add(BorderLeft);
  116. BorderLeft.BringToFront();
  117. BorderLeft.Top = 0;
  118. BorderLeft.Left = 0;
  119. BorderRight.BackColor = _BorderColor;
  120. BorderRight.Width = _Border;
  121. BorderRight.Height = Height;
  122. Controls.Add(BorderRight);
  123. BorderRight.BringToFront();
  124. BorderRight.Top = 0;
  125. BorderRight.Left = Width - _Border;
  126. }
  127. }
  128. /// <summary>
  129. /// 设置炫彩模式
  130. /// </summary>
  131. protected void SetColorful()
  132. {
  133. string[] colors = ColorStyle.Warm.Concat(ColorStyle.Silence).ToArray();
  134. int index = new Random().Next(colors.Length);
  135. BackColor = ColorTranslator.FromHtml(colors[index]);
  136. }
  137. protected override void OnPaint(PaintEventArgs e)
  138. {
  139. base.OnPaint(e);
  140. //Graphics g = CreateGraphics();
  141. //g.DrawRectangle(new Pen(Color.Red, 1), new Rectangle(0, 0, Width, Height));
  142. }
  143. /// <summary>
  144. /// 设置控件是否可用
  145. /// </summary>
  146. /// <param name="ctrl"></param>
  147. /// <param name="enable"></param>
  148. public void UIEnable(Control ctrl, bool enable = true)
  149. {
  150. try
  151. {
  152. BeginInvoke(new Action(() =>
  153. {
  154. ctrl.Enabled = enable;
  155. }));
  156. }
  157. catch (Exception e) { }
  158. }
  159. public void UIVisible(Control ctrl, bool enable = true)
  160. {
  161. try
  162. {
  163. BeginInvoke(new Action(() =>
  164. {
  165. ctrl.Visible = enable;
  166. }));
  167. }
  168. catch (Exception e) { }
  169. }
  170. public void UIClose()
  171. {
  172. try
  173. {
  174. BeginInvoke(new Action(() =>
  175. {
  176. Close();
  177. }));
  178. }
  179. catch (Exception e) { }
  180. }
  181. }
  182. }